Skip to content
Myr

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

3 files+75 added−17 removed

modified:man/man1/fzf.1+3 added
283 unmodified lines
284285e.g. \fBfzf --multi --preview='head -10 {+}'\fR286 \fBgit log --oneline | fzf --multi --preview 'git show {+1}'\fR287288When using a field index expression, leading and trailing whitespace is stripped289from the replacement string. To preserve the whitespace, use the \fBs\fR flag.290291Also, \fB{q}\fR is replaced to the current query string.292293Note that you can escape a placeholder pattern by prepending a backslash.294.RE
317 unmodified lines
modified:src/terminal_test.go+29 added-2 removed
19 unmodified lines
20 newItem("foo'bar \x1b[31mbaz\x1b[m"),21 newItem("foo'bar \x1b[31mbaz\x1b[m"),22 newItem("FOO'BAR \x1b[31mBAZ\x1b[m")}2324 delim := "'"25 var regex *regexp.Regexp2627 var result string28 check := func(expected string) {29 if result != expected {30 t.Errorf("expected: %s, actual: %s", expected, result)
43 unmodified lines
74 // 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}/'' ''")7778 // Whitespace preserving flag with "'" delimiter79 result = replacePlaceholder("echo {s1}", true, Delimiter{str: &delim}, false, "query", items1)80 check("echo ' foo'")8182 result = replacePlaceholder("echo {s2}", true, Delimiter{str: &delim}, false, "query", items1)83 check("echo 'bar baz'")8485 result = replacePlaceholder("echo {s}", true, Delimiter{str: &delim}, false, "query", items1)86 check("echo ' foo'\\''bar baz'")8788 result = replacePlaceholder("echo {s..}", true, Delimiter{str: &delim}, false, "query", items1)89 check("echo ' foo'\\''bar baz'")9091 // Whitespace preserving flag with regex delimiter92 regex = regexp.MustCompile("\\w+")9394 result = replacePlaceholder("echo {s1}", true, Delimiter{regex: regex}, false, "query", items1)95 check("echo ' '")9697 result = replacePlaceholder("echo {s2}", true, Delimiter{regex: regex}, false, "query", items1)98 check("echo ''\\'''")99100 result = replacePlaceholder("echo {s3}", true, Delimiter{regex: regex}, false, "query", items1)101 check("echo ' '")102103 // No match104 result = replacePlaceholder("echo {}/{+}", true, Delimiter{}, false, "query", []*Item{nil, nil})105 check("echo /")106
1 unmodified line
108 result = replacePlaceholder("echo {}/{+}", true, Delimiter{}, false, "query", []*Item{nil, item1})109 check("echo /' foo'\\''bar baz'")110111 // 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'")114115 // 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 lines
modified:src/terminal.go+43 added-15 removed
22 unmodified lines
2324var placeholder *regexp.Regexp2526func init() {27 placeholder = regexp.MustCompile("\\\\?(?:{\\+?[0-9,-.]*}|{q})")27 placeholder = regexp.MustCompile("\\\\?(?:{[+s]*[0-9,-.]*}|{q})")28}2930type jumpMode int31
188 unmodified lines
220 actSigStop221 actTop222)223224type placeholderFlags struct {225 plus bool226 preserveSpace bool227}228229func 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 lines
1134 }1135 return "'" + strings.Replace(entry, "'", "'\\''", -1) + "'"1136}11371138func parsePlaceholder(match string) (bool, string, placeholderFlags) {1139 flags := placeholderFlags{}11401141 if match[0] == '\\' {1142 // Escaped placeholder pattern1143 return true, match[1:], flags1144 }11451146 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 }11591160 matchWithoutFlags := "{" + match[skipChars:]11611162 return false, matchWithoutFlags, flags1163}11641165func 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 false
8 unmodified lines
1181 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)11861187 if escaped {1188 return match1189 }11901191 // Current query1192 if match == "{q}" {1193 return quoteEntry(query)1194 }11951165 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 }12001201 replacements := make([]string, len(items))
27 unmodified lines
1229 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