Skip to content
Myr

Enhance click-header event

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Expose the name of the mouse action as $FZF_KEY * Trigger click-header on mouse up * Enhanced clickable header for `kill` completion

authored by Junegunn Choi · committed by Junegunn Choi · parent 2c15cd7 · Browse files · 8 files+151 added−54 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+151 added−54 removed

  • other:+151 added−54 removed

8 files+151 added−54 removed

modified:shell/completion.bash+25 added-1 removed
407 unmodified lines
408 _fzf_proc_completion "$@"409}410411_fzf_proc_completion() {412 local transformer413 transformer='414 if [[ $FZF_KEY =~ ctrl|alt|shift ]] && [[ -n $FZF_NTH ]]; then415 nths=( $(tr , " " <<< "$FZF_NTH") )416 new_nths=()417 found=0418 for nth in ${nths[@]}; do419 if [[ $nth = $FZF_CLICK_HEADER_NTH ]]; then420 found=1421 else422 new_nths+=($nth)423 fi424 done425 [[ $found = 0 ]] && new_nths+=($FZF_CLICK_HEADER_NTH)426 new_nths=$(echo ${new_nths[@]} | tr " " ,)427 echo "change-nth($new_nths)+change-prompt($new_nths> )"428 else429 if [[ $FZF_NTH = $FZF_CLICK_HEADER_NTH ]]; then430 echo "change-nth()+change-prompt(> )"431 else432 echo "change-nth($FZF_CLICK_HEADER_NTH)+change-prompt($FZF_CLICK_HEADER_WORD> )"433 fi434 fi435 '436 _fzf_complete -m --header-lines=1 --no-preview --wrap --color fg:dim,nth:regular \413 --bind 'click-header:transform:echo "change-nth($FZF_CLICK_HEADER_NTH)+change-prompt($FZF_CLICK_HEADER_WORD> )"' -- "$@" < <(437 --bind "click-header:transform:$transformer" -- "$@" < <(438 command ps -eo user,pid,ppid,start,time,command 2> /dev/null ||439 command ps -eo user,pid,ppid,time,args 2> /dev/null || # For BusyBox440 command ps --everyone --full --windows # For cygwin441 )
183 unmodified lines
modified:shell/completion.zsh+25 added-1 removed
288 unmodified lines
289 )290}291292_fzf_complete_kill() {293 local transformer294 transformer='295 if [[ $FZF_KEY =~ ctrl|alt|shift ]] && [[ -n $FZF_NTH ]]; then296 nths=( $(tr , " " <<< "$FZF_NTH") )297 new_nths=()298 found=0299 for nth in ${nths[@]}; do300 if [[ $nth = $FZF_CLICK_HEADER_NTH ]]; then301 found=1302 else303 new_nths+=($nth)304 fi305 done306 [[ $found = 0 ]] && new_nths+=($FZF_CLICK_HEADER_NTH)307 new_nths=$(echo ${new_nths[@]} | tr " " ,)308 echo "change-nth($new_nths)+change-prompt($new_nths> )"309 else310 if [[ $FZF_NTH = $FZF_CLICK_HEADER_NTH ]]; then311 echo "change-nth()+change-prompt(> )"312 else313 echo "change-nth($FZF_CLICK_HEADER_NTH)+change-prompt($FZF_CLICK_HEADER_WORD> )"314 fi315 fi316 '317 _fzf_complete -m --header-lines=1 --no-preview --wrap --color fg:dim,nth:regular \294 --bind 'click-header:transform:echo "change-nth($FZF_CLICK_HEADER_NTH)+change-prompt($FZF_CLICK_HEADER_WORD> )"' -- "$@" < <(318 --bind "click-header:transform:$transformer" -- "$@" < <(319 command ps -eo user,pid,ppid,start,time,command 2> /dev/null ||320 command ps -eo user,pid,ppid,time,args 2> /dev/null || # For BusyBox321 command ps --everyone --full --windows # For cygwin322 )
92 unmodified lines
modified:src/tui/light.go+6 added-8 removed
624 unmodified lines
625 }626627 // middle := t & 0b1628 left := t&0b11 == 0629 ctrl := t&0b10000 > 0630 alt := t&0b01000 > 0631 shift := t&0b00100 > 0632 drag := t&0b100000 > 0 // 32633630 // shift := t & 0b100631 // ctrl := t & 0b1000632 mod := t&0b1100 > 0633634 drag := t&0b100000 > 0635634 if scroll != 0 {637 return Event{Mouse, 0, &MouseEvent{y, x, scroll, false, false, false, mod}}635 return Event{Mouse, 0, &MouseEvent{y, x, scroll, false, false, false, ctrl, alt, shift}}636 }637638 double := false639 if down && !drag {
15 unmodified lines
655 r.clicks = [][2]int{}656 }657 }658 }661 return Event{Mouse, 0, &MouseEvent{y, x, 0, left, down, double, mod}}659 return Event{Mouse, 0, &MouseEvent{y, x, 0, left, down, double, ctrl, alt, shift}}660}661662func (r *LightRenderer) smcup() {663 r.flush()
553 unmodified lines
modified:src/tui/tcell.go+14 added-6 removed
264 unmodified lines
265 // mouse up event consists of two events, 1. (main) event with modifier and other metadata, 2. event with zeroed buttons266 // so mouse click is three consecutive events, but the first and last are indistinguishable from movement events (with released buttons)267 // dragging has same structure, it only repeats the middle (main) event appropriately268 x, y := ev.Position()269 mod := ev.Modifiers() != 0269270 mod := ev.Modifiers()271 ctrl := (mod & tcell.ModCtrl) > 0272 alt := (mod & tcell.ModAlt) > 0273 shift := (mod & tcell.ModShift) > 0274275 // since we dont have mouse down events (unlike LightRenderer), we need to track state in prevButton276 prevButton, button := _prevMouseButton, ev.Buttons()277 _prevMouseButton = button278 drag := prevButton == button279280 switch {281 case button&tcell.WheelDown != 0:278 return Event{Mouse, 0, &MouseEvent{y, x, -1, false, false, false, mod}}282 return Event{Mouse, 0, &MouseEvent{y, x, -1, false, false, false, ctrl, alt, shift}}283 case button&tcell.WheelUp != 0:280 return Event{Mouse, 0, &MouseEvent{y, x, +1, false, false, false, mod}}284 return Event{Mouse, 0, &MouseEvent{y, x, +1, false, false, false, ctrl, alt, shift}}285 case button&tcell.Button1 != 0:286 double := false287 if !drag {288 // all potential double click events put their coordinates in the clicks array
14 unmodified lines
303 r.clicks = [][2]int{}304 }305 }306 // fire single or double click event303 return Event{Mouse, 0, &MouseEvent{y, x, 0, true, !double, double, mod}}307 return Event{Mouse, 0, &MouseEvent{y, x, 0, true, !double, double, ctrl, alt, shift}}308 case button&tcell.Button2 != 0:305 return Event{Mouse, 0, &MouseEvent{y, x, 0, false, true, false, mod}}309 return Event{Mouse, 0, &MouseEvent{y, x, 0, false, true, false, ctrl, alt, shift}}310 default:311 // double and single taps on Windows don't quite work due to312 // the console acting on the events and not allowing us313 // to consume them.314 left := button&tcell.Button1 != 0315 down := left || button&tcell.Button3 != 0316 double := false317318 // No need to report mouse movement events when no button is pressed319 if drag {320 return Event{Invalid, 0, nil}321 }314 return Event{Mouse, 0, &MouseEvent{y, x, 0, left, down, double, mod}}322 return Event{Mouse, 0, &MouseEvent{y, x, 0, left, down, double, ctrl, alt, shift}}323 }324325 // process keyboard:326 case *tcell.EventKey:
555 unmodified lines
modified:src/tui/tui.go+35 added-1 removed
148 unmodified lines
149 return Event{e.Type, e.Char, nil}150}151152func (e Event) KeyName() string {153 if me := e.MouseEvent; me != nil {154 return me.Name()155 }156157 if e.Type >= Invalid {158 return ""159 }160
209 unmodified lines
370 S int371 Left bool372 Down bool373 Double bool370 Mod bool374 Ctrl bool375 Alt bool376 Shift bool377}378379func (e MouseEvent) Mod() bool {380 return e.Ctrl || e.Alt || e.Shift381}382383func (e MouseEvent) Name() string {384 name := ""385 if e.Down {386 return name387 }388389 if e.Ctrl {390 name += "ctrl-"391 }392 if e.Alt {393 name += "alt-"394 }395 if e.Shift {396 name += "shift-"397 }398 if e.Double {399 name += "double-"400 }401 if !e.Left {402 name += "right-"403 }404 return name + "click"405}406407type BorderShape int408409const (410 BorderUndefined BorderShape = iota
660 unmodified lines
modified:src/terminal.go+39 added-36 removed
4618 unmodified lines
4619 barDragging := false4620 pbarDragging := false4621 pborderDragging := -14622 wasDown := false4623 pmx, pmy := -1, -14624 needBarrier := true46254626 // If an action is bound to 'start', we're going to process it before reading4627 // user input.
794 unmodified lines
5422 }5423 case actMouse:5424 me := event.MouseEvent5425 mx, my := me.X, me.Y5425 clicked := !wasDown && me.Down5426 click := !wasDown && me.Down5427 clicked := wasDown && !me.Down && (mx == pmx && my == pmy)5428 wasDown = me.Down5429 if click {5430 pmx, pmy = mx, my5431 }5432 if !me.Down {5433 barDragging = false5434 pbarDragging = false5435 pborderDragging = -15436 previewDraggingPos = -15437 pmx, pmy = -1, -15438 }54395440 // Scrolling5441 if me.S != 0 {5442 if t.window.Enclose(my, mx) && t.merger.Length() > 0 {5443 evt := tui.ScrollUp5438 if me.Mod {5444 if me.Mod() {5445 evt = tui.SScrollUp5446 }5447 if me.S < 0 {5448 evt = tui.ScrollDown5443 if me.Mod {5449 if me.Mod() {5450 evt = tui.SScrollDown5451 }5452 }5453 return doActions(actionsFor(evt))
7 unmodified lines
5461 break5462 }54635464 // Preview dragging5459 if me.Down && (previewDraggingPos >= 0 || clicked && t.hasPreviewWindow() && t.pwindow.Enclose(my, mx)) {5465 if me.Down && (previewDraggingPos >= 0 || click && t.hasPreviewWindow() && t.pwindow.Enclose(my, mx)) {5466 if previewDraggingPos > 0 {5467 scrollPreviewBy(previewDraggingPos - my)5468 }5469 previewDraggingPos = my
1 unmodified line
5471 }54725473 // Preview scrollbar dragging5474 headerLines := t.activePreviewOpts.headerLines5469 pbarDragging = me.Down && (pbarDragging || clicked && t.hasPreviewWindow() && my >= t.pwindow.Top()+headerLines && my < t.pwindow.Top()+t.pwindow.Height() && mx == t.pwindow.Left()+t.pwindow.Width())5475 pbarDragging = me.Down && (pbarDragging || click && t.hasPreviewWindow() && my >= t.pwindow.Top()+headerLines && my < t.pwindow.Top()+t.pwindow.Height() && mx == t.pwindow.Left()+t.pwindow.Width())5476 if pbarDragging {5477 effectiveHeight := t.pwindow.Height() - headerLines5478 numLines := len(t.previewer.lines) - headerLines5479 barLength, _ := getScrollbar(1, numLines, effectiveHeight, util.Min(numLines-effectiveHeight, t.previewer.offset-headerLines))
8 unmodified lines
5488 break5489 }54905491 // Preview border dragging (resizing)5486 if pborderDragging < 0 && clicked && t.hasPreviewWindow() {5492 if pborderDragging < 0 && click && t.hasPreviewWindow() {5493 switch t.activePreviewOpts.position {5494 case posUp:5495 if t.pborder.Enclose(my, mx) && my == t.pborder.Top()+t.pborder.Height()-1 {5496 pborderDragging = 0
72 unmodified lines
5569 break5570 }55715572 // Inside the header window5567 // TODO: Should we trigger this on mouse up instead?5568 // Should we still trigger it when the position has changed from the down event?5569 if t.headerVisible && t.headerWindow != nil && t.headerWindow.Enclose(my, mx) {5573 if clicked && t.headerVisible && t.headerWindow != nil && t.headerWindow.Enclose(my, mx) {5574 mx -= t.headerWindow.Left() + t.headerIndent(t.headerBorderShape)5575 my -= t.headerWindow.Top()5576 if mx < 0 {5577 break
5 unmodified lines
5583 t.clickHeaderColumn = mx + 15584 return doActions(actionsFor(tui.ClickHeader))5585 }55865583 if t.headerVisible && t.headerLinesWindow != nil && t.headerLinesWindow.Enclose(my, mx) {5587 if clicked && t.headerVisible && t.headerLinesWindow != nil && t.headerLinesWindow.Enclose(my, mx) {5588 mx -= t.headerLinesWindow.Left() + t.headerIndent(t.headerLinesShape)5589 my -= t.headerLinesWindow.Top()5590 if mx < 0 {5591 break
27 unmodified lines
5619 }5620 }56215622 // Scrollbar dragging5619 barDragging = me.Down && (barDragging || clicked && my >= min && mx == t.window.Width()-1)5623 barDragging = me.Down && (barDragging || click && my >= min && mx == t.window.Width()-1)5624 if barDragging {5625 barLength, barStart := t.getScrollbar()5626 if barLength > 0 {5627 maxItems := t.maxItems()
24 unmodified lines
5652 if t.vset(cy) && t.cy < t.merger.Length() {5653 return doActions(actionsFor(tui.DoubleClick))5654 }5655 }5652 break5656 }56575658 if me.Down {5659 mxCons := util.Constrain(mx-t.promptLen, 0, len(t.input))
3 unmodified lines
5663 } else if my >= min {5664 t.vset(cy)5665 req(reqList)5666 evt := tui.RightClick5664 if me.Mod {5667 if me.Mod() {5668 evt = tui.SRightClick5669 }5670 if me.Left {5671 evt = tui.LeftClick5669 if me.Mod {5672 if me.Mod() {5673 evt = tui.SLeftClick5674 }5675 }5676 return doActions(actionsFor(evt))5674 } else if t.headerVisible && t.headerWindow == nil {5675 // Header5676 // TODO: Should we trigger this on mouse up instead?5677 numLines := t.visibleHeaderLinesInList()5678 lineOffset := 05679 if t.inputWindow == nil && !t.headerFirst {5680 // offset for info line5681 if t.noSeparatorLine() {5682 lineOffset = 15683 } else {5684 lineOffset = 25685 }5677 }5678 }5679 if clicked && t.headerVisible && t.headerWindow == nil {5680 // Header5681 numLines := t.visibleHeaderLinesInList()5682 lineOffset := 05683 if t.inputWindow == nil && !t.headerFirst {5684 // offset for info line5685 if t.noSeparatorLine() {5686 lineOffset = 15687 } else {5688 lineOffset = 25689 }5687 my -= lineOffset5688 mx -= t.pointerLen + t.markerLen5689 if my >= 0 && my < numLines && mx >= 0 {5690 if t.layout == layoutReverse {5691 t.clickHeaderLine = my + 15692 } else {5693 t.clickHeaderLine = numLines - my5694 }5695 t.clickHeaderColumn = mx + 15696 return doActions(actionsFor(tui.ClickHeader))5690 }5691 my -= lineOffset5692 mx -= t.pointerLen + t.markerLen5693 if my >= 0 && my < numLines && mx >= 0 {5694 if t.layout == layoutReverse {5695 t.clickHeaderLine = my + 15696 } else {5697 t.clickHeaderLine = numLines - my5698 }5699 t.clickHeaderColumn = mx + 15700 return doActions(actionsFor(tui.ClickHeader))5701 }5702 }5703 case actReload, actReloadSync:5704 t.failed = nil
394 unmodified lines
modified:ADVANCED.md+4 added-1 removed
527 unmodified lines
528529 # Otherwise, if the query does not end with a space,530 # restart ripgrep and reload the list531 elif ! [[ $FZF_QUERY =~ \ $ ]]; then532 pat=${words[0]}532 echo "reload:sleep 0.1; $RG_PREFIX \"${words[0]}\" || true"533 echo "reload:sleep 0.1; $RG_PREFIX \"$pat\" || true"534 else535 echo search:536 fi537'538fzf --ansi --disabled --query "$INITIAL_QUERY" \539 --with-shell 'bash -c' \
183 unmodified lines
modified:CHANGELOG.md+3 added
24 unmodified lines
25 )+transform-prompt(26 echo "$FZF_CLICK_HEADER_WORD> "27 )'28 ```29 - `kill` completion for bash and zsh were updated to use this feature30- Added `search(...)` and `transform-search(...)` action to trigger an fzf search with an arbitrary query string. This can be used to extend the search syntax of fzf. In the following example, fzf will use the first word of the query to trigger ripgrep search, and use the rest of the query to perform fzf search within the result.31 ```sh32 TRANSFORMER='33 words=($FZF_QUERY)
6 unmodified lines
40 # Otherwise, if the query does not end with a space,41 # restart ripgrep and reload the list42 elif ! [[ $FZF_QUERY =~ \ $ ]]; then43 echo "reload:rg --column --color=always --smart-case \"${words[0]}\""44 else45 echo search:46 fi47 '48 fzf --ansi --disabled \49 --with-shell 'bash -c' \
2442 unmodified lines