generated from charmbracelet/bubbletea-app-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
136 lines (123 loc) · 3.43 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"fmt"
"os"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/vidyasagar0405/go-varcall/tabs"
)
type errMsg error
type model struct {
samtoolsTabModel tabs.SamtoolsModel
bcftoolsTabModel tabs.BcftoolsModel
help help.Model
err error
helpTabModel tabs.HelpModel
state tabs.SessionState
Keys KeyMap
tabs []string
homeTabModel tabs.HomeModel
textInput textinput.Model
spinner spinner.Model
activeTab int
}
func initialModel() model {
s := spinner.New()
s.Spinner = spinner.Points
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
return model{
samtoolsTabModel: tabs.InitialSamtoolsModel(),
bcftoolsTabModel: tabs.InitialBcftoolsModel(),
help: help.New(),
err: nil,
helpTabModel: tabs.InitialHelpModel(),
state: tabs.SessionState{},
Keys: Keys,
tabs: []string{"Home", "Samtools", "Bcftools", "Help"},
homeTabModel: tabs.InitialHomeModel(),
textInput: textinput.Model{},
spinner: s,
activeTab: 0,
}
}
func (m model) tabView() string {
var tabViews []string
for i, tab := range m.tabs {
style := lipgloss.NewStyle().Padding(0, 1, 0, 3)
if i == m.activeTab {
style = style.Bold(true).Foreground(lipgloss.Color("205"))
}
tabViews = append(tabViews, style.Render(tab))
}
return lipgloss.JoinHorizontal(lipgloss.Left, tabViews...)
}
func (m model) Init() tea.Cmd {
return m.spinner.Tick
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.help.Width = msg.Width
case tea.KeyMsg:
switch {
case key.Matches(msg, m.Keys.quit):
return m, tea.Quit
case key.Matches(msg, m.Keys.nextTab):
m.activeTab = (m.activeTab + 1) % len(m.tabs)
case key.Matches(msg, m.Keys.prevTab):
m.activeTab = (m.activeTab - 1 + len(m.tabs)) % len(m.tabs)
case key.Matches(msg, m.Keys.Help):
m.help.ShowAll = !m.help.ShowAll
}
default:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
var cmd tea.Cmd
switch m.activeTab {
case 0:
m.homeTabModel, cmd = m.homeTabModel.Update(msg)
m.state = m.homeTabModel.UpdateSessionState(m.state)
case 1:
m.samtoolsTabModel, cmd = m.samtoolsTabModel.Update(msg)
case 2:
m.bcftoolsTabModel, cmd = m.bcftoolsTabModel.Update(msg)
case 3:
m.helpTabModel, cmd = m.helpTabModel.Update(msg)
}
return m, cmd
}
func (m model) View() string {
titleStyle := lipgloss.NewStyle().
MarginLeft(2).
Padding(1).
Bold(true).
Border(lipgloss.HiddenBorder(), false).
Foreground(lipgloss.Color("#F25D93")).
SetString("varcall")
tabContent := ""
switch m.activeTab {
case 0:
tabContent = m.homeTabModel.View()
case 1:
tabContent = m.samtoolsTabModel.View()
case 2:
tabContent = m.bcftoolsTabModel.View()
case 3:
tabContent = m.helpTabModel.View()
}
helpView := m.help.View(m.Keys)
return fmt.Sprintf("%v\n\n%s\n\n%s\n\n%v\n\n%v", titleStyle, m.tabView(), tabContent, m.spinner.View(), helpView)
}
func main() {
p := tea.NewProgram(initialModel(), tea.WithAltScreen(), tea.WithMouseAllMotion())
if _, err := p.Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}