| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
_ "embed" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
"os/exec" |
| 8 |
"strings" |
| 9 |
|
| 10 |
fzf "github.com/junegunn/fzf/src" |
| 11 |
"github.com/junegunn/fzf/src/protector" |
| 12 |
) |
| 13 |
|
| 14 |
var version = "0.74" |
| 15 |
var revision = "devel" |
| 16 |
|
| 17 |
//go:embed shell/key-bindings.bash |
| 18 |
var bashKeyBindings []byte |
| 19 |
|
| 20 |
//go:embed shell/completion.bash |
| 21 |
var bashCompletion []byte |
| 22 |
|
| 23 |
//go:embed shell/key-bindings.zsh |
| 24 |
var zshKeyBindings []byte |
| 25 |
|
| 26 |
//go:embed shell/completion.zsh |
| 27 |
var zshCompletion []byte |
| 28 |
|
| 29 |
//go:embed shell/key-bindings.fish |
| 30 |
var fishKeyBindings []byte |
| 31 |
|
| 32 |
//go:embed shell/key-bindings.nu |
| 33 |
var nushellKeyBindings []byte |
| 34 |
|
| 35 |
//go:embed shell/completion.nu |
| 36 |
var nushellCompletion []byte |
| 37 |
|
| 38 |
//go:embed shell/completion.fish |
| 39 |
var fishCompletion []byte |
| 40 |
|
| 41 |
//go:embed man/man1/fzf.1 |
| 42 |
var manPage []byte |
| 43 |
|
| 44 |
func printScript(label string, content []byte) { |
| 45 |
fmt.Println("### " + label + " ###") |
| 46 |
fmt.Println(strings.TrimSpace(string(content))) |
| 47 |
fmt.Println("### end: " + label + " ###") |
| 48 |
} |
| 49 |
|
| 50 |
func exit(code int, err error) { |
| 51 |
if code == fzf.ExitError && err != nil { |
| 52 |
fmt.Fprintln(os.Stderr, err.Error()) |
| 53 |
} |
| 54 |
os.Exit(code) |
| 55 |
} |
| 56 |
|
| 57 |
func main() { |
| 58 |
protector.Protect() |
| 59 |
|
| 60 |
options, err := fzf.ParseOptions(true, os.Args[1:]) |
| 61 |
if err != nil { |
| 62 |
exit(fzf.ExitError, err) |
| 63 |
return |
| 64 |
} |
| 65 |
if options.Bash { |
| 66 |
printScript("key-bindings.bash", bashKeyBindings) |
| 67 |
printScript("completion.bash", bashCompletion) |
| 68 |
return |
| 69 |
} |
| 70 |
if options.Zsh { |
| 71 |
printScript("key-bindings.zsh", zshKeyBindings) |
| 72 |
printScript("completion.zsh", zshCompletion) |
| 73 |
return |
| 74 |
} |
| 75 |
if options.Fish { |
| 76 |
printScript("key-bindings.fish", fishKeyBindings) |
| 77 |
printScript("completion.fish", fishCompletion) |
| 78 |
return |
| 79 |
} |
| 80 |
if options.Nushell { |
| 81 |
printScript("key-bindings.nu", nushellKeyBindings) |
| 82 |
printScript("completion.nu", nushellCompletion) |
| 83 |
return |
| 84 |
} |
| 85 |
if options.Help { |
| 86 |
fmt.Print(fzf.Usage) |
| 87 |
return |
| 88 |
} |
| 89 |
if options.Version { |
| 90 |
if len(revision) > 0 { |
| 91 |
fmt.Printf("%s (%s)\n", version, revision) |
| 92 |
} else { |
| 93 |
fmt.Println(version) |
| 94 |
} |
| 95 |
return |
| 96 |
} |
| 97 |
if options.Man { |
| 98 |
file := fzf.WriteTemporaryFile([]string{string(manPage)}, "\n") |
| 99 |
if len(file) == 0 { |
| 100 |
fmt.Print(string(manPage)) |
| 101 |
return |
| 102 |
} |
| 103 |
defer os.Remove(file) |
| 104 |
cmd := exec.Command("man", file) |
| 105 |
cmd.Stdin = os.Stdin |
| 106 |
cmd.Stdout = os.Stdout |
| 107 |
if err := cmd.Run(); err != nil { |
| 108 |
fmt.Print(string(manPage)) |
| 109 |
} |
| 110 |
return |
| 111 |
} |
| 112 |
|
| 113 |
code, err := fzf.Run(options) |
| 114 |
exit(code, err) |
| 115 |
} |