forked from gizak/termui
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmain.go
More file actions
116 lines (103 loc) · 2.81 KB
/
main.go
File metadata and controls
116 lines (103 loc) · 2.81 KB
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
package main
import (
"fmt"
"log"
ui "github.com/metaspartan/gotui/v5"
"github.com/metaspartan/gotui/v5/widgets"
)
func main() {
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize gotui: %v", err)
}
defer ui.Close()
termWidth, termHeight := ui.TerminalDimensions()
// Vertical Scrollbar
vScroll := widgets.NewScrollbar()
vScroll.SetRect(termWidth-3, 0, termWidth, termHeight-3)
vScroll.Orientation = widgets.ScrollbarVertical
vScroll.Max = 100
vScroll.PageSize = 20
vScroll.ThumbStyle = ui.NewStyle(ui.ColorYellow)
vScroll.TrackStyle = ui.NewStyle(ui.ColorWhite)
vScroll.Border = false
// Horizontal Scrollbar
hScroll := widgets.NewScrollbar()
hScroll.SetRect(0, termHeight-3, termWidth-4, termHeight)
hScroll.Orientation = widgets.ScrollbarHorizontal
hScroll.Max = 100
hScroll.PageSize = 20
hScroll.ThumbStyle = ui.NewStyle(ui.ColorGreen)
hScroll.TrackStyle = ui.NewStyle(ui.ColorYellow) // Match example Yellow track
hScroll.Border = false
// Ratatui example: track="-", thumb="▮", begin="<", end=">"
hScroll.TrackRune = '-'
hScroll.ThumbRune = '▮'
hScroll.BeginRune = '<'
hScroll.EndRune = '>'
// Information
p := widgets.NewParagraph()
p.Title = "Controller"
p.Text = "Use Arrow Keys to Scroll or MouseWheel.\nVertical: Up/Down\nHorizontal: Left/Right\nq: Quit"
// make it bigger
p.SetRect(termWidth/2-20, termHeight/2-3, termWidth/2+20, termHeight/2+5)
render := func() {
p.Text = fmt.Sprintf(
"Use Arrow Keys to Scroll or MouseWheel.\nVertical: %d/%d\nHorizontal: %d/%d\nq: Quit",
vScroll.Current, vScroll.Max,
hScroll.Current, hScroll.Max,
)
ui.Render(vScroll, hScroll, p)
}
render()
uiEvents := ui.PollEvents()
for {
e := <-uiEvents
if handleScrollbarEvents(e, vScroll, hScroll) {
return
}
render()
}
}
func handleScrollbarEvents(e ui.Event, vScroll, hScroll *widgets.Scrollbar) bool {
switch e.ID {
case "q", "<C-c>":
return true
case "<Left>", "<Right>":
handleHorizontalScroll(hScroll, e.ID)
default:
handleVerticalScroll(vScroll, e.ID)
}
return false
}
func handleVerticalScroll(vScroll *widgets.Scrollbar, key string) {
switch key {
case "<Up>", "<MouseWheelUp>":
vScroll.Current--
if vScroll.Current < 0 {
vScroll.Current = 0
}
case "<Down>", "<MouseWheelDown>":
vScroll.Current++
if vScroll.Current > vScroll.Max-vScroll.PageSize {
vScroll.Current = vScroll.Max - vScroll.PageSize
}
case "PageUp":
vScroll.Current -= vScroll.PageSize
case "PageDown":
vScroll.Current += vScroll.PageSize
}
}
func handleHorizontalScroll(hScroll *widgets.Scrollbar, key string) {
switch key {
case "<Left>":
hScroll.Current--
if hScroll.Current < 0 {
hScroll.Current = 0
}
case "<Right>":
hScroll.Current++
if hScroll.Current > hScroll.Max-hScroll.PageSize {
hScroll.Current = hScroll.Max - hScroll.PageSize
}
}
}