1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-17 00:18:05 +02:00

support going to start/end of line and deleting lines in simple editor

This commit is contained in:
Jesse Duffield
2019-05-26 12:27:59 +10:00
parent 527c025a0c
commit c039e5bed0
6 changed files with 70 additions and 40 deletions

View File

@ -33,18 +33,10 @@ var DefaultEditor Editor = EditorFunc(simpleEditor)
// simpleEditor is used as the default gocui editor.
func simpleEditor(v *View, key Key, ch rune, mod Modifier) {
switch {
case ch != 0 && mod == 0:
v.EditWrite(ch)
case key == KeySpace:
v.EditWrite(' ')
case key == KeyBackspace || key == KeyBackspace2:
v.EditDelete(true)
case key == KeyDelete:
v.EditDelete(false)
case key == KeyInsert:
v.Overwrite = !v.Overwrite
case key == KeyEnter:
v.EditNewLine()
case key == KeyArrowDown:
v.MoveCursor(0, 1, false)
case key == KeyArrowUp:
@ -53,6 +45,20 @@ func simpleEditor(v *View, key Key, ch rune, mod Modifier) {
v.MoveCursor(-1, 0, false)
case key == KeyArrowRight:
v.MoveCursor(1, 0, false)
case key == KeyTab:
v.EditNewLine()
case key == KeySpace:
v.EditWrite(' ')
case key == KeyInsert:
v.Overwrite = !v.Overwrite
case key == KeyCtrlU:
v.EditDeleteToStartOfLine()
case key == KeyCtrlA:
v.EditGotoToStartOfLine()
case key == KeyCtrlE:
v.EditGotoToEndOfLine()
default:
v.EditWrite(ch)
}
}
@ -63,6 +69,48 @@ func (v *View) EditWrite(ch rune) {
v.moveCursor(w, 0, true)
}
// EditDeleteToStartOfLine is the equivalent of pressing ctrl+U in your terminal, it deletes to the end of the line. Or if you are already at the start of the line, it deletes the newline character
func (v *View) EditDeleteToStartOfLine() {
x, _ := v.Cursor()
if x == 0 {
v.EditDelete(true)
} else {
// delete characters until we are the start of the line
for x > 0 {
v.EditDelete(true)
x, _ = v.Cursor()
}
}
}
// EditGotoToStartOfLine takes you to the start of the current line
func (v *View) EditGotoToStartOfLine() {
x, _ := v.Cursor()
for x > 0 {
v.MoveCursor(-1, 0, false)
x, _ = v.Cursor()
}
}
// EditGotoToEndOfLine takes you to the end of the line
func (v *View) EditGotoToEndOfLine() {
_, y := v.Cursor()
_ = v.SetCursor(0, y+1)
x, newY := v.Cursor()
if newY == y {
// we must be on the last line, so lets move to the very end
prevX := -1
for prevX != x {
prevX = x
v.MoveCursor(1, 0, false)
x, _ = v.Cursor()
}
} else {
// most left so now we're at the end of the original line
v.MoveCursor(-1, 0, false)
}
}
// EditDelete deletes a rune at the cursor position. back determines the
// direction.
func (v *View) EditDelete(back bool) {

View File

@ -733,7 +733,7 @@ func (g *Gui) execKeybindings(v *View, ev *termbox.Event) (matched bool, err err
if kb.matchView(v) {
return g.execKeybinding(v, kb)
}
if kb.viewName == "" && ((v != nil && !v.Editable) || kb.ch == 0) {
if kb.viewName == "" && ((v != nil && !v.Editable) || (kb.ch == 0 && kb.key != KeyCtrlU && kb.key != KeyCtrlA && kb.key != KeyCtrlE)) {
globalKb = kb
}
}

View File

@ -8,6 +8,7 @@ import (
"bytes"
"io"
"strings"
"sync"
"time"
"github.com/go-errors/errors"
@ -91,6 +92,8 @@ type View struct {
// If HasLoader is true, the message will be appended with a spinning loader animation
HasLoader bool
writeMutex sync.Mutex
}
type viewLine struct {
@ -224,6 +227,8 @@ func (v *View) Origin() (x, y int) {
// be called to clear the view's buffer.
func (v *View) Write(p []byte) (n int, err error) {
v.tainted = true
v.writeMutex.Lock()
defer v.writeMutex.Unlock()
for _, ch := range bytes.Runes(p) {
switch ch {
@ -250,6 +255,7 @@ func (v *View) Write(p []byte) (n int, err error) {
}
}
}
return len(p), nil
}
@ -603,3 +609,8 @@ func Loader() cell {
chr: chr,
}
}
// IsTainted tells us if the view is tainted
func (v *View) IsTainted() bool {
return v.tainted
}