Skip to content

Commit

Permalink
textarea: new bindings for "go to begin" / "go to end"
Browse files Browse the repository at this point in the history
  • Loading branch information
knz committed Aug 29, 2022
1 parent 776062e commit 531cf2e
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion textarea/textarea.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type KeyMap struct {
Paste key.Binding
WordBackward key.Binding
WordForward key.Binding
MoveToBegin key.Binding
MoveToEnd key.Binding

UppercaseWordForward key.Binding
LowercaseWordForward key.Binding
Expand Down Expand Up @@ -73,6 +75,8 @@ var DefaultKeyMap = KeyMap{
LineStart: key.NewBinding(key.WithKeys("home", "ctrl+a")),
LineEnd: key.NewBinding(key.WithKeys("end", "ctrl+e")),
Paste: key.NewBinding(key.WithKeys("ctrl+v")),
MoveToBegin: key.NewBinding(key.WithKeys("alt+<", "ctrl+home")),
MoveToEnd: key.NewBinding(key.WithKeys("alt+>", "ctrl+end")),

CapitalizeWordForward: key.NewBinding(key.WithKeys("alt+c")),
LowercaseWordForward: key.NewBinding(key.WithKeys("alt+l")),
Expand Down Expand Up @@ -737,6 +741,19 @@ func (m Model) Width() int {
return m.width
}

// moveToBegin moves the cursor to the beginning of the input.
func (m *Model) moveToBegin() {
m.row = 0
m.col = 0
m.lastCharOffset = 0
}

// moveToEnd moves the cursor to the end of the input.
func (m *Model) moveToEnd() {
m.row = len(m.value) - 1
m.SetCursor(len(m.value[m.row]))
}

// SetWidth sets the width of the textarea to fit exactly within the given width.
// This means that the textarea will account for the width of the prompt and
// whether or not line numbers are being shown.
Expand Down Expand Up @@ -862,7 +879,10 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.CursorUp()
case key.Matches(msg, m.KeyMap.WordBackward):
m.wordLeft()

case key.Matches(msg, m.KeyMap.MoveToBegin):
m.moveToBegin()
case key.Matches(msg, m.KeyMap.MoveToEnd):
m.moveToEnd()
case key.Matches(msg, m.KeyMap.LowercaseWordForward):
m.lowercaseRight()
case key.Matches(msg, m.KeyMap.UppercaseWordForward):
Expand Down

0 comments on commit 531cf2e

Please sign in to comment.