2024-09-07 10:36:44 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2024-09-07 16:31:10 -07:00
|
|
|
"sort"
|
2024-09-07 10:36:44 -07:00
|
|
|
|
2024-09-07 15:23:46 -07:00
|
|
|
"github.com/charmbracelet/bubbles/help"
|
|
|
|
"github.com/charmbracelet/bubbles/key"
|
2024-09-07 16:31:10 -07:00
|
|
|
"github.com/charmbracelet/bubbles/spinner"
|
2024-09-07 10:36:44 -07:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2024-09-07 14:38:48 -07:00
|
|
|
gloss "github.com/charmbracelet/lipgloss"
|
2024-09-07 16:01:47 -07:00
|
|
|
"github.com/charmbracelet/lipgloss/list"
|
2024-09-07 14:38:48 -07:00
|
|
|
"golang.org/x/term"
|
2024-09-07 10:36:44 -07:00
|
|
|
)
|
|
|
|
|
2024-09-07 14:38:48 -07:00
|
|
|
var width, height, _ = term.GetSize(int(os.Stdout.Fd()))
|
|
|
|
|
2024-09-07 10:36:44 -07:00
|
|
|
type menu struct {
|
2024-09-07 15:23:46 -07:00
|
|
|
order SoftwarePackages
|
|
|
|
current int
|
|
|
|
keys keyMap
|
|
|
|
help help.Model
|
|
|
|
inputStyle gloss.Style
|
2024-09-07 16:31:10 -07:00
|
|
|
spinner spinner.Model
|
2024-09-07 16:34:18 -07:00
|
|
|
quitting bool
|
2024-09-07 10:36:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func initialModel() menu {
|
2024-09-07 16:31:10 -07:00
|
|
|
s := spinner.New()
|
|
|
|
s.Spinner = spinner.MiniDot
|
|
|
|
s.Style = gloss.NewStyle().Foreground(gloss.Color("3"))
|
|
|
|
|
2024-09-07 10:36:44 -07:00
|
|
|
return menu{
|
2024-09-07 16:34:18 -07:00
|
|
|
current: 3,
|
|
|
|
keys: keys,
|
|
|
|
help: help.New(),
|
|
|
|
spinner: s,
|
|
|
|
quitting: false,
|
2024-09-07 15:23:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type keyMap struct {
|
|
|
|
Quit key.Binding
|
|
|
|
}
|
|
|
|
|
|
|
|
var keys = keyMap{
|
|
|
|
Quit: key.NewBinding(
|
|
|
|
key.WithKeys("q", "esc", "ctrl+c"),
|
|
|
|
key.WithHelp("q", "quit"),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k keyMap) ShortHelp() []key.Binding {
|
|
|
|
return []key.Binding{k.Quit}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k keyMap) FullHelp() [][]key.Binding {
|
|
|
|
keys := k.ShortHelp()
|
|
|
|
|
|
|
|
return [][]key.Binding{
|
|
|
|
keys,
|
2024-09-07 10:36:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 14:38:48 -07:00
|
|
|
type yamlMsg YamlStructure
|
|
|
|
|
|
|
|
type errMsg struct{ err error }
|
|
|
|
|
|
|
|
func (e errMsg) Error() string { return e.err.Error() }
|
|
|
|
|
2024-09-07 10:36:44 -07:00
|
|
|
func (m menu) Init() tea.Cmd {
|
2024-09-07 16:31:10 -07:00
|
|
|
return tea.Batch(readYaml, m.spinner.Tick)
|
2024-09-07 10:36:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
2024-09-07 16:31:10 -07:00
|
|
|
var cmd tea.Cmd
|
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
2024-09-07 10:36:44 -07:00
|
|
|
switch msg := msg.(type) {
|
2024-09-07 14:38:48 -07:00
|
|
|
|
|
|
|
case yamlMsg:
|
|
|
|
m.order = msg.SoftwarePackages
|
2024-09-07 15:23:46 -07:00
|
|
|
return m, nil
|
2024-09-07 14:38:48 -07:00
|
|
|
|
2024-09-07 10:36:44 -07:00
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.String() {
|
|
|
|
case "ctrl+c", "q":
|
2024-09-07 16:34:18 -07:00
|
|
|
m.quitting = true
|
2024-09-07 10:36:44 -07:00
|
|
|
return m, tea.Quit
|
|
|
|
}
|
2024-09-07 16:31:10 -07:00
|
|
|
|
|
|
|
case spinner.TickMsg:
|
|
|
|
m.spinner, cmd = m.spinner.Update(msg)
|
|
|
|
cmds = append(cmds, cmd)
|
2024-09-07 10:36:44 -07:00
|
|
|
}
|
|
|
|
|
2024-09-07 16:31:10 -07:00
|
|
|
return m, tea.Batch(cmds...)
|
2024-09-07 10:36:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m menu) View() string {
|
2024-09-07 15:43:19 -07:00
|
|
|
borderStyle := gloss.NewStyle().
|
|
|
|
BorderStyle(gloss.RoundedBorder()).
|
|
|
|
BorderForeground(gloss.Color("5")).
|
|
|
|
Padding(0, 1)
|
|
|
|
|
|
|
|
mainStyle := borderStyle.
|
|
|
|
Width(int(float64(width) * 0.65))
|
|
|
|
|
|
|
|
sidebarStyle := borderStyle.
|
|
|
|
Width(int(float64(width) * 0.3))
|
2024-09-07 14:38:48 -07:00
|
|
|
|
2024-09-07 15:23:46 -07:00
|
|
|
mainContent := ""
|
2024-09-07 16:01:47 -07:00
|
|
|
|
|
|
|
softwareListEnumerator := func(l list.Items, i int) string {
|
|
|
|
if m.current == i {
|
2024-09-07 16:31:10 -07:00
|
|
|
return m.spinner.View()
|
2024-09-07 16:01:47 -07:00
|
|
|
} else if m.current > i {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
software := list.New().Enumerator(softwareListEnumerator)
|
2024-09-07 14:38:48 -07:00
|
|
|
|
2024-09-07 16:31:10 -07:00
|
|
|
keys := make([]string, 0, len(m.order))
|
|
|
|
for k := range m.order {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
software.Item(m.order[k].Name)
|
2024-09-07 15:23:46 -07:00
|
|
|
}
|
|
|
|
|
2024-09-07 16:01:47 -07:00
|
|
|
sidebarContent := software.String()
|
|
|
|
|
2024-09-07 14:38:48 -07:00
|
|
|
main := mainStyle.Render(mainContent)
|
|
|
|
sidebar := sidebarStyle.Render(sidebarContent)
|
2024-09-07 10:36:44 -07:00
|
|
|
|
2024-09-07 14:38:48 -07:00
|
|
|
content := gloss.JoinHorizontal(gloss.Top, main, sidebar)
|
2024-09-07 10:36:44 -07:00
|
|
|
|
2024-09-07 15:23:46 -07:00
|
|
|
helpView := m.help.View(m.keys)
|
|
|
|
|
2024-09-07 16:34:18 -07:00
|
|
|
last := ""
|
|
|
|
if m.quitting {
|
|
|
|
last = "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
page := gloss.JoinVertical(gloss.Left, content, helpView, last)
|
2024-09-07 15:23:46 -07:00
|
|
|
|
|
|
|
return gloss.PlaceHorizontal(width, gloss.Center, page)
|
2024-09-07 10:36:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
p := tea.NewProgram(initialModel())
|
|
|
|
|
|
|
|
if _, err := p.Run(); err != nil {
|
|
|
|
fmt.Printf("There's been an error: %v", err)
|
|
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|