Implement flag for preserving whitespace around field (#1242)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ZDNoFYVe · committed by Junegunn Choi · parent 9ff3381 · Browse files · 3 files·+75 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+75 added−17 removed
- other:+75 added−17 removed
Provenance
- Commit
43345fb- Tree
b4adc3a- Author
ZDNoFYVe <5420669+ZDNoFYVe@users.noreply.github.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
9ff3381
3 files changed+75 added−17 removed
modified:man/man1/fzf.1+3 added
283 unmodified lines284 285 e.g. \fBfzf --multi --preview='head -10 {+}'\fR286 \fBgit log --oneline | fzf --multi --preview 'git show {+1}'\fR287 288+When using a field index expression, leading and trailing whitespace is stripped289+from the replacement string. To preserve the whitespace, use the \fBs\fR flag.290+291 Also, \fB{q}\fR is replaced to the current query string.292 293 Note that you can escape a placeholder pattern by prepending a backslash.294 .RE317 unmodified linesmodified:src/terminal_test.go+29 added-2 removed
19 unmodified lines20 newItem("foo'bar \x1b[31mbaz\x1b[m"),21 newItem("foo'bar \x1b[31mbaz\x1b[m"),22 newItem("FOO'BAR \x1b[31mBAZ\x1b[m")}23 24+ delim := "'"25+ var regex *regexp.Regexp26+27 var result string28 check := func(expected string) {29 if result != expected {30 t.Errorf("expected: %s, actual: %s", expected, result)43 unmodified lines74 // forcePlus75 result = replacePlaceholder("echo {1}/{2}/{-1}/{-2}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, true, "query", items2)76 check("echo 'foo'\\''bar' 'FOO'\\''BAR'/'baz' 'BAZ'/'baz' 'BAZ'/'foo'\\''bar' 'FOO'\\''BAR'/'foo'\\''bar baz' 'FOO'\\''BAR BAZ'/{n.t}/{}/{1}/{q}/'' ''")77 78+ // Whitespace preserving flag with "'" delimiter79+ result = replacePlaceholder("echo {s1}", true, Delimiter{str: &delim}, false, "query", items1)80+ check("echo ' foo'")81+82+ result = replacePlaceholder("echo {s2}", true, Delimiter{str: &delim}, false, "query", items1)83+ check("echo 'bar baz'")84+85+ result = replacePlaceholder("echo {s}", true, Delimiter{str: &delim}, false, "query", items1)86+ check("echo ' foo'\\''bar baz'")87+88+ result = replacePlaceholder("echo {s..}", true, Delimiter{str: &delim}, false, "query", items1)89+ check("echo ' foo'\\''bar baz'")90+91+ // Whitespace preserving flag with regex delimiter92+ regex = regexp.MustCompile("\\w+")93+94+ result = replacePlaceholder("echo {s1}", true, Delimiter{regex: regex}, false, "query", items1)95+ check("echo ' '")96+97+ result = replacePlaceholder("echo {s2}", true, Delimiter{regex: regex}, false, "query", items1)98+ check("echo ''\\'''")99+100+ result = replacePlaceholder("echo {s3}", true, Delimiter{regex: regex}, false, "query", items1)101+ check("echo ' '")102+103 // No match104 result = replacePlaceholder("echo {}/{+}", true, Delimiter{}, false, "query", []*Item{nil, nil})105 check("echo /")106 1 unmodified line108 result = replacePlaceholder("echo {}/{+}", true, Delimiter{}, false, "query", []*Item{nil, item1})109 check("echo /' foo'\\''bar baz'")110 111 // String delimiter84- delim := "'"112 result = replacePlaceholder("echo {}/{1}/{2}", true, Delimiter{str: &delim}, false, "query", items1)113 check("echo ' foo'\\''bar baz'/'foo'/'bar baz'")114 115 // Regex delimiter89- regex := regexp.MustCompile("[oa]+")116+ regex = regexp.MustCompile("[oa]+")117 // foo'bar baz118 result = replacePlaceholder("echo {}/{1}/{3}/{2..3}", true, Delimiter{regex: regex}, false, "query", items1)119 check("echo ' foo'\\''bar baz'/'f'/'r b'/''\\''bar b'")120 }19 unmodified linesmodified:src/terminal.go+43 added-15 removed
22 unmodified lines23 24 var placeholder *regexp.Regexp25 26 func init() {27- placeholder = regexp.MustCompile("\\\\?(?:{\\+?[0-9,-.]*}|{q})")27+ placeholder = regexp.MustCompile("\\\\?(?:{[+s]*[0-9,-.]*}|{q})")28 }29 30 type jumpMode int31 188 unmodified lines220 actSigStop221 actTop222 )223 224+type placeholderFlags struct {225+ plus bool226+ preserveSpace bool227+}228+229 func toActions(types ...actionType) []action {230 actions := make([]action, len(types))231 for idx, t := range types {232 actions[idx] = action{t: t, a: ""}901 unmodified lines1134 }1135 return "'" + strings.Replace(entry, "'", "'\\''", -1) + "'"1136 }1137 1138+func parsePlaceholder(match string) (bool, string, placeholderFlags) {1139+ flags := placeholderFlags{}1140+1141+ if match[0] == '\\' {1142+ // Escaped placeholder pattern1143+ return true, match[1:], flags1144+ }1145+1146+ skipChars := 11147+ for _, char := range match[1:] {1148+ switch char {1149+ case '+':1150+ flags.plus = true1151+ skipChars++1152+ case 's':1153+ flags.preserveSpace = true1154+ skipChars++1155+ default:1156+ break1157+ }1158+ }1159+1160+ matchWithoutFlags := "{" + match[skipChars:]1161+1162+ return false, matchWithoutFlags, flags1163+}1164+1165 func hasPlusFlag(template string) bool {1166 for _, match := range placeholder.FindAllString(template, -1) {1135- if match[0] == '\\' {1136- continue1137- }1138- if match[1] == '+' {1167+ _, _, flags := parsePlaceholder(match)1168+ if flags.plus {1169 return true1170 }1171 }1172 return false8 unmodified lines1181 if selected[0] == nil {1182 selected = []*Item{}1183 }1184 return placeholder.ReplaceAllStringFunc(template, func(match string) string {1155- // Escaped pattern1156- if match[0] == '\\' {1157- return match[1:]1185+ escaped, match, flags := parsePlaceholder(match)1186+1187+ if escaped {1188+ return match1189 }1190 1191 // Current query1192 if match == "{q}" {1193 return quoteEntry(query)1194 }1195 1165- plusFlag := forcePlus1166- if match[1] == '+' {1167- match = "{" + match[2:]1168- plusFlag = true1169- }1196 items := current1171- if plusFlag {1197+ if flags.plus || forcePlus {1198 items = selected1199 }1200 1201 replacements := make([]string, len(items))27 unmodified lines1229 if len(delims) > 0 && delims[len(delims)-1][1] == len(str) {1230 str = str[:delims[len(delims)-1][0]]1231 }1232 }1233+ if !flags.preserveSpace {1207- str = strings.TrimSpace(str)1234+ str = strings.TrimSpace(str)1235+ }1236 replacements[idx] = quoteEntry(str)1237 }1238 return strings.Join(replacements, " ")1239 })679 unmodified lines