Skip to content

Commit

Permalink
Add $FZF_DEFAULT_OPTS_FILE
Browse files Browse the repository at this point in the history
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
  • Loading branch information
junegunn committed Feb 8, 2024
1 parent 90b8187 commit b1acf4b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -311,6 +311,10 @@ or `py`.
- `FZF_DEFAULT_OPTS`
- Default options
- e.g. `export FZF_DEFAULT_OPTS="--layout=reverse --inline-info"`
- `FZF_DEFAULT_OPTS_FILE`
- If you prefer to manage default options in a file, set this variable to
point to the location of the file
- e.g. `export FZF_DEFAULT_OPTS_FILE=~/.fzfrc`

### Options

Expand Down
3 changes: 3 additions & 0 deletions man/man1/fzf.1
Expand Up @@ -867,6 +867,9 @@ this case make sure that the command is POSIX-compliant.
.B FZF_DEFAULT_OPTS
Default options. e.g. \fBexport FZF_DEFAULT_OPTS="--extended --cycle"\fR
.TP
.B FZF_DEFAULT_OPTS_FILE
The location of the file that contains the default options. e.g. \fBexport FZF_DEFAULT_OPTS_FILE=~/.fzfrc\fR
.TP
.B FZF_API_KEY
Can be used to require an API key when using \fB--listen\fR option. If not set,
no authentication will be required by the server. You can set this value if
Expand Down
33 changes: 29 additions & 4 deletions src/options.go
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b1acf4b

Please sign in to comment.