From 55f4ca9cd9f0c37fbbad9d38a8b6f8a23be33628 Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Sun, 8 Dec 2024 17:17:42 +0200 Subject: Backspace and enter support for viminput --- internal/client/ui/core/chat/chat.go | 9 +++++---- internal/client/ui/viminput/viminput.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) (limited to 'internal') diff --git a/internal/client/ui/core/chat/chat.go b/internal/client/ui/core/chat/chat.go index a93272f..e143eb0 100644 --- a/internal/client/ui/core/chat/chat.go +++ b/internal/client/ui/core/chat/chat.go @@ -14,13 +14,14 @@ type Model struct { } func New() Model { - vi := viminput.New(30, 3) + vi := viminput.New(90, 3) vi.Placeholder = "Send a message..." vi.PlaceholderStyle = lipgloss.NewStyle().Foreground(colors.Gray) vi.LineDecoration = func(lnum int, m viminput.Model) string { - lineNumberDecor := viminput.LineNumberDecoration(lipgloss.NewStyle()) - lineNumber := lineNumberDecor(lnum, m) - return lineNumber + " ┃ " + // lineNumberDecor := viminput.LineNumberDecoration(lipgloss.NewStyle()) + // lineNumber := lineNumberDecor(lnum, m) + // return lineNumber + " ┃ " + return "┃ " } vi.SetLines([]rune("test"), []rune("best")) diff --git a/internal/client/ui/viminput/viminput.go b/internal/client/ui/viminput/viminput.go index e3ebd0c..f45c472 100644 --- a/internal/client/ui/viminput/viminput.go +++ b/internal/client/ui/viminput/viminput.go @@ -254,6 +254,39 @@ func (m *Model) handleInsertModeKeys(key tea.KeyMsg) tea.Cmd { return nil } + if key.Type == tea.KeyBackspace { + line := m.lines[m.cursorLine] + if m.cursorColumn == 0 && m.cursorLine != 0 { + lineBefore := m.lines[m.cursorLine-1] + cursorColumn := len(lineBefore) + + m.lines = slices.Delete(m.lines, m.cursorLine, m.cursorLine+1) + m.lines[m.cursorLine-1] = append(lineBefore, line...) + + m.SetCursorLine(m.cursorLine - 1) + m.SetCursorColumn(cursorColumn) + } else if len(line) != 0 { + m.lines[m.cursorLine] = slices.Delete(line, m.cursorColumn-1, m.cursorColumn) + m.SetCursorColumn(m.cursorColumn - 1) + } + return nil + } + + if key.Type == tea.KeyEnter { + line := m.lines[m.cursorLine] + after := line[m.cursorColumn:] + m.lines[m.cursorLine] = line[:m.cursorColumn] + + var newline []rune + newline = append(newline, after...) + m.lines = slices.Insert(m.lines, m.cursorLine+1, newline) + + m.SetCursorLine(m.cursorLine + 1) + m.SetCursorColumn(0) + + return nil + } + keyStr := key.String() length := len(keyStr) if length == 1 && 32 <= keyStr[0] && keyStr[0] <= 126 { -- cgit v1.3.1