From 9ef27203f009004f67c56ebe9bd84183758ed3be Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Thu, 16 Feb 2017 16:52:17 -0500 Subject: [PATCH] Add support for line numbers and cursor locations in the new view --- cmd/micro/view2.go | 83 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/cmd/micro/view2.go b/cmd/micro/view2.go index db8c6b54..142088f0 100644 --- a/cmd/micro/view2.go +++ b/cmd/micro/view2.go @@ -19,6 +19,24 @@ func (v *View) DisplayView() { v.lineNumOffset = 0 } + // We need to add to the line offset if there are gutter messages + var hasGutterMessages bool + for _, v := range v.messages { + if len(v) > 0 { + hasGutterMessages = true + } + } + if hasGutterMessages { + v.lineNumOffset += 2 + } + + if v.x != 0 { + // One space for the extra split divider + v.lineNumOffset++ + } + + xOffset := v.x + v.lineNumOffset + height := v.Height width := v.Width left := v.leftCol @@ -26,10 +44,69 @@ func (v *View) DisplayView() { v.cellview.Draw(v.Buf, top, height, left, width) - for _, line := range v.cellview.lines { - for _, char := range line { + screenX := v.x + for lineN, line := range v.cellview.lines { + screenX = v.x + curLineN := v.Topline + lineN + + if v.x != 0 { + // Draw the split divider + screen.SetContent(screenX, lineN, '|', nil, defStyle.Reverse(true)) + screenX++ + } + lineNumStyle := defStyle + if v.Buf.Settings["ruler"] == true { + // Write the line number + if style, ok := colorscheme["line-number"]; ok { + lineNumStyle = style + } + if style, ok := colorscheme["current-line-number"]; ok { + if curLineN == v.Cursor.Y && tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() { + lineNumStyle = style + } + } + + lineNum := strconv.Itoa(curLineN + 1) + + // Write the spaces before the line number if necessary + for i := 0; i < maxLineNumLength-len(lineNum); i++ { + screen.SetContent(screenX, lineN, ' ', nil, lineNumStyle) + screenX++ + } + // Write the actual line number + for _, ch := range lineNum { + screen.SetContent(screenX, lineN, ch, nil, lineNumStyle) + screenX++ + } + + // Write the extra space + screen.SetContent(screenX, lineN, ' ', nil, lineNumStyle) + screenX++ + } + + var lastChar *Char + for i, char := range line { if char != nil { - screen.SetContent(char.visualLoc.X, char.visualLoc.Y, char.char, nil, char.style) + if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() && + v.Cursor.Y == char.realLoc.Y && v.Cursor.X == char.realLoc.X { + screen.ShowCursor(xOffset+char.visualLoc.X, char.visualLoc.Y) + } + screen.SetContent(xOffset+char.visualLoc.X, char.visualLoc.Y, char.char, nil, char.style) + if i == len(line)-1 { + lastChar = char + } + } + } + + if lastChar != nil { + if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() && + v.Cursor.Y == lastChar.realLoc.Y && v.Cursor.X == lastChar.realLoc.X+1 { + screen.ShowCursor(xOffset+lastChar.visualLoc.X+1, lastChar.visualLoc.Y) + } + } else if len(line) == 0 { + if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() && + v.Cursor.Y == curLineN { + screen.ShowCursor(xOffset, lineN) } } }