Cursor improvements

This commit is contained in:
Zachary Yedidia 2018-08-27 17:55:28 -04:00
parent dd619b3ff5
commit c3e2085e3c
9 changed files with 64 additions and 15 deletions

1
.gitignore vendored
View File

@ -10,3 +10,4 @@ packages/
todo.txt
test.txt
log.txt
*.old

View File

@ -40,9 +40,9 @@ update:
# Builds the runtime
runtime:
go get -u github.com/jteeuwen/go-bindata/...
$(GOBIN)/go-bindata -nometadata -o runtime.go runtime/...
mv runtime.go cmd/micro
gofmt -w cmd/micro/runtime.go
$(GOBIN)/go-bindata -pkg config -nomemcopy -nometadata -o runtime.go runtime/...
mv runtime.go cmd/micro/config
gofmt -w cmd/micro/config/runtime.go
test:
go test ./cmd/micro

View File

@ -30,21 +30,29 @@ func (a *BufActionHandler) Center() bool {
// CursorUp moves the cursor up
func (a *BufActionHandler) CursorUp() bool {
a.Cursor.Deselect(true)
a.Cursor.Up()
return true
}
// CursorDown moves the cursor down
func (a *BufActionHandler) CursorDown() bool {
a.Cursor.Deselect(true)
a.Cursor.Down()
return true
}
// CursorLeft moves the cursor left
func (a *BufActionHandler) CursorLeft() bool {
a.Cursor.Deselect(true)
a.Cursor.Left()
return true
}
// CursorRight moves the cursor right
func (a *BufActionHandler) CursorRight() bool {
a.Cursor.Deselect(true)
a.Cursor.Right()
return true
}

View File

@ -74,6 +74,7 @@ func NewBufActionHandler(buf *buffer.Buffer, win *Window) *BufActionHandler {
Buf: buf,
Loc: buf.StartCursor,
}}
a.Cursor = a.cursors[0]
buf.SetCursors(a.cursors)
return a

View File

@ -48,7 +48,7 @@ func (c *Cursor) Goto(b Cursor) {
// the current cursor its selection too
func (c *Cursor) GotoLoc(l Loc) {
c.X, c.Y = l.X, l.Y
c.LastVisualX = c.GetVisualX()
c.StoreVisualX()
}
// GetVisualX returns the x value of the cursor in visual spaces
@ -89,11 +89,13 @@ func (c *Cursor) GetCharPosInLine(b []byte, visualPos int) int {
width += runewidth.RuneWidth(r)
}
i++
if width >= visualPos {
if width == visualPos {
i++
}
break
}
i++
}
return i
@ -155,6 +157,21 @@ func (c *Cursor) DeleteSelection() {
}
}
// Deselect closes the cursor's current selection
// Start indicates whether the cursor should be placed
// at the start or end of the selection
func (c *Cursor) Deselect(start bool) {
if c.HasSelection() {
if start {
c.Loc = c.CurSelection[0]
} else {
c.Loc = c.CurSelection[1]
}
c.ResetSelection()
c.StoreVisualX()
}
}
// GetSelection returns the cursor's selection
func (c *Cursor) GetSelection() []byte {
if InBounds(c.CurSelection[0], c.Buf) && InBounds(c.CurSelection[1], c.Buf) {
@ -244,7 +261,7 @@ func (c *Cursor) Left() {
c.Up()
c.End()
}
c.LastVisualX = c.GetVisualX()
c.StoreVisualX()
}
// Right moves the cursor right one cell (if possible) or
@ -259,7 +276,7 @@ func (c *Cursor) Right() {
c.Down()
c.Start()
}
c.LastVisualX = c.GetVisualX()
c.StoreVisualX()
}
// Relocate makes sure that the cursor is inside the bounds
@ -278,3 +295,7 @@ func (c *Cursor) Relocate() {
c.X = utf8.RuneCount(c.Buf.LineBytes(c.Y))
}
}
func (c *Cursor) StoreVisualX() {
c.LastVisualX = c.GetVisualX()
}

