Skip to content

Commit

Permalink
let arrow keys add lines to commit message window
Browse files Browse the repository at this point in the history
Allows users to dynamically increase or decrease the height of the
commit message window by using the up & down arrows.  Pressing the down
arrow when the cursor is on the last line of the window increases the
height of the window.  Pressing the up arrow when the cursor is on the
last line, AND the last line is blank, decreases the height of the
window by one line.

Relates to: jesseduffield#28
  • Loading branch information
sl4mmy committed Mar 28, 2021
1 parent be13f3a commit 6bfdfad
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions pkg/gui/editors.go
@@ -1,6 +1,7 @@
package gui

import (
"strings"
"unicode"

"github.com/jesseduffield/gocui"
Expand All @@ -20,9 +21,22 @@ func (gui *Gui) commitMessageEditor(v *gocui.View, key gocui.Key, ch rune, mod g
case key == gocui.KeyDelete:
v.EditDelete(false)
case key == gocui.KeyArrowDown:
v.MoveCursor(0, 1, false)
_, cursorY := v.Cursor()
_, viewY := v.Size()
if cursorY+1 == viewY {
increaseViewHeight(v, cursorY)
} else {
v.MoveCursor(0, 1, false)
}
case key == gocui.KeyArrowUp:
v.MoveCursor(0, -1, false)
cursorX, cursorY := v.Cursor()
_, viewY := v.Size()
if viewY > 5 && cursorY+1 == viewY {
decreaseViewHeightWhenLastLineIsBlank(v, cursorY)
v.SetCursor(cursorX, cursorY-1)
} else {
v.MoveCursor(0, -1, false)
}
case key == gocui.KeyArrowLeft:
v.MoveCursor(-1, 0, false)
case key == gocui.KeyArrowRight:
Expand Down Expand Up @@ -80,3 +94,32 @@ func (gui *Gui) defaultEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.M
gui.setSuggestions(suggestions)
}
}

func decreaseViewHeightWhenLastLineIsBlank(v *gocui.View, currentLineNumber int) {
line, err := v.Line(currentLineNumber)
if err != nil {
return
}

if len(strings.TrimSpace(line)) > 0 {
return
}

lineLength := len(line)
v.SetCursor(0, currentLineNumber)
for i := 0; i < lineLength; i++ {
v.EditDelete(false)
}
v.EditDelete(true)
}

func increaseViewHeight(v *gocui.View, currentLineNumber int) {
line, err := v.Line(currentLineNumber)
if err != nil {
return
}

lineLength := len(line)
v.SetCursor(lineLength, currentLineNumber)
v.EditNewLine()
}

0 comments on commit 6bfdfad

Please sign in to comment.