--tmux vs. --height: Last one wins

This commit is contained in:
Junegunn Choi 2024-06-01 19:50:17 +09:00
parent cfdf2f1153
commit e2f93e5a2d
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
3 changed files with 31 additions and 21 deletions

View File

@ -20,7 +20,7 @@ Matcher -> EvtHeader -> Terminal (update header)
// Run starts fzf // Run starts fzf
func Run(opts *Options) (int, error) { func Run(opts *Options) (int, error) {
if opts.Tmux != nil && len(os.Getenv("TMUX")) > 0 { if opts.Tmux != nil && len(os.Getenv("TMUX")) > 0 && opts.Tmux.index >= opts.Height.index {
return runTmux(os.Args, opts) return runTmux(os.Args, opts)
} }

View File

@ -190,6 +190,7 @@ type heightSpec struct {
percent bool percent bool
auto bool auto bool
inverse bool inverse bool
index int
} }
type sizeSpec struct { type sizeSpec struct {
@ -230,6 +231,7 @@ type tmuxOptions struct {
width sizeSpec width sizeSpec
height sizeSpec height sizeSpec
position windowPosition position windowPosition
index int
} }
type layoutType int type layoutType int
@ -279,16 +281,17 @@ func (o *previewOpts) Toggle() {
o.hidden = !o.hidden o.hidden = !o.hidden
} }
func defaultTmuxOptions() *tmuxOptions { func defaultTmuxOptions(index int) *tmuxOptions {
return &tmuxOptions{ return &tmuxOptions{
position: posCenter, position: posCenter,
width: sizeSpec{50, true}, width: sizeSpec{50, true},
height: sizeSpec{50, true}} height: sizeSpec{50, true},
index: index}
} }
func parseTmuxOptions(arg string) (*tmuxOptions, error) { func parseTmuxOptions(arg string, index int) (*tmuxOptions, error) {
var err error var err error
opts := defaultTmuxOptions() opts := defaultTmuxOptions(index)
tokens := splitRegexp.Split(arg, -1) tokens := splitRegexp.Split(arg, -1)
errorToReturn := errors.New("invalid tmux option: " + arg + " (expected: [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]])") errorToReturn := errors.New("invalid tmux option: " + arg + " (expected: [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]])")
if len(tokens) == 0 || len(tokens) > 3 { if len(tokens) == 0 || len(tokens) > 3 {
@ -1635,8 +1638,8 @@ func parseSize(str string, maxPercent float64, label string) (sizeSpec, error) {
return sizeSpec{val, percent}, nil return sizeSpec{val, percent}, nil
} }
func parseHeight(str string) (heightSpec, error) { func parseHeight(str string, index int) (heightSpec, error) {
heightSpec := heightSpec{} heightSpec := heightSpec{index: index}
if strings.HasPrefix(str, "~") { if strings.HasPrefix(str, "~") {
heightSpec.auto = true heightSpec.auto = true
str = str[1:] str = str[1:]
@ -1884,7 +1887,7 @@ func parseMarkerMultiLine(str string) ([3]string, error) {
return result, nil return result, nil
} }
func parseOptions(opts *Options, allArgs []string) error { func parseOptions(index *int, opts *Options, allArgs []string) error {
var err error var err error
var historyMax int var historyMax int
if opts.History == nil { if opts.History == nil {
@ -1920,8 +1923,10 @@ func parseOptions(opts *Options, allArgs []string) error {
opts.Version = false opts.Version = false
opts.Man = false opts.Man = false
} }
startIndex := *index
for i := 0; i < len(allArgs); i++ { for i := 0; i < len(allArgs); i++ {
arg := allArgs[i] arg := allArgs[i]
index := i + startIndex
switch arg { switch arg {
case "--man": case "--man":
clearExitingOpts() clearExitingOpts()
@ -1946,11 +1951,11 @@ func parseOptions(opts *Options, allArgs []string) error {
case "--tmux": case "--tmux":
given, str := optionalNextString(allArgs, &i) given, str := optionalNextString(allArgs, &i)
if given { if given {
if opts.Tmux, err = parseTmuxOptions(str); err != nil { if opts.Tmux, err = parseTmuxOptions(str, index); err != nil {
return err return err
} }
} else { } else {
opts.Tmux = defaultTmuxOptions() opts.Tmux = defaultTmuxOptions(index)
} }
case "--no-tmux": case "--no-tmux":
opts.Tmux = nil opts.Tmux = nil
@ -2314,7 +2319,7 @@ func parseOptions(opts *Options, allArgs []string) error {
if err != nil { if err != nil {
return err return err
} }
if opts.Height, err = parseHeight(str); err != nil { if opts.Height, err = parseHeight(str, index); err != nil {
return err return err
} }
case "--min-height": case "--min-height":
@ -2456,7 +2461,7 @@ func parseOptions(opts *Options, allArgs []string) error {
return err return err
} }
} else if match, value := optString(arg, "--tmux="); match { } else if match, value := optString(arg, "--tmux="); match {
if opts.Tmux, err = parseTmuxOptions(value); err != nil { if opts.Tmux, err = parseTmuxOptions(value, index); err != nil {
return err return err
} }
} else if match, value := optString(arg, "--scheme="); match { } else if match, value := optString(arg, "--scheme="); match {
@ -2510,7 +2515,7 @@ func parseOptions(opts *Options, allArgs []string) error {
return err return err
} }
} else if match, value := optString(arg, "--height="); match { } else if match, value := optString(arg, "--height="); match {
if opts.Height, err = parseHeight(value); err != nil { if opts.Height, err = parseHeight(value, index); err != nil {
return err return err
} }
} else if match, value := optString(arg, "--min-height="); match { } else if match, value := optString(arg, "--min-height="); match {
@ -2631,6 +2636,7 @@ func parseOptions(opts *Options, allArgs []string) error {
} }
} }
} }
*index += len(allArgs)
if opts.HeaderLines < 0 { if opts.HeaderLines < 0 {
return errors.New("header lines must be a non-negative integer") return errors.New("header lines must be a non-negative integer")
@ -2852,6 +2858,7 @@ func postProcessOptions(opts *Options) error {
// ParseOptions parses command-line options // ParseOptions parses command-line options
func ParseOptions(useDefaults bool, args []string) (*Options, error) { func ParseOptions(useDefaults bool, args []string) (*Options, error) {
opts := defaultOptions() opts := defaultOptions()
index := 0
if useDefaults { if useDefaults {
// 1. Options from $FZF_DEFAULT_OPTS_FILE // 1. Options from $FZF_DEFAULT_OPTS_FILE
@ -2866,7 +2873,7 @@ func ParseOptions(useDefaults bool, args []string) (*Options, error) {
return nil, errors.New(path + ": " + parseErr.Error()) return nil, errors.New(path + ": " + parseErr.Error())
} }
if len(words) > 0 { if len(words) > 0 {
if err := parseOptions(opts, words); err != nil { if err := parseOptions(&index, opts, words); err != nil {
return nil, errors.New(path + ": " + err.Error()) return nil, errors.New(path + ": " + err.Error())
} }
} }
@ -2878,14 +2885,14 @@ func ParseOptions(useDefaults bool, args []string) (*Options, error) {
return nil, errors.New("$FZF_DEFAULT_OPTS: " + parseErr.Error()) return nil, errors.New("$FZF_DEFAULT_OPTS: " + parseErr.Error())
} }
if len(words) > 0 { if len(words) > 0 {
if err := parseOptions(opts, words); err != nil { if err := parseOptions(&index, opts, words); err != nil {
return nil, errors.New("$FZF_DEFAULT_OPTS: " + err.Error()) return nil, errors.New("$FZF_DEFAULT_OPTS: " + err.Error())
} }
} }
} }
// 3. Options from command-line arguments // 3. Options from command-line arguments
if err := parseOptions(opts, args); err != nil { if err := parseOptions(&index, opts, args); err != nil {
return nil, err return nil, err
} }

View File

@ -106,10 +106,11 @@ func TestSplitNth(t *testing.T) {
} }
func TestIrrelevantNth(t *testing.T) { func TestIrrelevantNth(t *testing.T) {
index := 0
{ {
opts := defaultOptions() opts := defaultOptions()
words := []string{"--nth", "..", "-x"} words := []string{"--nth", "..", "-x"}
parseOptions(opts, words) parseOptions(&index, opts, words)
postProcessOptions(opts) postProcessOptions(opts)
if len(opts.Nth) != 0 { if len(opts.Nth) != 0 {
t.Errorf("nth should be empty: %v", opts.Nth) t.Errorf("nth should be empty: %v", opts.Nth)
@ -118,7 +119,7 @@ func TestIrrelevantNth(t *testing.T) {
for _, words := range [][]string{{"--nth", "..,3", "+x"}, {"--nth", "3,1..", "+x"}, {"--nth", "..-1,1", "+x"}} { for _, words := range [][]string{{"--nth", "..,3", "+x"}, {"--nth", "3,1..", "+x"}, {"--nth", "..-1,1", "+x"}} {
{ {
opts := defaultOptions() opts := defaultOptions()
parseOptions(opts, words) parseOptions(&index, opts, words)
postProcessOptions(opts) postProcessOptions(opts)
if len(opts.Nth) != 0 { if len(opts.Nth) != 0 {
t.Errorf("nth should be empty: %v", opts.Nth) t.Errorf("nth should be empty: %v", opts.Nth)
@ -127,7 +128,7 @@ func TestIrrelevantNth(t *testing.T) {
{ {
opts := defaultOptions() opts := defaultOptions()
words = append(words, "-x") words = append(words, "-x")
parseOptions(opts, words) parseOptions(&index, opts, words)
postProcessOptions(opts) postProcessOptions(opts)
if len(opts.Nth) != 2 { if len(opts.Nth) != 2 {
t.Errorf("nth should not be empty: %v", opts.Nth) t.Errorf("nth should not be empty: %v", opts.Nth)
@ -335,10 +336,11 @@ func TestColorSpec(t *testing.T) {
} }
func TestDefaultCtrlNP(t *testing.T) { func TestDefaultCtrlNP(t *testing.T) {
index := 0
check := func(words []string, et tui.EventType, expected actionType) { check := func(words []string, et tui.EventType, expected actionType) {
e := et.AsEvent() e := et.AsEvent()
opts := defaultOptions() opts := defaultOptions()
parseOptions(opts, words) parseOptions(&index, opts, words)
postProcessOptions(opts) postProcessOptions(opts)
if opts.Keymap[e][0].t != expected { if opts.Keymap[e][0].t != expected {
t.Error() t.Error()
@ -364,8 +366,9 @@ func TestDefaultCtrlNP(t *testing.T) {
} }
func optsFor(words ...string) *Options { func optsFor(words ...string) *Options {
index := 0
opts := defaultOptions() opts := defaultOptions()
parseOptions(opts, words) parseOptions(&index, opts, words)
postProcessOptions(opts) postProcessOptions(opts)
return opts return opts
} }