diff --git a/tools/tui/readline/actions.go b/tools/tui/readline/actions.go index 16181e2c0..ea0c22a85 100644 --- a/tools/tui/readline/actions.go +++ b/tools/tui/readline/actions.go @@ -258,3 +258,25 @@ func (self *Readline) erase_chars_after_cursor(amt uint, traverse_line_breaks bo self.erase_between(pos, self.cursor) return num } + +func (self *Readline) perform_action(ac Action, repeat_count uint) bool { + switch ac { + case ActionBackspace: + return self.erase_chars_before_cursor(repeat_count, true) > 0 + case ActionDelete: + return self.erase_chars_after_cursor(repeat_count, true) > 0 + case ActionMoveToStartOfLine: + return self.move_to_start_of_line() + case ActionMoveToEndOfLine: + return self.move_to_end_of_line() + case ActionMoveToStartOfDocument: + return self.move_to_start() + case ActionMoveToEndOfDocument: + return self.move_to_end() + case ActionCursorLeft: + return self.move_cursor_left(repeat_count, true) > 0 + case ActionCursorRight: + return self.move_cursor_right(repeat_count, true) > 0 + } + return false +} diff --git a/tools/tui/readline/api.go b/tools/tui/readline/api.go index 72eb13ed2..4ed4fe062 100644 --- a/tools/tui/readline/api.go +++ b/tools/tui/readline/api.go @@ -30,6 +30,20 @@ func (self Position) Less(other Position) bool { return self.Y < other.Y || (self.Y == other.Y && self.X < other.X) } +type Action uint + +const ( + ActionNil Action = iota + ActionBackspace + ActionDelete + ActionMoveToStartOfLine + ActionMoveToEndOfLine + ActionMoveToStartOfDocument + ActionMoveToEndOfDocument + ActionCursorLeft + ActionCursorRight +) + type Readline struct { prompt string prompt_len int @@ -97,3 +111,7 @@ func (self *Readline) OnText(text string, from_key_event bool, in_bracketed_past self.add_text(text) return nil } + +func (self *Readline) PerformAction(ac Action, repeat_count uint) bool { + return self.perform_action(ac, repeat_count) +} diff --git a/tools/tui/readline/keys.go b/tools/tui/readline/keys.go index 0546b178f..9cbd90a73 100644 --- a/tools/tui/readline/keys.go +++ b/tools/tui/readline/keys.go @@ -10,9 +10,34 @@ import ( var _ = fmt.Print +var default_shortcuts = map[string]Action{ + "backspace": ActionBackspace, + "delete": ActionDelete, + "home": ActionMoveToStartOfLine, + "end": ActionMoveToEndOfLine, + "ctrl+home": ActionMoveToStartOfDocument, + "ctrl+end": ActionMoveToEndOfDocument, + "left": ActionCursorLeft, + "right": ActionCursorRight, +} + +func action_for_key_event(event *loop.KeyEvent, shortcuts map[string]Action) Action { + for sc, ac := range shortcuts { + if event.MatchesPressOrRepeat(sc) { + return ac + } + } + return ActionNil +} + func (self *Readline) handle_key_event(event *loop.KeyEvent) error { if event.Text != "" { return nil } + ac := action_for_key_event(event, default_shortcuts) + if ac != ActionNil { + event.Handled = true + self.perform_action(ac, 1) + } return nil }