-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (79 loc) · 1.95 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
package main
import (
"fmt"
"os"
tea "github.com/charmbracelet/bubbletea"
"github.com/shawalli/bubbles/tabs"
)
func tabSizeCmd(width, height int) tea.Cmd {
return func() tea.Msg { return tabs.TabSizeMsg{Width: width, Height: height} }
}
type PageModel struct {
content string
width int
height int
}
func (pm PageModel) Init() tea.Cmd { return nil }
func (pm PageModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "left":
pm.width = pm.width - 1
cmd = tabSizeCmd(pm.width, pm.height)
case "right":
pm.width = pm.width + 1
cmd = tabSizeCmd(pm.width, pm.height)
}
case tea.WindowSizeMsg:
pm.width = msg.Width
pm.height = msg.Height
}
return pm, cmd
}
func (pm PageModel) View() string { return pm.content }
type Model struct {
tabs tabs.Model
}
func (m Model) Init() tea.Cmd { return nil }
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "esc", "q", "ctrl+c":
return m, tea.Quit
}
}
t, cmd := m.tabs.Update(msg)
m.tabs = t.(tabs.Model)
return m, cmd
}
func (m Model) View() string {
return m.tabs.View()
}
func main() {
t := []tabs.Tab{
tabs.NewTab(
"Tab 1",
PageModel{content: "This is the content 1.\n\nYou can adjust my width with the left and right arrows!"},
),
tabs.NewTab(
"Tab 2",
PageModel{content: "This is the content 2.\n\nWhen you're looking at me,\nany change in my width affects my sibling tabs as well!"},
),
tabs.NewTab(
"Tab 3",
PageModel{content: "This is the content 3.\n\nResizing the terminal window resets all tabs to the terminal width."},
),
tabs.NewTab(
"Tab 4",
PageModel{content: "This is the content 4.\n\nHappy to be here!"},
),
}
m := Model{tabs: tabs.New(t...)}
if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Printf("could not run program: %v", err)
os.Exit(1)
}
}