View File

@ -150,7 +150,7 @@ func main() {
for {
// Display everything
w.Clear()
screen.Screen.Fill(' ', config.DefStyle)
w.DisplayBuffer()
w.DisplayStatusLine()
screen.Screen.Show()

View File

@ -40,12 +40,12 @@ func Init() {
Screen, err = tcell.NewScreen()
if err != nil {
fmt.Println(err)
fmt.Println("Fatal: Micro could not initialize a screen.")
fmt.Println("Fatal: Micro could not initialize a Screen.")
os.Exit(1)
}
} else {
fmt.Println(err)
fmt.Println("Fatal: Micro could not initialize a screen.")
fmt.Println("Fatal: Micro could not initialize a Screen.")
os.Exit(1)
}
}
@ -64,4 +64,6 @@ func Init() {
}
os.Setenv("TCELLDB", tcelldb)
// Screen.SetStyle(defStyle)
}

View File

@ -30,8 +30,8 @@ type StatusLine struct {
// NewStatusLine returns a statusline bound to a window
func NewStatusLine(win *Window) *StatusLine {
s := new(StatusLine)
// s.FormatLeft = "$(filename) $(modified)($(line),$(col)) $(opt:filetype) $(opt:fileformat)"
s.FormatLeft = "$(filename) $(modified)(line,col) $(opt:filetype) $(opt:fileformat)"
s.FormatLeft = "$(filename) $(modified)($(line),$(col)) $(opt:filetype) $(opt:fileformat)"
// s.FormatLeft = "$(filename) $(modified)(line,col) $(opt:filetype) $(opt:fileformat)"
s.FormatRight = "$(bind:ToggleKeyMenu): show bindings, $(bind:ToggleHelp): open help"
s.Info = map[string]func(*buffer.Buffer) string{
"filename": func(b *buffer.Buffer) string {
@ -41,10 +41,10 @@ func NewStatusLine(win *Window) *StatusLine {
return b.GetName()
},
"line": func(b *buffer.Buffer) string {
return strconv.Itoa(b.GetActiveCursor().Y)
return strconv.Itoa(b.GetActiveCursor().Y + 1)
},
"col": func(b *buffer.Buffer) string {
return strconv.Itoa(b.GetActiveCursor().X)
return strconv.Itoa(b.GetActiveCursor().X + 1)
},
"modified": func(b *buffer.Buffer) string {
if b.Modified() {

View File

@ -82,6 +82,15 @@ func (w *Window) GetStyle(style tcell.Style, bloc buffer.Loc, r rune) tcell.Styl
return style
}
func (w *Window) ShowCursor(x, y int, main bool) {
if main {
screen.Screen.ShowCursor(x, y)
} else {
r, _, _, _ := screen.Screen.GetContent(x, y)
screen.Screen.SetContent(x, y, r, nil, config.DefStyle.Reverse(true))
}
}
// DisplayBuffer draws the buffer being shown in this window on the screen.Screen
func (w *Window) DisplayBuffer() {
b := w.Buf
@ -134,6 +143,10 @@ func (w *Window) DisplayBuffer() {
line, nColsBeforeStart := util.SliceVisualEnd(line, bloc.X, tabsize)
totalwidth := bloc.X - nColsBeforeStart
for len(line) > 0 {
if w.Buf.GetActiveCursor().X == bloc.X && w.Buf.GetActiveCursor().Y == bloc.Y {
w.ShowCursor(vloc.X, vloc.Y, true)
}
r, size := utf8.DecodeRune(line)
curStyle = w.GetStyle(curStyle, bloc, r)
@ -186,6 +199,9 @@ func (w *Window) DisplayBuffer() {
}
}
}
if w.Buf.GetActiveCursor().X == bloc.X && w.Buf.GetActiveCursor().Y == bloc.Y {
w.ShowCursor(vloc.X, vloc.Y, true)
}
bloc.X = w.StartCol
bloc.Y++
if bloc.Y >= b.LinesNum() {