โ– humdrum codex / glint v1.0.2
license AGPL-3.0
7.3 KB raw
  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
package editor

import (
	"strings"

	"glint/internal/theme"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
)

// Position is a cursor location in rune coordinates.
type Position struct {
	Row, Col int
}

// Editor is a self-contained markdown text buffer. It knows nothing about
// files or the vault; the app wires load and save around it.
type Editor struct {
	Lines   []string
	Cursor  Position
	Scroll  int
	Dirty   bool
	Width   int
	Height  int // visible text rows
	goalCol int // remembered visual column for vertical moves
	theme   theme.Theme
}

// New returns an empty editor with one blank line and the default theme.
func New() *Editor {
	return &Editor{
		Lines:  []string{""},
		theme:  theme.FlexokiDark(),
		Width:  80,
		Height: 24,
	}
}

// SetTheme swaps the active theme; the next View re-scans with the new colors.
func (e *Editor) SetTheme(t theme.Theme) { e.theme = t }

// SetContent replaces the buffer, resetting cursor, scroll, and dirty state.
func (e *Editor) SetContent(b []byte) {
	text := strings.ReplaceAll(string(b), "\r\n", "\n")
	e.Lines = strings.Split(text, "\n")
	if len(e.Lines) == 0 {
		e.Lines = []string{""}
	}
	e.Cursor = Position{}
	e.Scroll = 0
	e.goalCol = 0
	e.Dirty = false
}

// Bytes serializes the buffer with \n line separators.
func (e *Editor) Bytes() []byte {
	return []byte(strings.Join(e.Lines, "\n"))
}

// SetSize records the viewport dimensions (h = visible text rows).
func (e *Editor) SetSize(w, h int) {
	e.Width = w
	if h < 1 {
		h = 1
	}
	e.Height = h
	e.followCursor()
}

func (e *Editor) curLine() []rune   { return []rune(e.Lines[e.Cursor.Row]) }
func (e *Editor) setLine(rs []rune) { e.Lines[e.Cursor.Row] = string(rs) }

// InsertRune inserts r at the cursor and advances it.
func (e *Editor) InsertRune(r rune) {
	rs := e.curLine()
	col := clamp(e.Cursor.Col, 0, len(rs))
	rs = append(rs[:col], append([]rune{r}, rs[col:]...)...)
	e.setLine(rs)
	e.Cursor.Col = col + 1
	e.Dirty = true
	e.setGoal()
	e.followCursor()
}

// InsertNewline splits the current line at the cursor.
func (e *Editor) InsertNewline() {
	rs := e.curLine()
	col := clamp(e.Cursor.Col, 0, len(rs))
	left, right := string(rs[:col]), string(rs[col:])
	e.Lines[e.Cursor.Row] = left
	rest := append([]string{right}, e.Lines[e.Cursor.Row+1:]...)
	e.Lines = append(e.Lines[:e.Cursor.Row+1], rest...)
	e.Cursor.Row++
	e.Cursor.Col = 0
	e.Dirty = true
	e.setGoal()
	e.followCursor()
}

// Backspace deletes the rune before the cursor, joining lines at column 0.
func (e *Editor) Backspace() {
	if e.Cursor.Col > 0 {
		rs := e.curLine()
		rs = append(rs[:e.Cursor.Col-1], rs[e.Cursor.Col:]...)
		e.setLine(rs)
		e.Cursor.Col--
		e.Dirty = true
		e.setGoal()
		e.followCursor()
		return
	}
	if e.Cursor.Row == 0 {
		return
	}
	prev := []rune(e.Lines[e.Cursor.Row-1])
	joinCol := len(prev)
	merged := string(prev) + e.Lines[e.Cursor.Row]
	e.Lines[e.Cursor.Row-1] = merged
	e.Lines = append(e.Lines[:e.Cursor.Row], e.Lines[e.Cursor.Row+1:]...)
	e.Cursor.Row--
	e.Cursor.Col = joinCol
	e.Dirty = true
	e.setGoal()
	e.followCursor()
}

// Delete removes the rune at the cursor, joining the next line at end-of-line.
func (e *Editor) Delete() {
	rs := e.curLine()
	if e.Cursor.Col < len(rs) {
		rs = append(rs[:e.Cursor.Col], rs[e.Cursor.Col+1:]...)
		e.setLine(rs)
		e.Dirty = true
		e.setGoal()
		e.followCursor()
		return
	}
	if e.Cursor.Row >= len(e.Lines)-1 {
		return
	}
	e.Lines[e.Cursor.Row] = e.Lines[e.Cursor.Row] + e.Lines[e.Cursor.Row+1]
	e.Lines = append(e.Lines[:e.Cursor.Row+1], e.Lines[e.Cursor.Row+2:]...)
	e.Dirty = true
	e.setGoal()
	e.followCursor()
}

