Toggle line numbers

This commit is contained in:
aerth 2016-04-22 20:02:26 +00:00
parent acabfcb8bb
commit 0440ca45cd
No known key found for this signature in database
GPG key ID: 63B5467D7A697B1D
2 changed files with 45 additions and 18 deletions

View file

@ -21,6 +21,7 @@ type Settings struct {
AutoIndent bool `json:"autoindent"` AutoIndent bool `json:"autoindent"`
Syntax bool `json:"syntax"` Syntax bool `json:"syntax"`
TabsToSpaces bool `json:"tabsToSpaces"` TabsToSpaces bool `json:"tabsToSpaces"`
Ruler bool `json:"ruler"`
} }
// InitSettings initializes the options map and sets all options to their default values // InitSettings initializes the options map and sets all options to their default values
@ -61,6 +62,7 @@ func DefaultSettings() Settings {
AutoIndent: true, AutoIndent: true,
Syntax: true, Syntax: true,
TabsToSpaces: false, TabsToSpaces: false,
Ruler: true,
} }
} }
@ -103,7 +105,17 @@ func SetOption(view *View, args []string) {
messenger.Error("Invalid value for " + option) messenger.Error("Invalid value for " + option)
return return
} }
} else if option == "ruler" {
if value == "on" {
settings.Ruler = true
} else if value == "off" {
settings.Ruler = false
} else {
messenger.Error("Invalid value for " + option)
return
} }
}
err := WriteSettings(filename) err := WriteSettings(filename)
if err != nil { if err != nil {
messenger.Error("Error writing to settings.json: " + err.Error()) messenger.Error("Error writing to settings.json: " + err.Error())

View file

@ -1,13 +1,14 @@
package main package main
import ( import (
"github.com/atotto/clipboard"
"github.com/gdamore/tcell"
"github.com/mitchellh/go-homedir"
"io/ioutil" "io/ioutil"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"github.com/atotto/clipboard"
"github.com/gdamore/tcell"
"github.com/mitchellh/go-homedir"
) )
// The View struct stores information about a view into a buffer. // The View struct stores information about a view into a buffer.
@ -509,6 +510,12 @@ func (v *View) HandleEvent(event tcell.Event) {
case tcell.KeyCtrlD: case tcell.KeyCtrlD:
v.HalfPageDown() v.HalfPageDown()
relocate = false relocate = false
case tcell.KeyCtrlR:
if settings.Ruler == false {
settings.Ruler = true
} else {
settings.Ruler = false
}
case tcell.KeyRune: case tcell.KeyRune:
// Insert a character // Insert a character
if v.cursor.HasSelection() { if v.cursor.HasSelection() {
@ -620,8 +627,11 @@ func (v *View) DisplayView() {
// We are going to have to offset by that amount // We are going to have to offset by that amount
maxLineLength := len(strconv.Itoa(len(v.buf.lines))) maxLineLength := len(strconv.Itoa(len(v.buf.lines)))
// + 1 for the little space after the line number // + 1 for the little space after the line number
if settings.Ruler == true {
v.lineNumOffset = maxLineLength + 1 v.lineNumOffset = maxLineLength + 1
} else {
v.lineNumOffset = 0
}
var highlightStyle tcell.Style var highlightStyle tcell.Style
for lineN := 0; lineN < v.height; lineN++ { for lineN := 0; lineN < v.height; lineN++ {
@ -639,7 +649,9 @@ func (v *View) DisplayView() {
lineNumStyle = style lineNumStyle = style
} }
// Write the spaces before the line number if necessary // Write the spaces before the line number if necessary
lineNum := strconv.Itoa(lineN + v.topline + 1) var lineNum string
if settings.Ruler == true {
lineNum = strconv.Itoa(lineN + v.topline + 1)
for i := 0; i < maxLineLength-len(lineNum); i++ { for i := 0; i < maxLineLength-len(lineNum); i++ {
screen.SetContent(x, lineN, ' ', nil, lineNumStyle) screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
x++ x++
@ -649,10 +661,13 @@ func (v *View) DisplayView() {
screen.SetContent(x, lineN, ch, nil, lineNumStyle) screen.SetContent(x, lineN, ch, nil, lineNumStyle)
x++ x++
} }
if settings.Ruler == true {
// Write the extra space // Write the extra space
screen.SetContent(x, lineN, ' ', nil, lineNumStyle) screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
x++ x++
}
}
// Write the line // Write the line
tabchars := 0 tabchars := 0
runes := []rune(line) runes := []rune(line)