Skip to content

Commit

Permalink
Choose 'path' scheme when input is a TTY device
Browse files Browse the repository at this point in the history
And there is no 'reload' action bound to 'start' event.
  • Loading branch information
junegunn committed Jan 23, 2025
1 parent 71eabef commit 01275d1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Table of Contents
* [Custom fuzzy completion](#custom-fuzzy-completion)
* [Vim plugin](#vim-plugin)
* [Advanced topics](#advanced-topics)
* [Customizing for different types of input](#customizing-for-different-types-of-input)
* [Performance](#performance)
* [Executing external programs](#executing-external-programs)
* [Turning into a different process](#turning-into-a-different-process)
Expand Down Expand Up @@ -718,6 +719,25 @@ See [README-VIM.md](README-VIM.md).
Advanced topics
---------------
### Customizing for different types of input
Since fzf is a general-purpose text filter, its algorithm was designed to
"generally" work well with any kind of input. However, admittedly, there is no
true one-size-fits-all solution, and you may want to tweak the algorithm and
some of the settings depending on the type of the input. To make this process
easier, fzf provides a set of "scheme"s for some common types of input.
| Scheme | Description |
| :--- | :--- |
| `--scheme=default` | Generic scheme designed to work well with any kind of input |
| `--scheme=path` | Suitable for file paths |
| `--scheme=history` | Suitable for command history or any input where chronological ordering is important |
(See `fzf --man` for the details)
fzf heuristically chooses which scheme to use, but it will not always be
perfect. If you see suboptimal behavior, try setting the scheme explicitly.
### Performance
fzf is fast. Performance should not be a problem in most use cases. However,
Expand All @@ -727,6 +747,8 @@ you might want to be aware of the options that can affect performance.
makes the initial scanning slower. So it's not recommended that you add it
to your `$FZF_DEFAULT_OPTS`.
- `--nth` makes fzf slower because it has to tokenize each line.
- A plain string `--delimiter` should be preferred over a regular expression
delimiter.
- `--with-nth` makes fzf slower as fzf has to tokenize and reassemble each
line.
Expand Down
13 changes: 10 additions & 3 deletions man/man1/fzf.1
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ more weight to the chronological ordering. This also sets
.RE
.RE

.RS
fzf chooses \fBpath\fR scheme when the input is a TTY device, where fzf would
start its built-in walker or run \fB$FZF_DEFAULT_COMMAND\fR, and there is no
\fBreload\fR action bound to \fBstart\fR event. Otherwise, it chooses
\fBdefault\fR scheme.
.RE

.TP
.BI "\-\-algo=" TYPE
Fuzzy matching algorithm (default: v2)
Expand Down Expand Up @@ -1125,9 +1132,9 @@ Show man page
.SH ENVIRONMENT VARIABLES
.TP
.B FZF_DEFAULT_COMMAND
Default command to use when input is tty. On *nix systems, fzf runs the command
with \fB$SHELL \-c\fR if \fBSHELL\fR is set, otherwise with \fBsh \-c\fR, so in
this case make sure that the command is POSIX-compliant.
Default command to use when input is a TTY device. On *nix systems, fzf runs
the command with \fB$SHELL \-c\fR if \fBSHELL\fR is set, otherwise with \fBsh
\-c\fR, so in this case make sure that the command is POSIX-compliant.
.TP
.B FZF_DEFAULT_OPTS
Default options.
Expand Down
20 changes: 19 additions & 1 deletion src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3225,7 +3225,14 @@ func ParseOptions(useDefaults bool, args []string) (*Options, error) {
if len(opts.Scheme) == 0 {
opts.Scheme = "default"
if len(opts.Criteria) == 0 {
if util.IsTty(os.Stdin) && len(os.Getenv("FZF_DEFAULT_COMMAND")) == 0 {
// NOTE: Let's assume $FZF_DEFAULT_COMMAND generates a list of file paths.
// But it is possible that $FZF_DEFAULT_COMMAND is set to a command that
// doesn't generate file paths.
//
// In that case, you can either
// 1. explicitly set --scheme=default,
// 2. or replace $FZF_DEFAULT_COMMAND with an equivalent start:reload binding.
if !opts.hasReloadOnStart() && util.IsTty(os.Stdin) {
opts.Scheme = "path"
}
_, opts.Criteria, _ = parseScheme(opts.Scheme)
Expand All @@ -3240,6 +3247,17 @@ func ParseOptions(useDefaults bool, args []string) (*Options, error) {
return opts, nil
}

func (opts *Options) hasReloadOnStart() bool {
if actions, prs := opts.Keymap[tui.Start.AsEvent()]; prs {
for _, action := range actions {
if action.t == actReload || action.t == actReloadSync {
return true
}
}
}
return false
}

func (opts *Options) extractReloadOnStart() string {
cmd := ""
if actions, prs := opts.Keymap[tui.Start.AsEvent()]; prs {
Expand Down

0 comments on commit 01275d1

Please sign in to comment.