From e4bc2e3fe2f6cb888811b1d8f2f9029d6be42e52 Mon Sep 17 00:00:00 2001 From: Noboru Saito Date: Sat, 3 Oct 2020 16:25:37 +0900 Subject: [PATCH] Some bug fixes and performance improvements * Fixed incorrect behavior of combining characters. * Fixed a bug when the number of lines changes when switching line numbers. * Fixed a bug of the effect of changing the processing of EOL. * Fixed to reduce useless draw. --- oviewer/content.go | 7 +++++++ oviewer/draw.go | 11 +++++++---- oviewer/input.go | 18 +++++++++++++++--- oviewer/mouse.go | 3 +++ oviewer/move.go | 8 ++++---- oviewer/oviewer.go | 23 ++++++++++++++++------- 6 files changed, 52 insertions(+), 18 deletions(-) diff --git a/oviewer/content.go b/oviewer/content.go index cc004b38..c506cafb 100644 --- a/oviewer/content.go +++ b/oviewer/content.go @@ -316,6 +316,13 @@ func contentsToStr(lc lineContents) (string, map[int]int) { log.Println(err) } bn += len(string(c.mainc)) + for _, r := range c.combc { + _, err := buff.WriteRune(r) + if err != nil { + log.Println(err) + } + bn += len(string(r)) + } } str := buff.String() byteMap[bn] = len(lc) diff --git a/oviewer/draw.go b/oviewer/draw.go index 3fad08dc..95540e10 100644 --- a/oviewer/draw.go +++ b/oviewer/draw.go @@ -25,7 +25,7 @@ func (root *Root) draw() { l, b := root.bottomLineNum(root.Doc.endNum) if root.Doc.lineNum > l || (root.Doc.lineNum == l && root.Doc.branch > b) { if root.Doc.BufEOF() { - root.setMessage("EOF") + root.message = "EOF" } root.Doc.lineNum = l root.Doc.branch = b @@ -36,8 +36,6 @@ func (root *Root) draw() { root.lnumber = make([]lineNumber, root.vHight+1) - _, normalBgColor, _ := tcell.StyleDefault.Decompose() - lY := 0 lX := 0 branch := 0 @@ -65,6 +63,10 @@ func (root *Root) draw() { branch: branch, } + for x := 0; x < root.startX; x++ { + root.Screen.SetContent(x, hy, 0, nil, tcell.StyleDefault.Normal()) + } + if root.Doc.WrapMode { lX, lY = root.wrapContents(hy, lX, lY, lc) if lX > 0 { @@ -139,7 +141,7 @@ func (root *Root) draw() { // alternate background color if root.Doc.AlternateRows { - bgColor := normalBgColor + bgColor := root.ColorNormalBg if (root.Doc.lineNum+lY)%2 == 1 { bgColor = ColorAlternate } @@ -194,6 +196,7 @@ func (root *Root) wrapContents(y int, lX int, lY int, lc lineContents) (int, int content := lc[lX+x] if x+content.width+root.startX > root.vWidth { // next line + root.drawEOL(root.startX+x, y) lX += x break } diff --git a/oviewer/input.go b/oviewer/input.go index 9cf117d2..a4b7c24b 100644 --- a/oviewer/input.go +++ b/oviewer/input.go @@ -87,7 +87,13 @@ func (root *Root) inputKeyEvent(ev *tcell.EventKey) bool { runes := []rune(input.value) input.value = string(runes[:pos]) input.cursorX = runeWidth(input.value) - input.value += string(runes[pos+1:]) + next := pos + 1 + for ; next < len(runes); next++ { + if runewidth.RuneWidth(runes[next]) != 0 { + break + } + } + input.value += string(runes[next:]) case tcell.KeyDelete: pos := stringWidth(input.value, input.cursorX) runes := []rune(input.value) @@ -96,8 +102,14 @@ func (root *Root) inputKeyEvent(ev *tcell.EventKey) bool { dp = 0 } input.value = string(runes[:pos+dp]) - if len(runes) > pos+1 { - input.value += string(runes[pos+dp+1:]) + next := pos + 1 + for ; next < len(runes); next++ { + if runewidth.RuneWidth(runes[next]) != 0 { + break + } + } + if len(runes) > next { + input.value += string(runes[dp+next:]) } case tcell.KeyLeft: if input.cursorX <= 0 { diff --git a/oviewer/mouse.go b/oviewer/mouse.go index 1372806d..b881c49d 100644 --- a/oviewer/mouse.go +++ b/oviewer/mouse.go @@ -105,6 +105,9 @@ func (root *Root) CopySelect() { func (root *Root) drawSelect(x1, y1, x2, y2 int, sel bool) { if y1 == y2 { + if x1 == x2 { + return + } if x2 < x1 { x1, x2 = x2, x1 } diff --git a/oviewer/move.go b/oviewer/move.go index b1687d47..5e3b013a 100644 --- a/oviewer/move.go +++ b/oviewer/move.go @@ -65,13 +65,13 @@ func (root *Root) moveUp() { if err != nil { return } - if len(lc) < root.vWidth || root.Doc.branch <= 0 { + if len(lc) < (root.vWidth-root.startX) || root.Doc.branch <= 0 { if (root.Doc.lineNum) >= 1 { pre, err := root.Doc.lineToContents(root.Doc.lineNum+root.Doc.Header-1, root.Doc.TabWidth) if err != nil { return } - yyLen := len(pre) / (root.vWidth + 1) + yyLen := len(pre) / ((root.vWidth - root.startX) + 1) root.Doc.branch = yyLen } root.Doc.lineNum-- @@ -95,8 +95,8 @@ func (root *Root) moveDown() { if err != nil { return } - branch := (len(lc) / root.vWidth) - if len(lc) < root.vWidth || root.Doc.branch >= branch { + branch := (len(lc) / (root.vWidth - root.startX)) + if len(lc) < (root.vWidth-root.startX) || root.Doc.branch >= branch { root.Doc.branch = 0 root.Doc.lineNum++ return diff --git a/oviewer/oviewer.go b/oviewer/oviewer.go index b01baa7d..24fe1899 100644 --- a/oviewer/oviewer.go +++ b/oviewer/oviewer.go @@ -112,6 +112,9 @@ type Config struct { // OverLine color. ColorOverLine string + // ColorNormalBg is the normal Background color. + ColorNormalBg tcell.Color + Status status // Mouse support disable. @@ -353,6 +356,9 @@ func (root *Root) Run() error { } func (root *Root) setMessage(msg string) { + if root.message == msg { + return + } root.message = msg root.debugMessage(msg) root.statusDraw() @@ -424,6 +430,9 @@ func (root *Root) setGlobalStyle() { if root.ColorOverLine != "" { OverLineStyle = OverLineStyle.Foreground(tcell.GetColor(root.ColorOverLine)) } + + _, normalBgColor, _ := tcell.StyleDefault.Decompose() + root.ColorNormalBg = normalBgColor } // prepareView prepares when the screen size is changed. @@ -484,7 +493,7 @@ func (root *Root) setWrapHeaderLen() { if err != nil { continue } - root.wrapHeaderLen += 1 + (len(lc) / root.vWidth) + root.wrapHeaderLen += 1 + (len(lc) / (root.vWidth - root.startX)) } } @@ -507,16 +516,16 @@ func (root *Root) bottomLineNum(num int) (int, int) { } lc, err := root.Doc.lineToContents(num, root.Doc.TabWidth) if err != nil { - num -= 1 + num-- continue } - branch = (len(lc) / root.vWidth) + branch = (len(lc) / (root.vWidth - root.startX)) if y-branch <= 0 { branch = branch - y break } y -= branch - num -= 1 + num-- } return num - root.Doc.Header, branch } @@ -545,7 +554,7 @@ func (root *Root) toggleAlternateRows() { // toggleLineNumMode toggles LineNumMode every time it is called. func (root *Root) toggleLineNumMode() { root.Doc.LineNumMode = !root.Doc.LineNumMode - root.updateEndNum() + root.viewSync() root.setMessage(fmt.Sprintf("Set LineNumMode %t", root.Doc.LineNumMode)) } @@ -585,7 +594,7 @@ func (root *Root) goLine(input string) { } root.moveLine(lineNum - root.Doc.Header - 1) - root.debugMessage(fmt.Sprintf("Moved to line %d", lineNum)) + root.setMessage(fmt.Sprintf("Moved to line %d", lineNum)) } // markLineNum stores the specified number of lines. @@ -593,7 +602,7 @@ func (root *Root) markLineNum() { s := strconv.Itoa(root.Doc.lineNum + 1) root.input.GoCandidate.list = toLast(root.input.GoCandidate.list, s) root.input.GoCandidate.p = 0 - root.debugMessage(fmt.Sprintf("Marked to line %d", root.Doc.lineNum)) + root.setMessage(fmt.Sprintf("Marked to line %d", root.Doc.lineNum)) } // setHeader sets the number of lines in the header.