2015-01-01 19:49:30 +00:00
|
|
|
package main
|
|
|
|
|
2020-01-19 05:13:32 +00:00
|
|
|
import (
|
2024-03-13 14:59:34 +00:00
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
2024-05-06 16:06:42 +00:00
|
|
|
"os"
|
2024-05-20 10:33:46 +00:00
|
|
|
"os/exec"
|
2024-03-13 14:59:34 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-11-03 15:59:04 +00:00
|
|
|
fzf "github.com/junegunn/fzf/src"
|
2020-01-19 05:13:32 +00:00
|
|
|
"github.com/junegunn/fzf/src/protector"
|
|
|
|
)
|
2015-01-01 19:49:30 +00:00
|
|
|
|
2024-07-07 07:30:23 +00:00
|
|
|
var version = "0.54"
|
2024-05-06 16:06:42 +00:00
|
|
|
var revision = "devel"
|
2017-06-02 08:57:28 +00:00
|
|
|
|
2024-03-13 14:59:34 +00:00
|
|
|
//go:embed shell/key-bindings.bash
|
|
|
|
var bashKeyBindings []byte
|
|
|
|
|
|
|
|
//go:embed shell/completion.bash
|
|
|
|
var bashCompletion []byte
|
|
|
|
|
|
|
|
//go:embed shell/key-bindings.zsh
|
|
|
|
var zshKeyBindings []byte
|
|
|
|
|
|
|
|
//go:embed shell/completion.zsh
|
|
|
|
var zshCompletion []byte
|
|
|
|
|
|
|
|
//go:embed shell/key-bindings.fish
|
|
|
|
var fishKeyBindings []byte
|
|
|
|
|
2024-05-20 10:33:46 +00:00
|
|
|
//go:embed man/man1/fzf.1
|
|
|
|
var manPage []byte
|
|
|
|
|
2024-03-13 14:59:34 +00:00
|
|
|
func printScript(label string, content []byte) {
|
|
|
|
fmt.Println("### " + label + " ###")
|
|
|
|
fmt.Println(strings.TrimSpace(string(content)))
|
|
|
|
fmt.Println("### end: " + label + " ###")
|
|
|
|
}
|
|
|
|
|
2024-05-06 16:06:42 +00:00
|
|
|
func exit(code int, err error) {
|
2024-06-15 01:23:03 +00:00
|
|
|
if code == fzf.ExitError && err != nil {
|
2024-05-07 14:49:30 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
2024-05-06 16:06:42 +00:00
|
|
|
}
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
2015-01-01 19:49:30 +00:00
|
|
|
func main() {
|
2020-01-19 05:13:32 +00:00
|
|
|
protector.Protect()
|
2024-05-06 16:06:42 +00:00
|
|
|
|
|
|
|
options, err := fzf.ParseOptions(true, os.Args[1:])
|
|
|
|
if err != nil {
|
|
|
|
exit(fzf.ExitError, err)
|
|
|
|
return
|
|
|
|
}
|
2024-03-13 14:59:34 +00:00
|
|
|
if options.Bash {
|
|
|
|
printScript("key-bindings.bash", bashKeyBindings)
|
|
|
|
printScript("completion.bash", bashCompletion)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if options.Zsh {
|
|
|
|
printScript("key-bindings.zsh", zshKeyBindings)
|
|
|
|
printScript("completion.zsh", zshCompletion)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if options.Fish {
|
|
|
|
printScript("key-bindings.fish", fishKeyBindings)
|
|
|
|
fmt.Println("fzf_key_bindings")
|
|
|
|
return
|
|
|
|
}
|
2024-05-06 16:06:42 +00:00
|
|
|
if options.Help {
|
|
|
|
fmt.Print(fzf.Usage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if options.Version {
|
|
|
|
if len(revision) > 0 {
|
|
|
|
fmt.Printf("%s (%s)\n", version, revision)
|
|
|
|
} else {
|
|
|
|
fmt.Println(version)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-05-20 10:33:46 +00:00
|
|
|
if options.Man {
|
|
|
|
file := fzf.WriteTemporaryFile([]string{string(manPage)}, "\n")
|
|
|
|
if len(file) == 0 {
|
|
|
|
fmt.Print(string(manPage))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove(file)
|
|
|
|
cmd := exec.Command("man", file)
|
2024-06-01 04:23:46 +00:00
|
|
|
cmd.Stdin = os.Stdin
|
2024-05-20 10:33:46 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
fmt.Print(string(manPage))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-05-06 16:06:42 +00:00
|
|
|
|
|
|
|
code, err := fzf.Run(options)
|
|
|
|
exit(code, err)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|