summaryrefslogtreecommitdiff
path: root/internal/client/ui/viminput/viminput.go
blob: e3ebd0c238da0afc535924a32a28ccfa64ac5228 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package viminput

import (
	"slices"
	"strings"
	"unicode"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
	"github.com/kyren223/eko/internal/client/ui/colors"
)

var DefaultCursorStyle = lipgloss.NewStyle().Background(colors.White).Foreground(colors.Background)

type (
	LineDecoration = func(lnum int, m Model) string
)

const Unchanged = -1

const (
	NormalMode = iota
	InsertMode
	VisualMode
	VisualLineMode
	OpendingMode
)

type Model struct {
	PlaceholderStyle lipgloss.Style
	PromptStyle      lipgloss.Style

	Placeholder    string
	LineDecoration LineDecoration

	lines        [][]rune
	cursorLine   int
	cursorColumn int
	goalColumn   int
	mode         int
	pending      byte

	width  int
	height int
	focus  bool
}

func New(width, height int) Model {
	return Model{
		PlaceholderStyle: lipgloss.NewStyle(),
		PromptStyle:      lipgloss.NewStyle(),
		Placeholder:      "",
		LineDecoration:   EmptyLineDecoration,
		lines:            [][]rune{},
		cursorLine:       0,
		cursorColumn:     0,
		goalColumn:       -1,
		mode:             NormalMode,
		width:            width,
		height:           height,
		focus:            false,
	}
}

func (m Model) Init() tea.Cmd {
	return nil
}

func (m Model) View() string {
	var lines [][]rune
	if len(m.lines) == 0 {
		placeholder := m.PlaceholderStyle.Render(m.Placeholder)
		lines = append(lines, []rune(placeholder))
	} else {
		lines = m.lines
	}

	var builder strings.Builder
	for i, line := range lines {
		lineDecoration := m.LineDecoration(i, m)
		builder.WriteString(lineDecoration)

		if m.cursorLine != i {
			builder.WriteString(string(line))
		} else if m.cursorColumn == len(m.lines[m.cursorLine]) {
			builder.WriteString(string(line))
			builder.WriteString(DefaultCursorStyle.Render(" "))
		} else {
			builder.WriteString(string(line[:m.cursorColumn]))
			builder.WriteString(DefaultCursorStyle.Render(string(line[m.cursorColumn])))
			builder.WriteString(string(line[m.cursorColumn+1:]))
		}

		builder.WriteByte('\n')
	}

	result := builder.String()
	result = lipgloss.NewStyle().Width(m.width).Height(m.height).Render(result)

	return result
}

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
	if !m.focus {
		return m, nil
	}

	switch msg := msg.(type) {
	case tea.KeyMsg:
		cmd := m.handleKeys(msg)
		return m, cmd
	}

	return m, nil
}

func (m *Model) SetWidth(width int) {
	m.width = width
}

func (m Model) Width() int {
	return m.width
}

func (m *Model) SetHeight(height int) {
	m.height = height
}

func (m Model) Height() int {
	return m.height
}

func (m *Model) Focus() {
	m.focus = true
}

func (m *Model) Blur() {
	m.focus = false
}

func (m *Model) SetLines(lines ...[]rune) {
	m.lines = lines
}

func (m *Model) Lines() [][]rune {
	return m.lines
}

func (m *Model) SetCursorColumn(col int) {
	if len(m.lines[m.cursorLine]) == 0 {
		m.cursorColumn = 0
	} else {
		m.cursorColumn = max(col, 0)
	}
	m.goalColumn = -1
}

func (m *Model) SetCursorLine(line int) {
	m.cursorLine = min(max(line, 0), len(m.lines)-1)
	fromLength := m.cursorColumn
	toLength := len(m.lines[m.cursorLine])
	if fromLength > toLength && m.goalColumn == -1 {
		m.goalColumn = fromLength
	}
	if m.goalColumn != -1 {
		m.cursorColumn = max(min(toLength-1, m.goalColumn), 0)
	}
}

// func (m *Model) cursorColumn int {
// 	return m.cursorColumn
// }
//
// func (m *Model) cursorLine int {
// 	return m.cursorLine
// }

func (m *Model) handleKeys(key tea.KeyMsg) tea.Cmd {
	switch m.mode {
	case NormalMode:
		return m.handleNormalModeKeys(key)
	case InsertMode:
		return m.handleInsertModeKeys(key)
	}

	return nil
}

