Add support for an alternative preview window layout
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #2804 Close #2844 Related #2277
authored by Junegunn Choi · committed by Junegunn Choi · parent 8df872a · Browse files · 4 files·+136 added−76 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+136 added−76 removed
- other:+136 added−76 removed
Provenance
- Commit
82b4672- Tree
59760f8- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
8df872a
4 files changed+136 added−76 removed
modified:man/man1/fzf.1+14 added-5 removed
19 unmodified lines20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN22 THE SOFTWARE.23 ..24-.TH fzf 1 "Apr 2022" "fzf 0.30.0" "fzf - a command-line fuzzy finder"24+.TH fzf 1 "Jul 2022" "fzf 0.31.0" "fzf - a command-line fuzzy finder"25 26 .SH NAME27 fzf - a command-line fuzzy finder28 440 unmodified lines469 sleep 0.01470 done'\fR471 .RE472 .TP473-.BI "--preview-window=" "[POSITION][,SIZE[%]][,border-BORDER_OPT][,[no]wrap][,[no]follow][,[no]cycle][,[no]hidden][,+SCROLL[OFFSETS][/DENOM]][,~HEADER_LINES][,default]"473+.BI "--preview-window=" "[POSITION][,SIZE[%]][,border-BORDER_OPT][,[no]wrap][,[no]follow][,[no]cycle][,[no]hidden][,+SCROLL[OFFSETS][/DENOM]][,~HEADER_LINES][,default][,<SIZE_THRESHOLD(ALTERNATIVE_LAYOUT)]"474 475 .RS476 .B POSITION: (default: right)477 \fBup9 unmodified lines487 * If size is given as 0, preview window will not be visible, but fzf will still488 execute the command in the background.489 490 * Long lines are truncated by default. Line wrap can be enabled with491-\fB:wrap\fR flag.491+\fBwrap\fR flag.492 493-* Preview window will automatically scroll to the bottom when \fB:follow\fR493+* Preview window will automatically scroll to the bottom when \fBfollow\fR494 flag is set, similarly to how \fBtail -f\fR works.495 496 .RS497 e.g.3 unmodified lines501 (( i % 300 == 0 )) && printf "\\033[2J"502 done'\fR503 .RE504 505-* Cyclic scrolling is enabled with \fB:cycle\fR flag.505+* Cyclic scrolling is enabled with \fBcycle\fR flag.506 507 * To change the style of the border of the preview window, specify one of508 the options for \fB--border\fR with \fBborder-\fR prefix.509 e.g. \fBborder-rounded\fR (border with rounded edges, default),41 unmodified lines551 # Display top 3 lines as the fixed header552 fzf --preview 'bat --style=full --color=always {}' --preview-window '~3'\fR553 .RE554 555+* You can specify an alternative set of options that are used only when the size556+ of the preview window is below a certain threshold. Note that only one557+ alternative layout is allowed.558+559+.RS560+e.g.561+ \fBfzf --preview 'cat {}' --preview-window 'right,border-left,<30(up,30%,border-bottom)'\fR562+.RE563+564 .SS Scripting565 .TP566 .BI "-q, --query=" "STR"567 Start the finder with the given query464 unmodified linesmodified:src/options.go+23 added-6 removed
87 unmodified lines88 [up|down|left|right][,SIZE[%]]89 [,[no]wrap][,[no]cycle][,[no]follow][,[no]hidden]90 [,border-BORDER_OPT]91 [,+SCROLL[OFFSETS][/DENOM]][,~HEADER_LINES]92- [,default]92+ [,default][,<SIZE_THRESHOLD(ALTERNATIVE_LAYOUT)]93 94 Scripting95 -q, --query=STR Start the finder with the given query96 -1, --select-1 Automatically select the only match77 unmodified lines174 cycle bool175 follow bool176 border tui.BorderShape177 headerLines int178+ threshold int179+ alternative *previewOpts180 }181 182 func (a previewOpts) sameLayout(b previewOpts) bool {181- return a.size == b.size && a.position == b.position && a.border == b.border && a.hidden == b.hidden183+ return a.size == b.size && a.position == b.position && a.border == b.border && a.hidden == b.hidden && a.threshold == b.threshold &&184+ (a.alternative != nil && b.alternative != nil && a.alternative.sameLayout(*b.alternative) ||185+ a.alternative == nil && b.alternative == nil)186 }187 188 func (a previewOpts) sameContentLayout(b previewOpts) bool {189 return a.wrap == b.wrap && a.headerLines == b.headerLines60 unmodified lines250 Version bool251 }252 253 func defaultPreviewOpts(command string) previewOpts {250- return previewOpts{command, posRight, sizeSpec{50, true}, "", false, false, false, false, tui.BorderRounded, 0}254+ return previewOpts{command, posRight, sizeSpec{50, true}, "", false, false, false, false, tui.BorderRounded, 0, 0, nil}255 }256 257 func defaultOptions() *Options {258 return &Options{913 unmodified lines1172 return infoDefault1173 }1174 1175 func parsePreviewWindow(opts *previewOpts, input string) {1172- delimRegex := regexp.MustCompile("[:,]") // : for backward compatibility1176+ tokenRegex := regexp.MustCompile(`[:,]*(<([1-9][0-9]*)\(([^)<]+)\)|[^,:]+)`)1177 sizeRegex := regexp.MustCompile("^[0-9]+%?$")1178 offsetRegex := regexp.MustCompile(`^(\+{-?[0-9]+})?([+-][0-9]+)*(-?/[1-9][0-9]*)?$`)1179 headerRegex := regexp.MustCompile("^~(0|[1-9][0-9]*)$")1176- tokens := delimRegex.Split(input, -1)1177- for _, token := range tokens {1180+ tokens := tokenRegex.FindAllStringSubmatch(input, -1)1181+ var alternative string1182+ for _, match := range tokens {1183+ if len(match[2]) > 0 {1184+ opts.threshold = atoi(match[2])1185+ alternative = match[3]1186+ continue1187+ }1188+ token := match[1]1189 switch token {1190 case "":1191 case "default":1192 *opts = defaultPreviewOpts(opts.command)50 unmodified lines1243 errorExit("invalid preview window option: " + token)1244 }1245 }1246 }1247+ if len(alternative) > 0 {1248+ alternativeOpts := *opts1249+ opts.alternative = &alternativeOpts1250+ opts.alternative.alternative = nil1251+ parsePreviewWindow(opts.alternative, alternative)1252+ }1253 }1254 1255 func parseMargin(opt string, margin string) [4]sizeSpec {1256 margins := strings.Split(margin, ",")519 unmodified linesmodified:src/terminal.go+89 added-65 removed
817 unmodified lines818 t.border.Close()819 }820 if t.window != nil {821 t.window.Close()822+ t.window = nil823 }824 if t.pborder != nil {825 t.pborder.Close()826+ t.pborder = nil827 }828 if t.pwindow != nil {829 t.pwindow.Close()830+ t.pwindow = nil831 }832 // Reset preview version so that full redraw occurs833 t.previewed.version = 0834 36 unmodified lines871 }872 width = screenWidth - marginInt[1] - marginInt[3]873 height = screenHeight - marginInt[0] - marginInt[2]874 875+ // Set up preview window876 noBorder := tui.MakeBorderStyle(tui.BorderNone, t.unicode)877 if previewVisible {874- createPreviewWindow := func(y int, x int, w int, h int) {875- pwidth := w876- pheight := h877- var previewBorder tui.BorderStyle878- if t.previewOpts.border == tui.BorderNone {879- previewBorder = tui.MakeTransparentBorder()880- } else {881- previewBorder = tui.MakeBorderStyle(t.previewOpts.border, t.unicode)878+ var resizePreviewWindows func(previewOpts previewOpts)879+ resizePreviewWindows = func(previewOpts previewOpts) {880+ if previewOpts.hidden {881+ return882 }883+ hasThreshold := previewOpts.threshold > 0 && previewOpts.alternative != nil884+ createPreviewWindow := func(y int, x int, w int, h int) {885+ pwidth := w886+ pheight := h887+ var previewBorder tui.BorderStyle888+ if previewOpts.border == tui.BorderNone {889+ previewBorder = tui.MakeTransparentBorder()890+ } else {891+ previewBorder = tui.MakeBorderStyle(previewOpts.border, t.unicode)892+ }883- t.pborder = t.tui.NewWindow(y, x, w, h, true, previewBorder)884- switch t.previewOpts.border {885- case tui.BorderSharp, tui.BorderRounded:886- pwidth -= 4887- pheight -= 2888- x += 2889- y += 1890- case tui.BorderLeft:891- pwidth -= 2892- x += 2893- case tui.BorderRight:894- pwidth -= 2895- case tui.BorderTop:896- pheight -= 1897- y += 1898- case tui.BorderBottom:899- pheight -= 1900- case tui.BorderHorizontal:901- pheight -= 2902- y += 1903- case tui.BorderVertical:904- pwidth -= 4905- x += 2893+ t.pborder = t.tui.NewWindow(y, x, w, h, true, previewBorder)894+ switch previewOpts.border {895+ case tui.BorderSharp, tui.BorderRounded:896+ pwidth -= 4897+ pheight -= 2898+ x += 2899+ y += 1900+ case tui.BorderLeft:901+ pwidth -= 2902+ x += 2903+ case tui.BorderRight:904+ pwidth -= 2905+ case tui.BorderTop:906+ pheight -= 1907+ y += 1908+ case tui.BorderBottom:909+ pheight -= 1910+ case tui.BorderHorizontal:911+ pheight -= 2912+ y += 1913+ case tui.BorderVertical:914+ pwidth -= 4915+ x += 2916+ }917+ t.pwindow = t.tui.NewWindow(y, x, pwidth, pheight, true, noBorder)918 }907- t.pwindow = t.tui.NewWindow(y, x, pwidth, pheight, true, noBorder)919+ verticalPad := 2920+ minPreviewHeight := 3921+ switch previewOpts.border {922+ case tui.BorderNone, tui.BorderVertical, tui.BorderLeft, tui.BorderRight:923+ verticalPad = 0924+ minPreviewHeight = 1925+ case tui.BorderTop, tui.BorderBottom:926+ verticalPad = 1927+ minPreviewHeight = 2928+ }929+ switch previewOpts.position {930+ case posUp, posDown:931+ pheight := calculateSize(height, previewOpts.size, minHeight, minPreviewHeight, verticalPad)932+ if hasThreshold && pheight < previewOpts.threshold {933+ resizePreviewWindows(*previewOpts.alternative)934+ return935+ }936+ if previewOpts.position == posUp {937+ t.window = t.tui.NewWindow(938+ marginInt[0]+pheight, marginInt[3], width, height-pheight, false, noBorder)939+ createPreviewWindow(marginInt[0], marginInt[3], width, pheight)940+ } else {941+ t.window = t.tui.NewWindow(942+ marginInt[0], marginInt[3], width, height-pheight, false, noBorder)943+ createPreviewWindow(marginInt[0]+height-pheight, marginInt[3], width, pheight)944+ }945+ case posLeft, posRight:946+ pwidth := calculateSize(width, previewOpts.size, minWidth, 5, 4)947+ if hasThreshold && pwidth < previewOpts.threshold {948+ fmt.Println("Alternative", (*previewOpts.alternative).position == posDown)949+ resizePreviewWindows(*previewOpts.alternative)950+ return951+ }952+ if previewOpts.position == posLeft {953+ t.window = t.tui.NewWindow(954+ marginInt[0], marginInt[3]+pwidth, width-pwidth, height, false, noBorder)955+ createPreviewWindow(marginInt[0], marginInt[3], pwidth, height)956+ } else {957+ t.window = t.tui.NewWindow(958+ marginInt[0], marginInt[3], width-pwidth, height, false, noBorder)959+ createPreviewWindow(marginInt[0], marginInt[3]+width-pwidth, pwidth, height)960+ }961+ }962 }909- verticalPad := 2910- minPreviewHeight := 3911- switch t.previewOpts.border {912- case tui.BorderNone, tui.BorderVertical, tui.BorderLeft, tui.BorderRight:913- verticalPad = 0914- minPreviewHeight = 1915- case tui.BorderTop, tui.BorderBottom:916- verticalPad = 1917- minPreviewHeight = 2918- }919- switch t.previewOpts.position {920- case posUp:921- pheight := calculateSize(height, t.previewOpts.size, minHeight, minPreviewHeight, verticalPad)922- t.window = t.tui.NewWindow(923- marginInt[0]+pheight, marginInt[3], width, height-pheight, false, noBorder)924- createPreviewWindow(marginInt[0], marginInt[3], width, pheight)925- case posDown:926- pheight := calculateSize(height, t.previewOpts.size, minHeight, minPreviewHeight, verticalPad)927- t.window = t.tui.NewWindow(928- marginInt[0], marginInt[3], width, height-pheight, false, noBorder)929- createPreviewWindow(marginInt[0]+height-pheight, marginInt[3], width, pheight)930- case posLeft:931- pwidth := calculateSize(width, t.previewOpts.size, minWidth, 5, 4)932- t.window = t.tui.NewWindow(933- marginInt[0], marginInt[3]+pwidth, width-pwidth, height, false, noBorder)934- createPreviewWindow(marginInt[0], marginInt[3], pwidth, height)935- case posRight:936- pwidth := calculateSize(width, t.previewOpts.size, minWidth, 5, 4)937- t.window = t.tui.NewWindow(938- marginInt[0], marginInt[3], width-pwidth, height, false, noBorder)939- createPreviewWindow(marginInt[0], marginInt[3]+width-pwidth, pwidth, height)940- }941- } else {963+ resizePreviewWindows(t.previewOpts)964+ }965+ if t.window == nil {966 t.window = t.tui.NewWindow(967 marginInt[0],968 marginInt[3],969 width,1968 unmodified linesmodified:CHANGELOG.md+10 added
1 unmodified line2 =========3 4 0.31.05 ------6+- Added support for an alternative preview window layout that is activated7+ when the size of the preview window is smaller than a certain threshold.8+ ```sh9+ # If the width of the preview window is smaller than 50 columns,10+ # it will be displayed above the search window.11+ fzf --preview 'cat {}' --preview-window 'right,50%,border-left,<50(up,30%,border-bottom)'12+13+ # Or you can just hide it like so14+ fzf --preview 'cat {}' --preview-window '<50(hidden)'15+ ```16 - Use SGR mouse mode to support larger terminals17 - Bug fixes and improvements18 19 0.30.01217 unmodified lines