-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (105 loc) · 2.66 KB
/
main.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
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
package cropviewport
import (
tea "github.com/charmbracelet/bubbletea"
process "github.com/ogios/ansisgr-process"
)
type CropViewportModel struct {
ANSITableList *process.ANSITableList
KeyMap map[string]func() tea.Cmd
Sublines []*SubLine
Block [4]int
}
func NewCropViewportModel() tea.Model {
c := &CropViewportModel{}
c.KeyMap = map[string]func() tea.Cmd{
"j": func() tea.Cmd {
c.NextLine(1)
return nil
},
"k": func() tea.Cmd {
c.PrevLine(1)
return nil
},
"ctrl+d": func() tea.Cmd {
c.NextLine(c.Block[3] / 2)
return nil
},
"ctrl+u": func() tea.Cmd {
c.PrevLine(c.Block[3] / 2)
return nil
},
"h": func() tea.Cmd {
c.PrevCol(1)
return nil
},
"l": func() tea.Cmd {
c.NextCol(1)
return nil
},
"H": func() tea.Cmd {
c.PrevCol(c.Block[2] / 2)
return nil
},
"L": func() tea.Cmd {
c.NextCol(c.Block[2] / 2)
return nil
},
}
return c
}
func (c *CropViewportModel) SetBlock(x, y, width, height int) {
c.Block = [4]int{
x, y, width, height,
}
}
func (c *CropViewportModel) SetContent(s string) (*process.ANSITableList, []*SubLine) {
t, l := ProcessContent(s)
c.SetContentGivenData(t, l)
return t, l
}
func (c *CropViewportModel) SetContentGivenData(tableList *process.ANSITableList, lines []*SubLine) {
c.ANSITableList = tableList
c.Sublines = lines
}
func (c *CropViewportModel) BackToTop() {
c.Block[1] = 0
}
func (c *CropViewportModel) BackToLeft() {
c.Block[0] = 0
}
func (c *CropViewportModel) PrevLine(step int) {
c.Block[1] = max(c.Block[1]-step, 0)
}
func (c *CropViewportModel) NextLine(step int) {
c.Block[1] = min(c.Block[1]+step, max(len(c.Sublines)-c.Block[3], 0))
}
func (c *CropViewportModel) PrevCol(step int) {
c.Block[0] = max(c.Block[0]-step, 0)
}
func (c *CropViewportModel) NextCol(step int) {
c.Block[0] += step
}
// * returns nil
func (c *CropViewportModel) Init() tea.Cmd {
return nil
}
func (c *CropViewportModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds := []tea.Cmd{}
switch msg := msg.(type) {
case tea.KeyMsg:
handler, ok := c.KeyMap[msg.String()]
if ok {
cmds = append(cmds, handler())
}
}
return c, tea.Batch(cmds...)
}
// const NO_CONTENT = "No content available"
// var NO_CONTENT_TABLE, NO_CONTENT_SUBLINES = process.ProcessContent("No content available")
var NO_CONTENT_TABLE, NO_CONTENT_SUBLINES = ProcessContent("No content available")
func (c *CropViewportModel) View() string {
if c.ANSITableList == nil || c.Sublines == nil {
return CropView(NO_CONTENT_TABLE, NO_CONTENT_SUBLINES, c.Block[0], c.Block[1], c.Block[2], c.Block[3])
}
return CropView(c.ANSITableList, c.Sublines, c.Block[0], c.Block[1], c.Block[2], c.Block[3])
}