summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2024-02-29 09:49:33 +0900
committerGitHub <noreply@github.com>2024-02-29 09:49:33 +0900
commit1833670fb91dfa17047af320ce7f015e0d500ba6 (patch)
treec202dd6107c1139e9082dc7852014a780fa60efa /src
parent3dd42f5aa2d7fc6f508f2b21408730a8678c0a19 (diff)
Add $FZF_DEFAULT_OPTS_FILE (#3618)
For those who prefer to manage default options in a file. If the file is not found, fzf will exit with an error. We're not setting a default value for it because: 1. it's hard to find a default value that can be universally agreed upon 2. to avoid fzf having to check for the existence of the file even when it's not used
Diffstat (limited to 'src')
-rw-r--r--src/options.go37
1 files changed, 31 insertions, 6 deletions
diff --git a/src/options.go b/src/options.go
index dcb70dcb..09787a44 100644
--- a/src/options.go
+++ b/src/options.go
@@ -126,8 +126,8 @@ const usage = `usage: fzf [options]
Environment variables
FZF_DEFAULT_COMMAND Default command to use when input is tty
- FZF_DEFAULT_OPTS Default options
- (e.g. '--layout=reverse --inline-info')
+ FZF_DEFAULT_OPTS Default options (e.g. '--layout=reverse --info=inline')
+ FZF_DEFAULT_OPTS_FILE Location of the file to read default options from
FZF_API_KEY X-API-Key header for HTTP server (--listen)
`
@@ -421,8 +421,10 @@ func help(code int) {
os.Exit(code)
}
+var errorContext = ""
+
func errorExit(msg string) {
- os.Stderr.WriteString(msg + "\n")
+ os.Stderr.WriteString(errorContext + msg + "\n")
os.Exit(exitError)
}
@@ -2167,13 +2169,36 @@ func ParseOptions() *Options {
}
}
- // Options from Env var
- words, _ := shellwords.Parse(os.Getenv("FZF_DEFAULT_OPTS"))
+ // 1. Options from $FZF_DEFAULT_OPTS_FILE
+ if path := os.Getenv("FZF_DEFAULT_OPTS_FILE"); path != "" {
+ bytes, err := os.ReadFile(path)
+ if err != nil {
+ errorContext = "$FZF_DEFAULT_OPTS_FILE: "
+ errorExit(err.Error())
+ }
+
+ words, parseErr := shellwords.Parse(string(bytes))
+ if parseErr != nil {
+ errorContext = path + ": "
+ errorExit(parseErr.Error())
+ }
+ if len(words) > 0 {
+ parseOptions(opts, words)
+ }
+ }
+
+ // 2. Options from $FZF_DEFAULT_OPTS string
+ words, parseErr := shellwords.Parse(os.Getenv("FZF_DEFAULT_OPTS"))
+ errorContext = "$FZF_DEFAULT_OPTS: "
+ if parseErr != nil {
+ errorExit(parseErr.Error())
+ }
if len(words) > 0 {
parseOptions(opts, words)
}
- // Options from command-line arguments
+ // 3. Options from command-line arguments
+ errorContext = ""
parseOptions(opts, os.Args[1:])
postProcessOptions(opts)