Skip to content

Commit

Permalink
fix(#29): make --plain disable strict path matching
Browse files Browse the repository at this point in the history
Also updates docs and shell completions.

Closes #29
  • Loading branch information
natecraddock committed Mar 10, 2023
1 parent 734af10 commit c346c15
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion complete/_zf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ _zf() {
"(-f --filter)"{-f,--filter}"[Skip interactive use and filter using the given query]:string:"
"(-k --keep-order)"{-k,--keep-order}"[Don't sort by rank and preserve order of lines read on stdin]"
"(-l --lines)"{-l,--lines}"[Set the maximum number of result lines to show (default 10)]:int:"
"(-p --plain)"{-l,--lines}"[Disable filename match prioritization]"
"(-p --plain)"{-l,--lines}"[Treat input as plaintext and disable filepath matching features]"
)
_arguments $args[@]
}
Expand Down
2 changes: 1 addition & 1 deletion complete/zf.fish
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ complete -c zf -f
complete -f -c zf -s f -l filter -d "Skip interactive use and filter using the given query"
complete -f -c zf -s k -l keep-order -d "Don't sort by rank and preserve order of lines read on stdin"
complete -x -c zf -s l -l lines -d "Set the maximum number of result lines to show (default 10)"
complete -f -c zf -s p -l plain -d "Disable filename match prioritization"
complete -f -c zf -s p -l plain -d "Treat input as plaintext and disable filepath matching features"
complete -f -c zf -s v -l version -d "Show version information and exit"
complete -f -c zf -s h -l help -d "Display this help and exit"
4 changes: 2 additions & 2 deletions doc/zf.1
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ greater. Set to a large number (like 1000) to display zf full screen.

.TP
.BR \-p ", " \-\-plain
Disable filename match prioritization. Useful when the input lines are not
filenames.
Treat input as plaintext and disable filepath matching features. Useful when the input lines are not
file paths.

.TP
.BR \-v ", " \-\-version
Expand Down
2 changes: 1 addition & 1 deletion doc/zf.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Matching is case insensitive unless an uppercase letter is found in the query.

`-l, --lines`: Set the maximum number of result lines to show. Defaults to 10. Will be clamped to the number of candidates or the height of the terminal, whichever is greater. Set to a large number (like 1000) to display zf full screen.

`-p, --plain`: Disable filename match prioritization. Useful when the input lines are not filenames.
`-p, --plain`: Treat input as plaintext and disable filepath matching features. Useful when the input lines are not file paths.

`-v, --version`: Show version information and exit

Expand Down
2 changes: 1 addition & 1 deletion src/filter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub fn rankCandidate(
// each tokens rank is summed. if any token does not match the candidate is ignored
var rank: f64 = 0;
for (query_tokens) |token| {
const strict_path = hasSeparator(token);
const strict_path = !plain and hasSeparator(token);
if (rankToken(candidate, filename, token, case_sensitive, strict_path)) |r| {
rank += r;
} else return null;
Expand Down
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const help =
\\-f, --filter Skip interactive use and filter using the given query
\\-k, --keep-order Don't sort by rank and preserve order of lines read on stdin
\\-l, --lines Set the maximum number of result lines to show (default 10)
\\-p, --plain Disable filename match prioritization
\\-p, --plain Treat input as plaintext and disable filepath matching features
\\-v, --version Show version information and exit
\\-h, --help Display this help and exit
;
Expand Down
5 changes: 3 additions & 2 deletions src/ui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ fn calculateHighlights(
filenameOrNull: ?[]const u8,
tokens: [][]const u8,
case_sensitive: bool,
plain: bool,
matches: []usize,
) []usize {
var index: usize = 0;
for (tokens) |token| {
const strict_path = filter.hasSeparator(token);
const strict_path = !plain and filter.hasSeparator(token);
const matched = filter.highlightToken(str, filenameOrNull, token, case_sensitive, strict_path, matches[index..]);
index += matched.len;
}
Expand Down Expand Up @@ -98,7 +99,7 @@ inline fn drawCandidate(

var matches_buf: [2048]usize = undefined;
const filename = if (plain) null else std.fs.path.basename(candidate.str);
const matches = calculateHighlights(candidate.str, filename, tokens, case_sensitive, &matches_buf);
const matches = calculateHighlights(candidate.str, filename, tokens, case_sensitive, plain, &matches_buf);

const str_width = dw.strWidth(candidate.str, .half) catch unreachable;
const str = graphemeWidthSlice(candidate.str, @min(width - @as(usize, if (selected) 2 else 0), str_width));
Expand Down

0 comments on commit c346c15

Please sign in to comment.