Skip to content

Commit

Permalink
Some bug fixes and performance improvements
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
noborus committed Oct 3, 2020
1 parent 62887bc commit e4bc2e3
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 18 deletions.
7 changes: 7 additions & 0 deletions oviewer/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 7 additions & 4 deletions oviewer/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 15 additions & 3 deletions oviewer/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions oviewer/mouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions oviewer/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -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--
Expand All @@ -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
Expand Down
23 changes: 16 additions & 7 deletions oviewer/oviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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))
}
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -585,15 +594,15 @@ 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.
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.
Expand Down

0 comments on commit e4bc2e3

Please sign in to comment.