func (m *Model) handleNormalModeKeys(key tea.KeyMsg) tea.Cmd {
	line, col := m.Motion(key.String())
	if line != -1 {
		m.SetCursorLine(line)
	}
	if col != -1 {
		m.SetCursorColumn(col)
	}

	switch key.String() {
	case "d":
		fallthrough
	case "y":
		fallthrough
	case "c":
		fallthrough
	case "v":
		m.mode = OpendingMode
		m.pending = key.String()[0]

	case "i":
		m.mode = InsertMode
	case "a":
		m.mode = InsertMode
		m.SetCursorColumn(m.cursorColumn + 1)
	case "I":
		m.SetCursorColumn(0)
		m.mode = InsertMode
	case "A":
		m.mode = InsertMode
		m.SetCursorColumn(len(m.lines[m.cursorLine]))

	case "x":
		line := m.lines[m.cursorLine]
		if len(line) != 0 {
			end := len(line) - 1
			copy(line[m.cursorColumn:], line[m.cursorColumn+1:])
			m.lines[m.cursorLine] = line[:end]
			if m.cursorColumn == end {
				m.SetCursorColumn(m.cursorColumn - 1)
			}
		}
	case "D":
		line := m.lines[m.cursorLine]
		if len(line) != 0 {
			m.lines[m.cursorLine] = line[:m.cursorColumn]
			m.SetCursorColumn(m.cursorColumn - 1)
		}
	case "o":
		m.lines = slices.Insert(m.lines, m.cursorLine+1, []rune(""))
		m.SetCursorLine(m.cursorLine + 1)
		m.SetCursorColumn(0)
		m.mode = InsertMode
	case "O":
		m.lines = slices.Insert(m.lines, m.cursorLine, []rune(""))
		m.SetCursorColumn(0)
		m.mode = InsertMode
	}
	return nil
}

func (m *Model) handleInsertModeKeys(key tea.KeyMsg) tea.Cmd {
	if key.Type == tea.KeyEscape {
		m.SetCursorColumn(m.cursorColumn - 1)
		m.mode = NormalMode
		return nil
	}

	keyStr := key.String()
	length := len(keyStr)
	if length == 1 && 32 <= keyStr[0] && keyStr[0] <= 126 {
		line := m.lines[m.cursorLine]
		m.lines[m.cursorLine] = slices.Insert(line, m.cursorColumn, rune(keyStr[0]))
		m.SetCursorColumn(m.cursorColumn + 1)
	}

	return nil
}

func (m *Model) RuneAtCursor() rune {
	return m.lines[m.cursorLine][m.cursorColumn]
}

func (m Model) Motion(motion string) (line, col int) {
	switch motion {
	case "h":
		return Unchanged, m.cursorColumn - 1
	case "j":
		return m.cursorLine + 1, Unchanged
	case "k":
		return m.cursorLine - 1, Unchanged
	case "l":
		return Unchanged, min(m.cursorColumn+1, len(m.lines[m.cursorLine])-1)

	case "0":
		return Unchanged, 0
	case "$":
		return Unchanged, len(m.lines[m.cursorLine]) - 1
	case "_":
		for i, r := range m.lines[m.cursorLine] {
			if !unicode.IsSpace(r) {
				return Unchanged, i
			}
		}
		return Unchanged, len(m.lines[m.cursorLine]) - 1
	case "-":
		if m.cursorLine-1 < 0 {
			return Unchanged, Unchanged
		}
		line := m.lines[m.cursorLine-1]
		for i, r := range line {
			if !unicode.IsSpace(r) {
				return m.cursorLine - 1, i
			}
		}
		return m.cursorLine - 1, len(line) - 1
	case "+":
		if m.cursorLine+1 >= len(m.lines) {
			return Unchanged, Unchanged
		}
		line := m.lines[m.cursorLine+1]
		for i, r := range line {
			if !unicode.IsSpace(r) {
				return m.cursorLine + 1, i
			}
		}
		return m.cursorLine + 1, len(line) - 1
	// case "E":
	// for {
	// 	line := m.lines[m.cursorLine]
	// 	m.SetCursorColumn(m.cursorColumn + 1)
	// 	for m.cursorColumn == len(line) {
	// 		cursorLine := m.cursorLine + 1
	// 		m.SetCursorLine(cursorLine)
	// 		m.SetCursorColumn(0)
	// 		if cursorLine == len(m.lines) {
	// 			return nil
	// 		}
	// 	}
	// 	if !unicode.IsSpace(m.RuneAtCursor()) {
	// 		break
	// 	}
	// }
	// for !unicode.IsSpace(m.RuneAtCursor()) {
	// 	line := m.lines[m.cursorLine]
	// 	m.SetCursorColumn(m.cursorColumn + 1)
	// 	if m.cursorColumn == len(line) {
	// 		break
	// 	}
	// }
	// m.SetCursorColumn(m.cursorColumn - 1)
	default:
		return Unchanged, Unchanged
	}
}