bartender/main.go

185 lines
3.5 KiB
Go
Raw Normal View History

2024-09-07 10:36:44 -07:00
package main
import (
"fmt"
"os"
2024-09-07 16:31:10 -07:00
"sort"
"strings"
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"
"github.com/charmbracelet/bubbles/viewport"
2024-09-07 10:36:44 -07:00
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
2024-09-07 14:38:48 -07:00
"golang.org/x/term"
2024-09-07 10:36:44 -07:00
)
type menu struct {
order []string
current int
keys keyMap
help help.Model
spinner spinner.Model
quitting bool
width int
height int
sub chan string
output *string
viewport viewport.Model
2024-09-07 10:36:44 -07:00
}
2024-09-07 18:01:05 -07:00
const (
softwareInstructionsFile = "/Users/marley/hackin/install.fairie/home/.chezmoidata.yaml"
softwareGroup = "_Full-Desktop"
)
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 = lipgloss.NewStyle().Foreground(lipgloss.Color("3"))
width, height, _ := term.GetSize(int(os.Stdout.Fd()))
2024-09-07 16:31:10 -07:00
m := menu{
current: 0,
keys: keys,
help: help.New(),
spinner: s,
quitting: false,
width: width,
height: height,
sub: make(chan string),
output: new(string),
viewport: viewport.New(0, 30),
2024-09-07 15:23:46 -07:00
}
return m
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 18:01:05 -07:00
type softwareListMsg []string
2024-09-07 14:38:48 -07:00
type errMsg struct{ err error }
func (e errMsg) Error() string { return e.err.Error() }
func (m *menu) appendOutput(s string) {
*m.output += "\n" + s
m.viewport.SetContent(*m.output)
m.viewport.GotoBottom()
}
2024-09-07 10:36:44 -07:00
func (m menu) Init() tea.Cmd {
2024-09-07 18:01:05 -07:00
return tea.Batch(getSoftwareList(softwareInstructionsFile), m.spinner.Tick)
2024-09-07 10:36:44 -07:00
}
func (m menu) setDimensions() {
m.width, m.height, _ = term.GetSize(int(os.Stdout.Fd()))
}
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
2024-09-07 18:01:05 -07:00
case softwareListMsg:
m.order = msg
cmds = append(cmds, m.installPackage(), waitForCmdResponses(m.sub))
case cmdMsg:
m.appendOutput(string(msg))
cmds = append(cmds, waitForCmdResponses(m.sub))
case cmdDoneMsg:
m.current++
m.output = new(string)
cmds = append(cmds, m.installPackage(), waitForCmdResponses(m.sub))
2024-09-07 14:38:48 -07:00
2024-09-07 10:36:44 -07:00
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Quit):
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)
case tea.WindowSizeMsg:
m.setDimensions()
m.viewport.Width = lipgloss.Width(m.mainView())
m.viewport.Height = lipgloss.Height(m.mainView())
case errMsg:
m.appendOutput("Error: " + msg.Error())
2024-09-07 10:36:44 -07:00
}
m.viewport, cmd = m.viewport.Update(msg)
cmds = append(cmds, cmd)
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 {
content := lipgloss.JoinHorizontal(lipgloss.Top, m.mainView(), m.sidebarView())
2024-09-07 10:36:44 -07:00
top := strings.Repeat("\n", topPadding)
last := ""
if m.quitting {
last = "\n"
}
page := lipgloss.JoinVertical(lipgloss.Left, top, content, m.helpView(), last)
2024-09-07 15:23:46 -07:00
return lipgloss.PlaceHorizontal(m.width, lipgloss.Center, page)
2024-09-07 10:36:44 -07:00
}
func main() {
p := tea.NewProgram(
initialModel(),
tea.WithAltScreen(),
)
2024-09-07 10:36:44 -07:00
if _, err := p.Run(); err != nil {
fmt.Printf("There's been an error: %v", err)
os.Exit(1)
}
}
func sortMapKeys(m SoftwarePackages) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}