Skip to content

Commit

Permalink
Fix rendering of the prompt line when overflow occurs with `--info=in…
Browse files Browse the repository at this point in the history
…line`

Fix junegunn#2692
  • Loading branch information
junegunn committed Dec 22, 2021
1 parent 176ee69 commit cd23401
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CHANGELOG
```sh
fzf --preview 'cat {}' --bind 'ctrl-/:change-preview-window(right,70%|down,40%,border-horizontal|hidden|right)'
```
- Fixed rendering of the prompt line when overflow occurs with `--info=inline`

0.28.0
------
Expand Down
5 changes: 3 additions & 2 deletions src/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"io/ioutil"
"math"
"os"
"os/signal"
"regexp"
Expand Down Expand Up @@ -709,7 +710,7 @@ func (t *Terminal) sortSelected() []selectedItem {
}

func (t *Terminal) displayWidth(runes []rune) int {
width, _ := util.RunesWidth(runes, 0, t.tabstop, 0)
width, _ := util.RunesWidth(runes, 0, t.tabstop, math.MaxInt32)
return width
}

Expand Down Expand Up @@ -1197,7 +1198,7 @@ func (t *Terminal) printItem(result Result, line int, i int, current bool) {

func (t *Terminal) trimRight(runes []rune, width int) ([]rune, bool) {
// We start from the beginning to handle tab characters
width, overflowIdx := util.RunesWidth(runes, 0, t.tabstop, width)
_, overflowIdx := util.RunesWidth(runes, 0, t.tabstop, width)
if overflowIdx >= 0 {
return runes[:overflowIdx], true
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func RunesWidth(runes []rune, prefixWidth int, tabstop int, limit int) (int, int
w = runewidth.StringWidth(s) + strings.Count(s, "\n")
}
width += w
if limit > 0 && width > limit {
if width > limit {
return width, idx
}
idx += len(rs)
Expand Down
16 changes: 16 additions & 0 deletions src/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,19 @@ func TestOnce(t *testing.T) {
t.Error("Expected: false")
}
}

func TestRunesWidth(t *testing.T) {
for _, args := range [][]int{
{100, 5, -1},
{3, 4, 3},
{0, 1, 0},
} {
width, overflowIdx := RunesWidth([]rune("hello"), 0, 0, args[0])
if width != args[1] {
t.Errorf("Expected width: %d, actual: %d", args[1], width)
}
if overflowIdx != args[2] {
t.Errorf("Expected overflow index: %d, actual: %d", args[2], overflowIdx)
}
}
}

0 comments on commit cd23401

Please sign in to comment.