summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2024-05-07 01:06:42 +0900
committerGitHub <noreply@github.com>2024-05-07 01:06:42 +0900
commite8405f40fe2eb3675f1cb4f69e825eff5f13f269 (patch)
treec917367f1f0098939f9cdf7376a2a135907024fc /main.go
parent065b9e6fb2ce3e6e50ff423c3786989afa04ee14 (diff)
downloadfzf-e8405f40fe2eb3675f1cb4f69e825eff5f13f269.tar.gz
Refactor the code so that fzf can be used as a library (#3769)
Diffstat (limited to 'main.go')
-rw-r--r--main.go35
1 files changed, 31 insertions, 4 deletions
diff --git a/main.go b/main.go
index 768148ef..f0074b3e 100644
--- a/main.go
+++ b/main.go
@@ -3,14 +3,15 @@ package main
import (
_ "embed"
"fmt"
+ "os"
"strings"
fzf "github.com/junegunn/fzf/src"
"github.com/junegunn/fzf/src/protector"
)
-var version string = "0.51"
-var revision string = "devel"
+var version = "0.51"
+var revision = "devel"
//go:embed shell/key-bindings.bash
var bashKeyBindings []byte
@@ -33,9 +34,21 @@ func printScript(label string, content []byte) {
fmt.Println("### end: " + label + " ###")
}
+func exit(code int, err error) {
+ if err != nil {
+ os.Stderr.WriteString(err.Error() + "\n")
+ }
+ os.Exit(code)
+}
+
func main() {
protector.Protect()
- options := fzf.ParseOptions()
+
+ options, err := fzf.ParseOptions(true, os.Args[1:])
+ if err != nil {
+ exit(fzf.ExitError, err)
+ return
+ }
if options.Bash {
printScript("key-bindings.bash", bashKeyBindings)
printScript("completion.bash", bashCompletion)
@@ -51,5 +64,19 @@ func main() {
fmt.Println("fzf_key_bindings")
return
}
- fzf.Run(options, version, revision)
+ 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
+ }
+
+ code, err := fzf.Run(options)
+ exit(code, err)
}