Skip to content

Commit 19ef889

Browse files
committed
Print --wrap-sign in preview window
Close #4233
1 parent bfea9e5 commit 19ef889

File tree

5 files changed

+66
-34
lines changed

5 files changed

+66
-34
lines changed

src/terminal.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,7 @@ func (t *Terminal) resizeWindows(forcePreview bool, redrawBorder bool) {
19361936
pwidth -= 1
19371937
}
19381938
t.pwindow = t.tui.NewWindow(y, x, pwidth, pheight, tui.WindowPreview, noBorder, true)
1939+
t.pwindow.SetWrapSign(t.wrapSign, t.wrapSignWidth)
19391940
if !hadPreviewWindow {
19401941
t.pwindow.Erase()
19411942
}

src/tui/light.go

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ func (r *LightRenderer) stderr(str string) {
4444
r.stderrInternal(str, true, "")
4545
}
4646

47-
const CR string = "\x1b[2m␍"
48-
const LF string = "\x1b[2m␊"
47+
const DIM string = "\x1b[2m"
48+
const CR string = DIM + "␍"
49+
const LF string = DIM + "␊"
4950

5051
func (r *LightRenderer) stderrInternal(str string, allowNLCR bool, resetCode string) {
5152
bytes := []byte(str)
@@ -127,19 +128,21 @@ type LightRenderer struct {
127128
}
128129

129130
type LightWindow struct {
130-
renderer *LightRenderer
131-
colored bool
132-
windowType WindowType
133-
border BorderStyle
134-
top int
135-
left int
136-
width int
137-
height int
138-
posx int
139-
posy int
140-
tabstop int
141-
fg Color
142-
bg Color
131+
renderer *LightRenderer
132+
colored bool
133+
windowType WindowType
134+
border BorderStyle
135+
top int
136+
left int
137+
width int
138+
height int
139+
posx int
140+
posy int
141+
tabstop int
142+
fg Color
143+
bg Color
144+
wrapSign string
145+
wrapSignWidth int
143146
}
144147

145148
func NewLightRenderer(ttyin *os.File, theme *ColorTheme, forceBlack bool, mouse bool, tabstop int, clearOnExit bool, fullscreen bool, maxHeightFunc func(int) int) (Renderer, error) {
@@ -1105,11 +1108,12 @@ type wrappedLine struct {
11051108
displayWidth int
11061109
}
11071110

1108-
func wrapLine(input string, prefixLength int, max int, tabstop int) []wrappedLine {
1111+
func wrapLine(input string, prefixLength int, initialMax int, tabstop int, wrapSignWidth int) []wrappedLine {
11091112
lines := []wrappedLine{}
11101113
width := 0
11111114
line := ""
11121115
gr := uniseg.NewGraphemes(input)
1116+
max := initialMax
11131117
for gr.Next() {
11141118
rs := gr.Runes()
11151119
str := string(rs)
@@ -1131,6 +1135,7 @@ func wrapLine(input string, prefixLength int, max int, tabstop int) []wrappedLin
11311135
line = str
11321136
prefixLength = 0
11331137
width = w
1138+
max = initialMax - wrapSignWidth
11341139
}
11351140
}
11361141
lines = append(lines, wrappedLine{string(line), width})
@@ -1140,7 +1145,7 @@ func wrapLine(input string, prefixLength int, max int, tabstop int) []wrappedLin
11401145
func (w *LightWindow) fill(str string, resetCode string) FillReturn {
11411146
allLines := strings.Split(str, "\n")
11421147
for i, line := range allLines {
1143-
lines := wrapLine(line, w.posx, w.width, w.tabstop)
1148+
lines := wrapLine(line, w.posx, w.width, w.tabstop, w.wrapSignWidth)
11441149
for j, wl := range lines {
11451150
w.stderrInternal(wl.text, false, resetCode)
11461151
w.posx += wl.displayWidth
@@ -1153,6 +1158,11 @@ func (w *LightWindow) fill(str string, resetCode string) FillReturn {
11531158
w.MoveAndClear(w.posy, w.posx)
11541159
w.Move(w.posy+1, 0)
11551160
w.renderer.stderr(resetCode)
1161+
if len(lines) > 1 {
1162+
w.stderrInternal(DIM+w.wrapSign, false, resetCode)
1163+
w.renderer.stderr(resetCode)
1164+
w.Move(w.posy, w.wrapSignWidth)
1165+
}
11561166
}
11571167
}
11581168
}
@@ -1226,6 +1236,11 @@ func (w *LightWindow) EraseMaybe() bool {
12261236
return false
12271237
}
12281238

1239+
func (w *LightWindow) SetWrapSign(sign string, width int) {
1240+
w.wrapSign = sign
1241+
w.wrapSignWidth = width
1242+
}
1243+
12291244
func (r *LightRenderer) HideCursor() {
12301245
r.showCursor = false
12311246
r.csi("?25l")

src/tui/tcell.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,22 @@ func (p ColorPair) style() tcell.Style {
3939
type Attr int32
4040

4141
type TcellWindow struct {
42-
color bool
43-
windowType WindowType
44-
top int
45-
left int
46-
width int
47-
height int
48-
normal ColorPair
49-
lastX int
50-
lastY int
51-
moveCursor bool
52-
borderStyle BorderStyle
53-
uri *string
54-
params *string
55-
showCursor bool
42+
color bool
43+
windowType WindowType
44+
top int
45+
left int
46+
width int
47+
height int
48+
normal ColorPair
49+
lastX int
50+
lastY int
51+
moveCursor bool
52+
borderStyle BorderStyle
53+
uri *string
54+
params *string
55+
showCursor bool
56+
wrapSign string
57+
wrapSignWidth int
5658
}
5759

5860
func (w *TcellWindow) Top() int {
@@ -629,6 +631,11 @@ func (w *TcellWindow) EraseMaybe() bool {
629631
return true
630632
}
631633

634+
func (w *TcellWindow) SetWrapSign(sign string, width int) {
635+
w.wrapSign = sign
636+
w.wrapSignWidth = width
637+
}
638+
632639
func (w *TcellWindow) EncloseX(x int) bool {
633640
return x >= w.left && x < (w.left+w.width)
634641
}
@@ -757,11 +764,18 @@ Loop:
757764

758765
// word wrap:
759766
xPos := w.left + w.lastX + lx
760-
if xPos >= (w.left + w.width) {
767+
if xPos >= w.left+w.width {
761768
w.lastY++
762769
w.lastX = 0
763770
lx = 0
764771
xPos = w.left
772+
wgr := uniseg.NewGraphemes(w.wrapSign)
773+
for wgr.Next() {
774+
rs := wgr.Runes()
775+
_screen.SetContent(w.left+lx, w.top+w.lastY, rs[0], rs[1:], style.Dim(true))
776+
lx += uniseg.StringWidth(string(rs))
777+
}
778+
xPos = w.left + lx
765779
}
766780

767781
yPos := w.top + w.lastY

src/tui/tui.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,8 @@ type Window interface {
659659
LinkEnd()
660660
Erase()
661661
EraseMaybe() bool
662+
663+
SetWrapSign(string, int)
662664
}
663665

664666
type FullscreenRenderer struct {

test/test_preview.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ def test_change_preview_window
453453
tmux.send_keys 'f'
454454
tmux.until do |lines|
455455
assert_equal '::', lines[0]
456-
assert_equal ' 3', lines[1]
456+
assert_equal ' 3', lines[1]
457457
end
458458
end
459459

@@ -527,7 +527,7 @@ def test_alternative_preview_window_opts
527527
tmux.send_keys "seq 10 | #{FZF} --preview-border rounded --preview-window '~5,2,+0,<100000(~0,+100,wrap,noinfo)' --preview 'seq 1000'", :Enter
528528
tmux.until { |lines| assert_equal 10, lines.match_count }
529529
tmux.until do |lines|
530-
assert_equal ['╭────╮', '│ 10 │', '│ 0 │', '│ 10 │', '│ 1 │'], lines.take(5).map(&:strip)
530+
assert_equal ['╭────╮', '│ 10 │', '│ ↳ 0│', '│ 10 │', '│ ↳ 1│'], lines.take(5).map(&:strip)
531531
end
532532
end
533533

0 commit comments

Comments
 (0)