-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathstate_model.go
77 lines (70 loc) · 1.88 KB
/
state_model.go
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
package main
import (
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
"github.com/viktomas/godu/commands"
"github.com/viktomas/godu/interactive"
)
type visualState struct {
folders []interactive.Line
selected int
xbound, ybound int
screenHeight int
}
func newVisualState(state commands.State, screenHeight int) visualState {
lines := interactive.ReportFolder(state.Folder, state.MarkedFiles)
xbound := 0
ybound := len(lines)
for index, line := range lines {
if len(line.Text)-1 > xbound {
xbound = len(line.Text) - 1
}
lines[index] = line
}
return visualState{lines, state.Selected, xbound, ybound, screenHeight}
}
func (vs visualState) GetCell(x, y int) (rune, tcell.Style, []rune, int) {
style := tcell.StyleDefault
// return empty cell if we are asking for a line that doesn't exist
if y >= len(vs.folders) {
return ' ', style, nil, 1
}
// For some reason tcell is asking for cells below the viewport, we will return empty cell
if y > vs.screenHeight {
return ' ', style, nil, 1
}
shiftedIndex := y
if vs.selected > vs.screenHeight {
// shifting the index enables displaying selected folders that would be otherwise hidden bellow the screen
shiftedIndex += vs.selected - vs.screenHeight
}
if shiftedIndex == vs.selected {
style = style.Reverse(true)
}
line := vs.folders[shiftedIndex]
if line.IsMarked {
style = style.Foreground(tcell.ColorGreen)
}
visualIndex := 0
for _, r := range line.Text {
width := runewidth.RuneWidth(r)
if visualIndex == x {
return r, style, nil, width
}
visualIndex += width
if visualIndex > x {
break
}
}
return ' ', style, nil, 1
}
func (vs visualState) GetBounds() (int, int) {
return vs.xbound, vs.ybound
}
func (visualState) SetCursor(int, int) {
}
func (visualState) GetCursor() (int, int, bool, bool) {
return 0, 0, false, false
}
func (visualState) MoveCursor(offx, offy int) {
}