// MoveLeft moves one rune left, wrapping to the end of the previous line.
func (e *Editor) MoveLeft() {
	if e.Cursor.Col > 0 {
		e.Cursor.Col--
	} else if e.Cursor.Row > 0 {
		e.Cursor.Row--
		e.Cursor.Col = len([]rune(e.Lines[e.Cursor.Row]))
	}
	e.setGoal()
	e.followCursor()
}

// MoveRight moves one rune right, wrapping to the start of the next line.
func (e *Editor) MoveRight() {
	if e.Cursor.Col < len(e.curLine()) {
		e.Cursor.Col++
	} else if e.Cursor.Row < len(e.Lines)-1 {
		e.Cursor.Row++
		e.Cursor.Col = 0
	}
	e.setGoal()
	e.followCursor()
}

// MoveUp moves to the previous visual row, keeping the goal column.
func (e *Editor) MoveUp() {
	rows := e.buildVisual()
	ci := cursorVIndex(rows, e.Cursor)
	if ci <= 0 {
		return
	}
	e.applyGoal(rows[ci-1])
	e.followCursor()
}

// MoveDown moves to the next visual row, keeping the goal column.
func (e *Editor) MoveDown() {
	rows := e.buildVisual()
	ci := cursorVIndex(rows, e.Cursor)
	if ci < 0 || ci+1 >= len(rows) {
		return
	}
	e.applyGoal(rows[ci+1])
	e.followCursor()
}

// applyGoal places the cursor at the goal column on the given visual row.
func (e *Editor) applyGoal(target vrow) {
	e.Cursor.Row = target.logRow
	e.Cursor.Col = target.start + min(e.goalCol, target.runes)
}

// MoveHome moves to column 0.
func (e *Editor) MoveHome() {
	e.Cursor.Col = 0
	e.setGoal()
	e.followCursor()
}

// MoveEnd moves to end of line.
func (e *Editor) MoveEnd() {
	e.Cursor.Col = len(e.curLine())
	e.setGoal()
	e.followCursor()
}

// followCursor scrolls the viewport so the cursor's visual row stays visible.
func (e *Editor) followCursor() {
	rows := e.buildVisual()
	ci := cursorVIndex(rows, e.Cursor)
	if ci < 0 {
		e.Scroll = 0
		return
	}
	if ci < e.Scroll {
		e.Scroll = ci
	}
	if ci >= e.Scroll+e.Height {
		e.Scroll = ci - e.Height + 1
	}
	if e.Scroll < 0 {
		e.Scroll = 0
	}
}

// setGoal records the cursor's current visual column as the goal for vertical moves.
func (e *Editor) setGoal() { e.goalCol = e.visualColOf(e.Cursor.Row, e.Cursor.Col) }

// visualColOf returns the column of (row, col) within its visual segment.
func (e *Editor) visualColOf(row, col int) int {
	segs := wrapLine(e.Lines[row], e.Width)
	for i := len(segs) - 1; i >= 0; i-- {
		if col >= segs[i].start {
			return col - segs[i].start
		}
	}
	return col
}

// HandleKey maps a key message to a buffer operation.
func (e *Editor) HandleKey(k tea.KeyMsg) {
	switch k.Type {
	case tea.KeyRunes, tea.KeySpace:
		// Real bubbletea reports a space as KeySpace with Runes == [' '], so the
		// loop already inserts it โ€” do not add a second space here.
		for _, r := range k.Runes {
			e.InsertRune(r)
		}
	case tea.KeyEnter:
		e.InsertNewline()
	case tea.KeyBackspace:
		e.Backspace()
	case tea.KeyDelete:
		e.Delete()
	case tea.KeyLeft:
		e.MoveLeft()
	case tea.KeyRight:
		e.MoveRight()
	case tea.KeyUp:
		e.MoveUp()
	case tea.KeyDown:
		e.MoveDown()
	case tea.KeyHome:
		e.MoveHome()
	case tea.KeyEnd:
		e.MoveEnd()
	case tea.KeyTab:
		e.InsertRune('\t')
	}
}

// View renders the visible visual rows, styled, with a themed cursor cell on
// the cursor's visual row. Output is e.Width columns wide; the app adds margins.
func (e *Editor) View() string {
	cursorStyle := lipgloss.NewStyle().Foreground(e.theme.Background).Background(e.theme.Pointer)
	rows := e.buildVisual()
	ci := cursorVIndex(rows, e.Cursor)
	var b strings.Builder
	end := e.Scroll + e.Height
	if end > len(rows) {
		end = len(rows)
	}
	for r := e.Scroll; r < end; r++ {
		if r == ci {
			b.WriteString(renderSpansCursor(rows[r].spans, e.Cursor.Col-rows[r].start, cursorStyle))
		} else {
			b.WriteString(renderSpans(rows[r].spans))
		}
		b.WriteByte('\n')
	}
	for r := end; r < e.Scroll+e.Height; r++ {
		b.WriteByte('\n')
	}
	return b.String()
}

func clamp(v, lo, hi int) int {
	if v < lo {
		return lo
	}
	if v > hi {
		return hi
	}
	return v
}