Fix multibyte ui (#8)

* gofmt

* Use ASCII symbols on CJK console

* text width should be calculated as cell width

* Add offset for x to adjust point to start multi-byte letters
This commit is contained in:
mattn 2018-12-05 13:15:56 +09:00 committed by Pankaj Garg
parent feffecb31c
commit c451749056
4 changed files with 32 additions and 9 deletions

View file

@ -7,11 +7,12 @@ package main
import (
"bufio"
tm "github.com/nsf/termbox-go"
"net"
"os"
"strconv"
"strings"
tm "github.com/nsf/termbox-go"
)
type ethrNetDevInfo struct {

View file

@ -6,11 +6,12 @@
package main
import (
tm "github.com/nsf/termbox-go"
"net"
"strings"
"syscall"
"unsafe"
tm "github.com/nsf/termbox-go"
)
var kernel32 = syscall.NewLazyDLL("kernel32.dll")

View file

@ -8,11 +8,12 @@ package main
import (
"errors"
"fmt"
tm "github.com/nsf/termbox-go"
"os"
"sync"
"sync/atomic"
"time"
tm "github.com/nsf/termbox-go"
)
type ethrTestResultAggregate struct {

32
ui.go
View file

@ -7,9 +7,11 @@ package main
import (
"fmt"
tm "github.com/nsf/termbox-go"
"math"
"time"
"github.com/mattn/go-runewidth"
tm "github.com/nsf/termbox-go"
)
const (
@ -56,6 +58,12 @@ type table struct {
border int
}
func init() {
if runewidth.IsEastAsian() {
symbols = []rune{'+', '-', '+', '|', '+', '+', '+', '+', '+', '+', '+', ' ', '░', '▒', '▓', '█', '^', 'v'}
}
}
func (t *table) drawTblRow(ledge, redge, middle, spr rune, fg, bg tm.Attribute) {
twidth := t.ccount + 1
for _, w := range t.cwidth {
@ -126,10 +134,14 @@ func printHLineText(x, y int, w int, text string) {
for i := 0; i < w; i++ {
tm.SetCell(x+i, y, symbols[horizontal], tm.ColorWhite, tm.ColorDefault)
}
offset := (w - len(text)) / 2
offset := (w - runewidth.StringWidth(text)) / 2
textArr := []rune(text)
xoff := 0
for i := 0; i < len(text); i++ {
tm.SetCell(x+offset+i, y, textArr[i], tm.ColorWhite, tm.ColorDefault)
tm.SetCell(x+offset+i+xoff, y, textArr[i], tm.ColorWhite, tm.ColorDefault)
if runewidth.RuneWidth(textArr[i]) == 2 {
xoff++
}
}
}
@ -145,19 +157,27 @@ func printText(x, y, w int, text string, fg, bg tm.Attribute) {
for i := 0; i < w; i++ {
tm.SetCell(x+i, y, ' ', fg, bg)
}
xoff := 0
for i := 0; i < len(textArr); i++ {
tm.SetCell(x+i, y, textArr[i], fg, bg)
tm.SetCell(x+i+xoff, y, textArr[i], fg, bg)
if runewidth.RuneWidth(textArr[i]) == 2 {
xoff++
}
}
}
func printCenterText(x, y, w int, text string, fg, bg tm.Attribute) {
offset := (w - len(text)) / 2
offset := (w - runewidth.StringWidth(text)) / 2
textArr := []rune(text)
for i := 0; i < w; i++ {
tm.SetCell(x+i, y, ' ', fg, bg)
}
xoff := 0
for i := 0; i < len(textArr); i++ {
tm.SetCell(x+offset+i, y, textArr[i], fg, bg)
tm.SetCell(x+offset+i+xoff, y, textArr[i], fg, bg)
if runewidth.RuneWidth(textArr[i]) == 2 {
xoff++
}
}
}