commit f7843c5
Hubert Hirtz
·
2020-08-05 20:39:29 +0000 UTC
parent fae0359
Use a custom runewidth.Condition Because some characters were not using the number of cells runewidth.RuneWidth returned, eg \u2026 and \u2192.
3 files changed,
+20,
-15
+5,
-11
1@@ -8,7 +8,6 @@ import (
2 "time"
3
4 "github.com/gdamore/tcell"
5- "github.com/mattn/go-runewidth"
6 )
7
8 var Home = "home"
9@@ -214,8 +213,6 @@ func (b *buffer) DrawLines(screen tcell.Screen, width int, height int) {
10
11 nickColWidth := 16
12
13- var sb styleBuffer
14- sb.Reset()
15 y0 := b.scrollAmt + height
16 for i := len(b.lines) - 1; 0 <= i; i-- {
17 if y0 < 0 {
18@@ -235,7 +232,7 @@ func (b *buffer) DrawLines(screen tcell.Screen, width int, height int) {
19 printTime(screen, 0, y0, st.Bold(true), line.at)
20 }
21
22- head := truncate(line.head, nickColWidth)
23+ head := truncate(line.head, nickColWidth, "\u2026")
24 x := 6 + nickColWidth - StringWidth(head)
25 c := identColor(line.head)
26 printString(screen, &x, y0, st.Foreground(colorFromCode(c)), head)
27@@ -243,6 +240,8 @@ func (b *buffer) DrawLines(screen tcell.Screen, width int, height int) {
28 x = x0
29 y := y0
30
31+ var sb styleBuffer
32+ sb.Reset()
33 for i, r := range line.body {
34 if 0 < len(nls) && i == nls[0] {
35 x = x0
36@@ -263,7 +262,7 @@ func (b *buffer) DrawLines(screen tcell.Screen, width int, height int) {
37 x++
38 }
39 screen.SetContent(x, y, r, nil, st)
40- x += runewidth.RuneWidth(r)
41+ x += runeWidth(r)
42 }
43 }
44
45@@ -574,7 +573,7 @@ func (bs *bufferList) drawTitleList(screen tcell.Screen, y int) {
46 func printString(screen tcell.Screen, x *int, y int, st tcell.Style, s string) {
47 for _, r := range s {
48 screen.SetContent(*x, y, r, nil, st)
49- *x += runewidth.RuneWidth(r)
50+ *x += runeWidth(r)
51 }
52 }
53
54@@ -595,11 +594,6 @@ func printTime(screen tcell.Screen, x int, y int, st tcell.Style, t time.Time) {
55 screen.SetContent(x+4, y, mn1, nil, st)
56 }
57
58-func truncate(s string, w int) string {
59- c := runewidth.Condition{ZeroWidthJoiner: true}
60- return c.Truncate(s, w, "\u2026")
61-}
62-
63 func identColor(s string) (code int) {
64 h := fnv.New32()
65 _, _ = h.Write([]byte(s))
+2,
-3
1@@ -2,7 +2,6 @@ package ui
2
3 import (
4 "github.com/gdamore/tcell"
5- "github.com/mattn/go-runewidth"
6 )
7
8 // editor is the text field where the user writes messages and commands.
9@@ -56,7 +55,7 @@ func (e *editor) PutRune(r rune) {
10 copy(e.text[e.cursorIdx+1:], e.text[e.cursorIdx:])
11 e.text[e.cursorIdx] = r
12
13- rw := runewidth.RuneWidth(r)
14+ rw := runeWidth(r)
15 tw := e.textWidth[len(e.textWidth)-1]
16 e.textWidth = append(e.textWidth, tw+rw)
17 for i := e.cursorIdx + 1; i < len(e.textWidth); i++ {
18@@ -155,7 +154,7 @@ func (e *editor) Draw(screen tcell.Screen, y int) {
19 for i < len(e.text) && x < e.width {
20 r := e.text[i]
21 screen.SetContent(x, y, r, nil, st)
22- x += runewidth.RuneWidth(r)
23+ x += runeWidth(r)
24 i++
25 }
26
+13,
-1
1@@ -5,6 +5,18 @@ import (
2 "github.com/mattn/go-runewidth"
3 )
4
5+var condition runewidth.Condition = runewidth.Condition{
6+ EastAsianWidth: true,
7+}
8+
9+func runeWidth(r rune) int {
10+ return condition.RuneWidth(r)
11+}
12+
13+func truncate(s string, w int, tail string) string {
14+ return condition.Truncate(s, w, tail)
15+}
16+
17 type widthBuffer struct {
18 width int
19 color colorBuffer
20@@ -25,7 +37,7 @@ func (wb *widthBuffer) WriteRune(r rune) {
21 if 1 < ok {
22 wb.width++
23 }
24- wb.width += runewidth.RuneWidth(r)
25+ wb.width += runeWidth(r)
26 }
27 }
28