-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraw_bar.go
134 lines (123 loc) · 3.56 KB
/
draw_bar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package gmir
import (
"fmt"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
)
func (v View) drawBar(screen tcell.Screen) {
screenWidth, screenHeight := screen.Size()
// Prefill bar with right style:
emitStr(screen, 0, screenHeight-1, styleBar, strings.Repeat(" ", screenWidth))
// FIXME: Would be better to calculate percentage of last visible line on screen.
percent := fmt.Sprintf("%.0f%%", 100*float32(v.line+1)/float32(len(v.lines)))
leftWidth := screenWidth - len(percent)
emitStr(screen, leftWidth, screenHeight-1, styleBar, percent)
if v.Info != "" {
emitStr(screen, 0, screenHeight-1, styleBar, v.Info+" ")
} else if v.Mode == Search || v.Mode == ReverseSearch {
v.drawSearchText(screen, leftWidth-1)
} else if v.selector == "" {
emitStr(screen, 0, screenHeight-1, styleBar, v.title+" ")
} else {
emitStr(screen, 0, screenHeight-1, styleBar, v.selector+" ")
}
}
func (v View) drawSearchText(screen tcell.Screen, maxWidth int) {
if maxWidth < 5 {
return
}
searchWidth := runewidth.StringWidth(v.Searchterm)
text := searchPrefix(v.Mode)
prefixWidth := runewidth.StringWidth(text)
maxWidth -= prefixWidth
cursor := len(text) // Byte index of cursor within text.
if searchWidth < maxWidth || v.Cursor < len(v.Searchterm) && searchWidth == maxWidth {
// Text fits within maxWidth.
text += v.Searchterm
cursor += v.Cursor
} else if v.Cursor == 0 {
// Start at cursor.
maxWidth -= 1
endIndex := headOfText(v.Searchterm[v.Cursor:], maxWidth)
text += v.Searchterm[v.Cursor:v.Cursor+endIndex] + "…"
} else {
text += "…"
cursor = len(text)
maxWidth -= 1
if v.Cursor == len(v.Searchterm) {
// Cursor is behind last character of v.Searchterm.
maxWidth -= 1
startIndex := tailOfText(v.Searchterm, searchWidth, maxWidth)
text += v.Searchterm[startIndex:]
cursor = len(text)
} else if runewidth.StringWidth(v.Searchterm[v.Cursor:]) < maxWidth {
// Start before cursor.
startIndex := tailOfText(v.Searchterm, searchWidth, maxWidth)
text += v.Searchterm[startIndex:]
cursor += v.Cursor - startIndex
} else {
// Start at cursor.
maxWidth -= 1
endIndex := headOfText(v.Searchterm[v.Cursor:], maxWidth)
text += v.Searchterm[v.Cursor:v.Cursor+endIndex] + "…"
}
}
_, screenHeight := screen.Size()
emitStrWithCursor(screen, 0, screenHeight-1, styleBar, text, cursor)
}
func searchPrefix(mode Mode) string {
if mode == Search {
return "/"
} else if mode == ReverseSearch {
return "?"
}
return ""
}
// tailOfText returns the index within text, where the tail, that fits
// within maxWidth, begins.
func tailOfText(text string, textWidth, maxWidth int) (startIndex int) {
seenWidth := 0
stopOnNextRealRune := false
for i, r := range text {
rw := runewidth.RuneWidth(r)
seenWidth += rw
if stopOnNextRealRune && rw > 0 {
startIndex = i
break
}
if textWidth-seenWidth <= maxWidth {
stopOnNextRealRune = true
}
}
return startIndex
}
// headOfText returns the index within text, where the head, that fits
// within maxWidth, ends.
func headOfText(text string, maxWidth int) (endIndex int) {
seenWidth := 0
for i, r := range text {
seenWidth += runewidth.RuneWidth(r)
endIndex = i
if seenWidth > maxWidth {
break
}
}
return endIndex
}
func emitStrWithCursor(s tcell.Screen, x, y int, style tcell.Style, str string, cursor int) {
if cursor == len(str) {
str += " "
}
for i, c := range str {
var comb []rune
w := runewidth.RuneWidth(c)
if w == 0 {
comb = []rune{c}
c = ' '
w = 1
}
s.SetContent(x, y, c, comb, style.Reverse(i != cursor))
x += w
}
}