Add --ellipsis=.. option
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #2432 Also see - #1769 - https://github.com/junegunn/fzf/pull/1844#issuecomment-586663660
authored by Junegunn Choi · committed by Junegunn Choi · parent b88eb72 · Browse files · 8 files·+97 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+97 added−22 removed
- tests:+6 added−0 removed
- other:+91 added−22 removed
Provenance
- Commit
ef67a45- Tree
f7339a0- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
b88eb72
8 files changed+97 added−22 removed
modified:man/man1/fzf-tmux.1+1 added-1 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-tmux 1 "Dec 2021" "fzf 0.29.0" "fzf-tmux - open fzf in tmux split pane"24+.TH fzf-tmux 1 "Mar 2022" "fzf 0.30.0" "fzf-tmux - open fzf in tmux split pane"25 26 .SH NAME27 fzf-tmux - open fzf in tmux split pane28 40 unmodified linesmodified:man/man1/fzf.1+4 added-1 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 "Dec 2021" "fzf 0.29.0" "fzf - a command-line fuzzy finder"24+.TH fzf 1 "Mar 2022" "fzf 0.30.0" "fzf - a command-line fuzzy finder"25 26 .SH NAME27 fzf - a command-line fuzzy finder28 272 unmodified lines301 lines that follow.302 .TP303 .B "--header-first"304 Print header before the prompt line305+.TP306+.BI "--ellipsis=" "STR"307+Ellipsis to show when line is truncated (default: '..')308 .SS Display309 .TP310 .B "--ansi"311 Enable processing of ANSI color codes710 unmodified linesmodified:src/util/util_test.go+10 added
52 unmodified lines53 t.Errorf("Expected overflow index: %d, actual: %d", args[2], overflowIdx)54 }55 }56 }57+58+func TestTruncate(t *testing.T) {59+ truncated, width := Truncate("가나다라마", 7)60+ if string(truncated) != "가나다" {61+ t.Errorf("Expected: 가나다, actual: %s", string(truncated))62+ }63+ if width != 6 {64+ t.Errorf("Expected: 6, actual: %d", width)65+ }66+}modified:src/util/util.go+17 added
32 unmodified lines33 }34 return width, -135 }36 37+// Truncate returns the truncated runes and its width38+func Truncate(input string, limit int) ([]rune, int) {39+ runes := []rune{}40+ width := 041+ gr := uniseg.NewGraphemes(input)42+ for gr.Next() {43+ rs := gr.Runes()44+ w := runewidth.StringWidth(string(rs))45+ if width+w > limit {46+ return runes, width47+ }48+ width += w49+ runes = append(runes, rs...)50+ }51+ return runes, width52+}53+54 // Max returns the largest integer55 func Max(first int, second int) int {56 if first >= second {57 return first98 unmodified linesmodified:src/options.go+18 added
68 unmodified lines69 --marker=STR Multi-select marker (default: '>')70 --header=STR String to print as header71 --header-lines=N The first N lines of the input are treated as header72 --header-first Print header before the prompt line73+ --ellipsis=STR Ellipsis to show when line is truncated (default: '..')74 75 Display76 --ansi Enable processing of ANSI color codes77 --tabstop=SPACES Number of spaces for a tab character (default: 8)157 unmodified lines235 History *History236 Header []string237 HeaderLines int238 HeaderFirst bool239+ Ellipsis string240 Margin [4]sizeSpec241 Padding [4]sizeSpec242 BorderShape tui.BorderShape243 Unicode bool55 unmodified lines299 History: nil,300 Header: make([]string, 0),301 HeaderLines: 0,302 HeaderFirst: false,303+ Ellipsis: "..",304 Margin: defaultMargin(),305 Padding: defaultMargin(),306 Unicode: true,307 Tabstop: 8,974 unmodified lines1282 }1283 validateJumpLabels := false1284 validatePointer := false1285 validateMarker := false1286+ validateEllipsis := false1287 for i := 0; i < len(allArgs); i++ {1288 arg := allArgs[i]1289 switch arg {1290 case "-h", "--help":177 unmodified lines1468 case "--header-first":1469 opts.HeaderFirst = true1470 case "--no-header-first":1471 opts.HeaderFirst = false1472+ case "--ellipsis":1473+ opts.Ellipsis = nextString(allArgs, &i, "ellipsis string required")1474+ validateEllipsis = true1475 case "--preview":1476 opts.Preview.command = nextString(allArgs, &i, "preview command required")1477 case "--no-preview":1478 opts.Preview.command = ""89 unmodified lines1568 } else if match, value := optString(arg, "--header="); match {1569 opts.Header = strLines(value)1570 } else if match, value := optString(arg, "--header-lines="); match {1571 opts.HeaderLines = atoi(value)1572+ } else if match, value := optString(arg, "--ellipsis="); match {1573+ opts.Ellipsis = value1574+ validateEllipsis = true1575 } else if match, value := optString(arg, "--preview="); match {1576 opts.Preview.command = value1577 } else if match, value := optString(arg, "--preview-window="); match {1578 parsePreviewWindow(&opts.Preview, value)54 unmodified lines1633 if err := validateSign(opts.Marker, "marker"); err != nil {1634 errorExit(err.Error())1635 }1636 }1637+1638+ if validateEllipsis {1639+ for _, r := range opts.Ellipsis {1640+ if !unicode.IsGraphic(r) {1641+ errorExit("invalid character in ellipsis")1642+ }1643+ }1644+ }1645 }1646 1647 func validateSign(sign string, signOptName string) error {1648 if sign == "" {104 unmodified linesmodified:src/terminal.go+28 added-20 removed
48 unmodified lines49 var offsetComponentRegex *regexp.Regexp50 var offsetTrimCharsRegex *regexp.Regexp51 var activeTempFiles []string52 53-const ellipsis string = ".."53 const clearCode string = "\x1b[2J"54 55 func init() {56 placeholder = regexp.MustCompile(`\\?(?:{[+sf]*[0-9,-.]*}|{q}|{\+?f?nf?})`)86 unmodified lines143 headerFirst bool144 headerLines int145 header []string146 header0 []string147+ ellipsis string148 ansi bool149 tabstop int150 margin [4]sizeSpec151 padding [4]sizeSpec388 unmodified lines540 headerFirst: opts.HeaderFirst,541 headerLines: opts.HeaderLines,542 header: header,543 header0: header,544+ ellipsis: opts.Ellipsis,545 ansi: opts.Ansi,546 tabstop: opts.Tabstop,547 reading: true,548 running: true,712 unmodified lines1261 }1262 1263 offsets := result.colorOffsets(charOffsets, t.theme, colBase, colMatch, current)1264 maxWidth := t.window.Width() - (t.pointerLen + t.markerLen + 1)1265+ ellipsis, ellipsisWidth := util.Truncate(t.ellipsis, maxWidth/2)1264- maxe = util.Constrain(maxe+util.Min(maxWidth/2-2, t.hscrollOff), 0, len(text))1266+ maxe = util.Constrain(maxe+util.Min(maxWidth/2-ellipsisWidth, t.hscrollOff), 0, len(text))1267 displayWidth := t.displayWidthWithLimit(text, 0, maxWidth)1268 if displayWidth > maxWidth {1267- transformOffsets := func(diff int32) {1269+ transformOffsets := func(diff int32, rightTrim bool) {1270 for idx, offset := range offsets {1271 b, e := offset.offset[0], offset.offset[1]1272+ el := int32(len(ellipsis))1270- b += 2 - diff1271- e += 2 - diff1272- b = util.Max32(b, 2)1273+ b += el - diff1274+ e += el - diff1275+ b = util.Max32(b, el)1276+ if rightTrim {1277+ e = util.Min32(e, int32(maxWidth-ellipsisWidth))1278+ }1279 offsets[idx].offset[0] = b1280 offsets[idx].offset[1] = util.Max32(b, e)1281 }1282 }1283 if t.hscroll {1284 if t.keepRight && pos == nil {1279- trimmed, diff := t.trimLeft(text, maxWidth-2)1280- transformOffsets(diff)1281- text = append([]rune(ellipsis), trimmed...)1282- } else if !t.overflow(text[:maxe], maxWidth-2) {1285+ trimmed, diff := t.trimLeft(text, maxWidth-ellipsisWidth)1286+ transformOffsets(diff, false)1287+ text = append(ellipsis, trimmed...)1288+ } else if !t.overflow(text[:maxe], maxWidth-ellipsisWidth) {1289 // Stri..1284- text, _ = t.trimRight(text, maxWidth-2)1285- text = append(text, []rune(ellipsis)...)1290+ text, _ = t.trimRight(text, maxWidth-ellipsisWidth)1291+ text = append(text, ellipsis...)1292 } else {1293 // Stri..1294+ rightTrim := false1288- if t.overflow(text[maxe:], 2) {1289- text = append(text[:maxe], []rune(ellipsis)...)1295+ if t.overflow(text[maxe:], ellipsisWidth) {1296+ text = append(text[:maxe], ellipsis...)1297+ rightTrim = true1298 }1299 // ..ri..1300 var diff int321293- text, diff = t.trimLeft(text, maxWidth-2)1301+ text, diff = t.trimLeft(text, maxWidth-ellipsisWidth)1302 1303 // Transform offsets1296- transformOffsets(diff)1297- text = append([]rune(ellipsis), text...)1304+ transformOffsets(diff, rightTrim)1305+ text = append(ellipsis, text...)1306 }1307 } else {1300- text, _ = t.trimRight(text, maxWidth-2)1301- text = append(text, []rune(ellipsis)...)1308+ text, _ = t.trimRight(text, maxWidth-ellipsisWidth)1309+ text = append(text, ellipsis...)1310 1311 for idx, offset := range offsets {1304- offsets[idx].offset[0] = util.Min32(offset.offset[0], int32(maxWidth-2))1312+ offsets[idx].offset[0] = util.Min32(offset.offset[0], int32(maxWidth-len(ellipsis)))1313 offsets[idx].offset[1] = util.Min32(offset.offset[1], int32(maxWidth))1314 }1315 }1316 displayWidth = t.displayWidthWithLimit(text, 0, displayWidth)1582 unmodified linesmodified:test/test_go.rb+6 added
2206 unmodified lines2207 tmux.until { |lines| refute_includes lines[0], 'hello' }2208 tmux.send_keys 'a'2209 end2210 end2211+2212+ def test_ellipsis2213+ tmux.send_keys 'seq 1000 | tr "\n" , | fzf --ellipsis=SNIPSNIP -e -q500', :Enter2214+ tmux.until { |lines| assert_equal 1, lines.match_count }2215+ tmux.until { |lines| assert_match(/^> SNIPSNIP.*SNIPSNIP$/, lines[-3]) }2216+ end2217 end2218 2219 module TestShell2220 def setup478 unmodified linesmodified:CHANGELOG.md+13 added
1 CHANGELOG2 =========3 4+0.30.05+------6+- Added `--ellipsis` option. You can take advantage of it to make fzf7+ effectively search non-visible parts of the item.8+ ```sh9+ # Search against hidden line numbers on the far right10+ nl /usr/share/dict/words |11+ awk '{printf "%s%1000s\n", $2, $1}' |12+ fzf --nth=-1 --no-hscroll --ellipsis='' |13+ awk '{print $2}'14+ ```15+- Increased TTY buffer limit (#2748)16+17 0.29.018 ------19 - Added `change-preview(...)` action to change the `--preview` command20 - cf. `preview(...)` is a one-off action that doesn't change the default1199 unmodified lines