Make pointer and multi-select marker customizable (#1844)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add --pointer and --marker option which can provide additional context to the user
authored by Hiroki Konishi · committed by GitHub · parent d61ac32 · Browse files · 6 files·+203 added−73 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+203 added−73 removed
- tests:+33 added−0 removed
- other:+170 added−73 removed
Provenance
- Commit
2a60edc- Tree
6ff74c8- Author
Hiroki Konishi <relastle@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
d61ac32
6 files changed+203 added−73 removed
modified:man/man1/fzf.1+6 added
229 unmodified lines230 .TP231 .BI "--prompt=" "STR"232 Input prompt (default: '> ')233 .TP234+.BI "--pointer=" "STR"235+Pointer to the current line (default: '>')236+.TP237+.BI "--marker=" "STR"238+Multi-select marker (default: '>')239+.TP240 .BI "--header=" "STR"241 The given string will be printed as the sticky header. The lines are displayed242 in the given order from top to bottom regardless of \fB--layout\fR option, and243 are not affected by \fB--with-nth\fR. ANSI color codes are processed even when533 unmodified linesmodified:shell/completion.bash+2 added
70 unmodified lines71 --reverse72 --margin73 --inline-info74 --prompt75+ --pointer76+ --marker77 --header78 --header-lines79 --ansi80 --tabstop260 unmodified linesmodified:src/options_test.go+26 added
420 unmodified lines421 if len(opts.Expect) != 3 {422 t.Error(opts.Expect)423 }424 }425+426+func TestValidateSign(t *testing.T) {427+ testCases := []struct {428+ inputSign string429+ isValid bool430+ }{431+ {"> ", true},432+ {"아", true},433+ {"😀", true},434+ {"", false},435+ {">>>", false},436+ {"\n", false},437+ {"\t", false},438+ }439+440+ for _, testCase := range testCases {441+ err := validateSign(testCase.inputSign, "")442+ if testCase.isValid && err != nil {443+ t.Errorf("Input sign `%s` caused error", testCase.inputSign)444+ }445+446+ if !testCase.isValid && err == nil {447+ t.Errorf("Input sign `%s` did not cause error", testCase.inputSign)448+ }449+ }450+}modified:src/options.go+51 added
4 unmodified lines5 "os"6 "regexp"7 "strconv"8 "strings"9+ "unicode"10 "unicode/utf8"11 12 "github.com/junegunn/fzf/src/algo"13 "github.com/junegunn/fzf/src/tui"14 "github.com/junegunn/fzf/src/util"15 16+ "github.com/mattn/go-runewidth"17 "github.com/mattn/go-shellwords"18 )19 20 const usage = `usage: fzf [options]39 unmodified lines60 --border Draw border above and below the finder61 --margin=MARGIN Screen margin (TRBL / TB,RL / T,RL,B / T,R,B,L)62 --info=STYLE Finder info style [default|inline|hidden]63 --prompt=STR Input prompt (default: '> ')64+ --pointer=STR Pointer to the current line (default: '>')65+ --marker=STR Multi-select marker (default: '>')66 --header=STR String to print as header67 --header-lines=N The first N lines of the input are treated as header68 69 Display122 unmodified lines192 FileWord bool193 InfoStyle infoStyle194 JumpLabels string195 Prompt string196+ Pointer string197+ Marker string198 Query string199 Select1 bool200 Exit0 bool201 Filter *string45 unmodified lines247 FileWord: false,248 InfoStyle: infoDefault,249 JumpLabels: defaultJumpLabels,250 Prompt: "> ",251+ Pointer: ">",252+ Marker: ">",253 Query: "",254 Select1: false,255 Exit0: false,256 Filter: nil,791 unmodified lines1048 opts.History.maxSize = historyMax1049 }1050 }1051 validateJumpLabels := false1052+ validatePointer := false1053+ validateMarker := false1054 for i := 0; i < len(allArgs); i++ {1055 arg := allArgs[i]1056 switch arg {1057 case "-h", "--help":140 unmodified lines1198 case "--no-print-query":1199 opts.PrintQuery = false1200 case "--prompt":1201 opts.Prompt = nextString(allArgs, &i, "prompt string required")1202+ case "--pointer":1203+ opts.Pointer = nextString(allArgs, &i, "pointer sign string required")1204+ validatePointer = true1205+ case "--marker":1206+ opts.Marker = nextString(allArgs, &i, "selected sign string required")1207+ validateMarker = true1208 case "--sync":1209 opts.Sync = true1210 case "--no-sync":1211 opts.Sync = false58 unmodified lines1270 } else if match, value := optString(arg, "-d", "--delimiter="); match {1271 opts.Delimiter = delimiterRegexp(value)1272 } else if match, value := optString(arg, "--prompt="); match {1273 opts.Prompt = value1274+ } else if match, value := optString(arg, "--pointer="); match {1275+ opts.Pointer = value1276+ validatePointer = true1277+ } else if match, value := optString(arg, "--marker="); match {1278+ opts.Marker = value1279+ validateMarker = true1280 } else if match, value := optString(arg, "-n", "--nth="); match {1281 opts.Nth = splitNth(value)1282 } else if match, value := optString(arg, "--with-nth="); match {1283 opts.WithNth = splitNth(value)70 unmodified lines1354 errorExit("non-ascii jump labels are not allowed")1355 }1356 }1357 }1358+1359+ if validatePointer {1360+ if err := validateSign(opts.Pointer, "pointer"); err != nil {1361+ errorExit(err.Error())1362+ }1363+ }1364+1365+ if validateMarker {1366+ if err := validateSign(opts.Marker, "marker"); err != nil {1367+ errorExit(err.Error())1368+ }1369+ }1370 }1371 1372+func validateSign(sign string, signOptName string) error {1373+ if sign == "" {1374+ return fmt.Errorf("%v cannot be empty", signOptName)1375+ }1376+ widthSum := 01377+ for _, r := range sign {1378+ if !unicode.IsGraphic(r) {1379+ return fmt.Errorf("invalid character in %v", signOptName)1380+ }1381+ widthSum += runewidth.RuneWidth(r)1382+ if widthSum > 2 {1383+ return fmt.Errorf("%v display width should be up to 2", signOptName)1384+ }1385+ }1386+ return nil1387+}1388+1389 func postProcessOptions(opts *Options) {1390 if util.IsWindows() && opts.Height.size > 0 {1391 errorExit("--height option is currently not supported on Windows")1392 }50 unmodified linesmodified:src/terminal.go+85 added-73 removed
57 unmodified lines58 var emptyLine = itemLine{}59 60 // Terminal represents terminal input/output61 type Terminal struct {62+ initDelay time.Duration63+ infoStyle infoStyle64+ spinner []string65+ prompt string66+ promptLen int67+ pointer string62- initDelay time.Duration63- infoStyle infoStyle64- spinner []string65- prompt string66- promptLen int67- queryLen [2]int68- layout layoutType69- fullscreen bool70- hscroll bool71- hscrollOff int72- wordRubout string73- wordNext string74- cx int75- cy int76- offset int77- xoffset int78- yanked []rune79- input []rune80- multi int81- sort bool82- toggleSort bool83- delimiter Delimiter84- expect map[int]string85- keymap map[int][]action86- pressed string87- printQuery bool88- history *History89- cycle bool90- header []string91- header0 []string92- ansi bool93- tabstop int94- margin [4]sizeSpec95- strong tui.Attr96- unicode bool97- bordered bool98- cleanExit bool99- border tui.Window100- window tui.Window101- pborder tui.Window102- pwindow tui.Window103- count int104- progress int105- reading bool106- failed *string107- jumping jumpMode108- jumpLabels string109- printer func(string)110- printsep string111- merger *Merger112- selected map[int32]selectedItem113- version int64114- reqBox *util.EventBox115- preview previewOpts116- previewer previewer117- previewBox *util.EventBox118- eventBox *util.EventBox119- mutex sync.Mutex120- initFunc func()121- prevLines []itemLine122- suppress bool123- startChan chan bool124- killChan chan int125- slab *util.Slab126- theme *tui.ColorTheme127- tui tui.Renderer68+ pointerLen int69+ pointerEmpty string70+ marker string71+ markerLen int72+ markerEmpty string73+ queryLen [2]int74+ layout layoutType75+ fullscreen bool76+ hscroll bool77+ hscrollOff int78+ wordRubout string79+ wordNext string80+ cx int81+ cy int82+ offset int83+ xoffset int84+ yanked []rune85+ input []rune86+ multi int87+ sort bool88+ toggleSort bool89+ delimiter Delimiter90+ expect map[int]string91+ keymap map[int][]action92+ pressed string93+ printQuery bool94+ history *History95+ cycle bool96+ header []string97+ header0 []string98+ ansi bool99+ tabstop int100+ margin [4]sizeSpec101+ strong tui.Attr102+ unicode bool103+ bordered bool104+ cleanExit bool105+ border tui.Window106+ window tui.Window107+ pborder tui.Window108+ pwindow tui.Window109+ count int110+ progress int111+ reading bool112+ failed *string113+ jumping jumpMode114+ jumpLabels string115+ printer func(string)116+ printsep string117+ merger *Merger118+ selected map[int32]selectedItem119+ version int64120+ reqBox *util.EventBox121+ preview previewOpts122+ previewer previewer123+ previewBox *util.EventBox124+ eventBox *util.EventBox125+ mutex sync.Mutex126+ initFunc func()127+ prevLines []itemLine128+ suppress bool129+ startChan chan bool130+ killChan chan int131+ slab *util.Slab132+ theme *tui.ColorTheme133+ tui tui.Renderer134 }135 136 type selectedItem struct {137 at time.Time308 unmodified lines446 killChan: make(chan int),447 tui: renderer,448 initFunc: func() { renderer.Init() }}449 t.prompt, t.promptLen = t.processTabs([]rune(opts.Prompt), 0)450+ t.pointer, t.pointerLen = t.processTabs([]rune(opts.Pointer), 0)451+ t.marker, t.markerLen = t.processTabs([]rune(opts.Marker), 0)452+ // Pre-calculated empty pointer and marker signs453+ t.pointerEmpty = strings.Repeat(" ", t.pointerLen)454+ t.markerEmpty = strings.Repeat(" ", t.markerLen)455+456 return &t457 }458 459 func (t *Terminal) noInfoLine() bool {403 unmodified lines863 864 func (t *Terminal) printItem(result Result, line int, i int, current bool) {865 item := result.item866 _, selected := t.selected[item.Index()]855- label := " "867+ label := t.pointerEmpty868 if t.jumping != jumpDisabled {869 if i < len(t.jumpLabels) {870 // Striped871 current = i%2 == 0860- label = t.jumpLabels[i : i+1]872+ label = t.jumpLabels[i:i+1] + strings.Repeat(" ", t.pointerLen-1)873 }874 } else if current {863- label = ">"875+ label = t.pointer876 }877 878 // Avoid unnecessary redraw879 newLine := itemLine{current: current, selected: selected, label: label,10 unmodified lines890 t.move(line, 0, false)891 if current {892 t.window.CPrint(tui.ColCurrentCursor, t.strong, label)893 if selected {882- t.window.CPrint(tui.ColCurrentSelected, t.strong, ">")894+ t.window.CPrint(tui.ColCurrentSelected, t.strong, t.marker)895 } else {884- t.window.CPrint(tui.ColCurrentSelected, t.strong, " ")896+ t.window.CPrint(tui.ColCurrentSelected, t.strong, t.markerEmpty)897 }898 newLine.width = t.printHighlighted(result, t.strong, tui.ColCurrent, tui.ColCurrentMatch, true, true)899 } else {900 t.window.CPrint(tui.ColCursor, t.strong, label)901 if selected {890- t.window.CPrint(tui.ColSelected, t.strong, ">")902+ t.window.CPrint(tui.ColSelected, t.strong, t.marker)903 } else {892- t.window.Print(" ")904+ t.window.Print(t.markerEmpty)905 }906 newLine.width = t.printHighlighted(result, 0, tui.ColNormal, tui.ColMatch, false, true)907 }908 fillSpaces := prevLine.width - newLine.width1293 unmodified linesmodified:test/test_go.rb+33 added
1405 unmodified lines1406 tmux.send_keys '3'1407 assert_equal '3', readonce.chomp1408 end1409 1410+ def test_pointer1411+ pointer = '>>'1412+ tmux.send_keys "seq 10 | #{fzf "--pointer '#{pointer}'"}", :Enter1413+ tmux.until { |lines| lines[-2] == ' 10/10' }1414+ lines = tmux.capture1415+ # Assert that specified pointer is displayed1416+ assert_equal "#{pointer} 1", lines[-3]1417+ end1418+1419+ def test_pointer_with_jump1420+ pointer = '>>'1421+ tmux.send_keys "seq 10 | #{fzf "--multi --jump-labels 12345 --bind 'ctrl-j:jump' --pointer '#{pointer}'"}", :Enter1422+ tmux.until { |lines| lines[-2] == ' 10/10' }1423+ tmux.send_keys 'C-j'1424+ # Correctly padded jump label should appear1425+ tmux.until { |lines| lines[-7] == '5 5' }1426+ tmux.until { |lines| lines[-8] == ' 6' }1427+ tmux.send_keys '5'1428+ lines = tmux.capture1429+ # Assert that specified pointer is displayed1430+ assert_equal "#{pointer} 5", lines[-7]1431+ end1432+1433+ def test_marker1434+ marker = '>>'1435+ tmux.send_keys "seq 10 | #{fzf "--multi --marker '#{marker}'"}", :Enter1436+ tmux.until { |lines| lines[-2] == ' 10/10' }1437+ tmux.send_keys :BTab1438+ lines = tmux.capture1439+ # Assert that specified marker is displayed1440+ assert_equal " #{marker}1", lines[-3]1441+ end1442+1443 def test_preview1444 tmux.send_keys %(seq 1000 | sed s/^2$// | #{FZF} -m --preview 'sleep 0.2; echo {{}-{+}}' --bind ?:toggle-preview), :Enter1445 tmux.until { |lines| lines[1].include?(' {1-1}') }1446 tmux.send_keys :Up640 unmodified lines