summaryrefslogtreecommitdiff
path: root/ui/editor.go
blob: c735e48c6d36ff4770368c74da003f9f2c42dd0e (plain) (blame)
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
package ui

import (
	"github.com/gdamore/tcell"
	"github.com/mattn/go-runewidth"
)

// editor is the text field where the user writes messages and commands.
type editor struct {
	// text contains the written runes. An empty slice means no text is written.
	text []rune

	// textWidth[i] contains the width of string(text[:i]). Therefore
	// len(textWidth) is always strictly greater than 0 and textWidth[0] is
	// always 0.
	textWidth []int

	// cursorIdx is the index in text of the placement of the cursor, or is
	// equal to len(text) if the cursor is at the end.
	cursorIdx int

	// offsetIdx is the number of elements of text that are skipped when
	// rendering.
	offsetIdx int

	// width is the width of the screen.
	width int
}

func newEditor(width int) editor {
	return editor{
		text:      []rune{},
		textWidth: []int{0},
		width:     width,
	}
}

func (e *editor) Resize(width int) {
	if width < e.width {
		e.cursorIdx = 0
		e.offsetIdx = 0
	}
	e.width = width
}

func (e *editor) IsCommand() bool {
	return len(e.text) != 0 && e.text[0] == '/'
}

func (e *editor) TextLen() int {
	return len(e.text)
}

func (e *editor) PutRune(r rune) {
	e.text = append(e.text, ' ')
	copy(e.text[e.cursorIdx+1:], e.text[e.cursorIdx:])
	e.text[e.cursorIdx] = r

	rw := runewidth.RuneWidth(r)
	tw := e.textWidth[len(e.textWidth)-1]
	e.textWidth = append(e.textWidth, tw+rw)
	for i := e.cursorIdx + 1; i < len(e.textWidth); i++ {
		e.textWidth[i] = rw + e.textWidth[i-1]
	}

	e.Right()
}

func (e *editor) RemRune() (ok bool) {
	ok = 0 < e.cursorIdx
	if !ok {
		return
	}

	// TODO avoid looping twice
	rw := e.textWidth[e.cursorIdx] - e.textWidth[e.cursorIdx-1]
	for i := e.cursorIdx; i < len(e.textWidth); i++ {
		e.textWidth[i] -= rw
	}
	copy(e.textWidth[e.cursorIdx:], e.textWidth[e.cursorIdx+1:])
	e.textWidth = e.textWidth[:len(e.textWidth)-1]

	copy(e.text[e.cursorIdx-1:], e.text[e.cursorIdx:])
	e.text = e.text[:len(e.text)-1]
	e.Left()
	return
}

func (e *editor) Flush() (content string) {
	content = string(e.text)
	e.text = e.text[:0]
	e.textWidth = e.textWidth[:1]
	e.cursorIdx = 0
	e.offsetIdx = 0
	return
}

func (e *editor) Right() {
	if e.cursorIdx == len(e.text) {
		return
	}
	e.cursorIdx++
	if e.width < e.textWidth[e.cursorIdx]-e.textWidth[e.offsetIdx] {
		e.offsetIdx += 16
		max := len(e.text) - 1
		if max < e.offsetIdx {
			e.offsetIdx = max
		}
	}
}

func (e *editor) Left() {
	if e.cursorIdx == 0 {
		return
	}
	e.cursorIdx--
	if e.cursorIdx <= e.offsetIdx {
		e.offsetIdx -= 16
		if e.offsetIdx < 0 {
			e.offsetIdx = 0
		}
	}
}

func (e *editor) Draw(screen tcell.Screen, y int) {
	st := tcell.StyleDefault

	x := 0
	i := e.offsetIdx

	for i < len(e.text) && x < e.width {
		r := e.text[i]
		screen.SetContent(x, y, r, nil, st)
		x += runewidth.RuneWidth(r)
		i++
	}

	for x < e.width {
		screen.SetContent(x, y, ' ', nil, st)
		x++
	}

	curStart := e.textWidth[e.cursorIdx] - e.textWidth[e.offsetIdx]
	curEnd := curStart + 1
	if e.cursorIdx+1 < len(e.textWidth) {
		curEnd = e.textWidth[e.cursorIdx+1] - e.textWidth[e.offsetIdx]
	}
	for x := curStart; x < curEnd; x++ {
		screen.ShowCursor(x, y)
	}
}