Allow comments in $FZF_DEFAULT_OPTS and $FZF_DEFAULT_OPTS_FILE

Close #3961
This commit is contained in:
Junegunn Choi
2024-08-13 18:51:02 +09:00
parent e28f5aa45b
commit e215e2daf3
4 changed files with 22 additions and 7 deletions

View File

@@ -12,7 +12,7 @@ import (
"github.com/junegunn/fzf/src/algo"
"github.com/junegunn/fzf/src/tui"
"github.com/mattn/go-shellwords"
"github.com/junegunn/go-shellwords"
"github.com/rivo/uniseg"
)
@@ -2915,6 +2915,12 @@ func postProcessOptions(opts *Options) error {
return processScheme(opts)
}
func parseShellWords(str string) ([]string, error) {
parser := shellwords.NewParser()
parser.ParseComment = true
return parser.Parse(str)
}
// ParseOptions parses command-line options
func ParseOptions(useDefaults bool, args []string) (*Options, error) {
opts := defaultOptions()
@@ -2928,7 +2934,7 @@ func ParseOptions(useDefaults bool, args []string) (*Options, error) {
return nil, errors.New("$FZF_DEFAULT_OPTS_FILE: " + err.Error())
}
words, parseErr := shellwords.Parse(string(bytes))
words, parseErr := parseShellWords(string(bytes))
if parseErr != nil {
return nil, errors.New(path + ": " + parseErr.Error())
}
@@ -2940,7 +2946,7 @@ func ParseOptions(useDefaults bool, args []string) (*Options, error) {
}
// 2. Options from $FZF_DEFAULT_OPTS string
words, parseErr := shellwords.Parse(os.Getenv("FZF_DEFAULT_OPTS"))
words, parseErr := parseShellWords(os.Getenv("FZF_DEFAULT_OPTS"))
if parseErr != nil {
return nil, errors.New("$FZF_DEFAULT_OPTS: " + parseErr.Error())
}