diff options
| author | hachem <im@hachem.wtf> | 2026-08-29 02:51:16 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-08-29 02:51:16 +0200 |
| commit | f60177934a18daea3b24a37cf109bb2c7bfaaa72 (patch) | |
| tree | 7b6e20dd7b5c94b9c176e75497c5045dcb589075 | |
| parent | e56d21fa04450d118f76347a96a12d1d5477958f (diff) | |
feat: nicer ui
| -rw-r--r-- | go.mod | 32 | ||||
| -rw-r--r-- | go.sum | 52 | ||||
| -rw-r--r-- | internal/bot/bot.go | 5 | ||||
| -rw-r--r-- | internal/cli/cli.go | 199 | ||||
| -rw-r--r-- | internal/cli/commands.go | 77 | ||||
| -rw-r--r-- | internal/cli/styles.go | 40 | ||||
| -rw-r--r-- | internal/config/config.go | 45 | ||||
| -rw-r--r-- | main.go | 13 |
8 files changed, 440 insertions, 23 deletions
@@ -1,3 +1,33 @@ -module github.com/le-hachem/benson +module github.com/hachem-wtf/benson go 1.26.6 + +require ( + github.com/charmbracelet/bubbles v1.0.0 + github.com/charmbracelet/bubbletea v1.3.10 + github.com/charmbracelet/lipgloss v1.1.0 +) + +require ( + github.com/atotto/clipboard v0.1.4 // indirect + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/charmbracelet/colorprofile v0.4.1 // indirect + github.com/charmbracelet/x/ansi v0.11.6 // indirect + github.com/charmbracelet/x/cellbuf v0.0.15 // indirect + github.com/charmbracelet/x/term v0.2.2 // indirect + github.com/clipperhouse/displaywidth v0.9.0 // indirect + github.com/clipperhouse/stringish v0.1.1 // indirect + github.com/clipperhouse/uax29/v2 v2.5.0 // indirect + github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect + github.com/lucasb-eyer/go-colorful v1.3.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-localereader v0.0.1 // indirect + github.com/mattn/go-runewidth v0.0.19 // indirect + github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect + github.com/muesli/cancelreader v0.2.2 // indirect + github.com/muesli/termenv v0.16.0 // indirect + github.com/rivo/uniseg v0.4.7 // indirect + github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect + golang.org/x/sys v0.38.0 // indirect + golang.org/x/text v0.3.8 // indirect +) @@ -0,0 +1,52 @@ +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/charmbracelet/bubbles v1.0.0 h1:12J8/ak/uCZEMQ6KU7pcfwceyjLlWsDLAxB5fXonfvc= +github.com/charmbracelet/bubbles v1.0.0/go.mod h1:9d/Zd5GdnauMI5ivUIVisuEm3ave1XwXtD1ckyV6r3E= +github.com/charmbracelet/bubbletea v1.3.10 h1:otUDHWMMzQSB0Pkc87rm691KZ3SWa4KUlvF9nRvCICw= +github.com/charmbracelet/bubbletea v1.3.10/go.mod h1:ORQfo0fk8U+po9VaNvnV95UPWA1BitP1E0N6xJPlHr4= +github.com/charmbracelet/colorprofile v0.4.1 h1:a1lO03qTrSIRaK8c3JRxJDZOvhvIeSco3ej+ngLk1kk= +github.com/charmbracelet/colorprofile v0.4.1/go.mod h1:U1d9Dljmdf9DLegaJ0nGZNJvoXAhayhmidOdcBwAvKk= +github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY= +github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30= +github.com/charmbracelet/x/ansi v0.11.6 h1:GhV21SiDz/45W9AnV2R61xZMRri5NlLnl6CVF7ihZW8= +github.com/charmbracelet/x/ansi v0.11.6/go.mod h1:2JNYLgQUsyqaiLovhU2Rv/pb8r6ydXKS3NIttu3VGZQ= +github.com/charmbracelet/x/cellbuf v0.0.15 h1:ur3pZy0o6z/R7EylET877CBxaiE1Sp1GMxoFPAIztPI= +github.com/charmbracelet/x/cellbuf v0.0.15/go.mod h1:J1YVbR7MUuEGIFPCaaZ96KDl5NoS0DAWkskup+mOY+Q= +github.com/charmbracelet/x/term v0.2.2 h1:xVRT/S2ZcKdhhOuSP4t5cLi5o+JxklsoEObBSgfgZRk= +github.com/charmbracelet/x/term v0.2.2/go.mod h1:kF8CY5RddLWrsgVwpw4kAa6TESp6EB5y3uxGLeCqzAI= +github.com/clipperhouse/displaywidth v0.9.0 h1:Qb4KOhYwRiN3viMv1v/3cTBlz3AcAZX3+y9OLhMtAtA= +github.com/clipperhouse/displaywidth v0.9.0/go.mod h1:aCAAqTlh4GIVkhQnJpbL0T/WfcrJXHcj8C0yjYcjOZA= +github.com/clipperhouse/stringish v0.1.1 h1:+NSqMOr3GR6k1FdRhhnXrLfztGzuG+VuFDfatpWHKCs= +github.com/clipperhouse/stringish v0.1.1/go.mod h1:v/WhFtE1q0ovMta2+m+UbpZ+2/HEXNWYXQgCt4hdOzA= +github.com/clipperhouse/uax29/v2 v2.5.0 h1:x7T0T4eTHDONxFJsL94uKNKPHrclyFI0lm7+w94cO8U= +github.com/clipperhouse/uax29/v2 v2.5.0/go.mod h1:Wn1g7MK6OoeDT0vL+Q0SQLDz/KpfsVRgg6W7ihQeh4g= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= +github.com/lucasb-eyer/go-colorful v1.3.0 h1:2/yBRLdWBZKrf7gB40FoiKfAWYQ0lqNcbuQwVHXptag= +github.com/lucasb-eyer/go-colorful v1.3.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= +github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= +github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw= +github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= +github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc= +github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= +golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc= +golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= diff --git a/internal/bot/bot.go b/internal/bot/bot.go index 31c776d..1c8182d 100644 --- a/internal/bot/bot.go +++ b/internal/bot/bot.go @@ -1,12 +1,7 @@ -// Package bot implements Benson's Discord bot, which periodically checks -// configured Modrinth mods for compatibility and posts reports to a channel. package bot import "fmt" -// Run starts the Discord bot and blocks until it is stopped. -// -// TODO: port the Modrinth compatibility checker and Discord integration. func Run() error { fmt.Println("benson bot — not yet implemented") return nil diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 337cb71..4e394e6 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -1,12 +1,199 @@ -// Package cli implements Benson's interactive server-manager REPL. package cli -import "fmt" +import ( + "fmt" + "strings" + + "github.com/charmbracelet/bubbles/spinner" + "github.com/charmbracelet/bubbles/textinput" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" + + "github.com/hachem-wtf/benson/internal/config" +) + +type command struct { + name string + desc string + run func(m *model) tea.Cmd +} + +type outputMsg struct { + text string +} + +type model struct { + cfg *config.Config + version string + + input textinput.Model + spin spinner.Model + + commands []command + busy bool + quitting bool + + width int +} -// Run starts the interactive CLI. version is displayed in the banner. -// -// TODO: port the REPL and commands (list-mods, check, generate, merge, clean). func Run(version string) error { - fmt.Printf("benson CLI v%s — not yet implemented\n", version) + cfg, err := config.Load() + if err != nil { + return fmt.Errorf("failed to load %s: %w", config.Path(), err) + } + + _, err = tea.NewProgram(newModel(cfg, version)).Run() + return err +} + +func newModel(cfg *config.Config, version string) *model { + ti := textinput.New() + ti.Placeholder = "type a command… (try 'help')" + ti.Prompt = "" + ti.CharLimit = 128 + ti.Focus() + + sp := spinner.New() + sp.Spinner = spinner.Dot + sp.Style = lipgloss.NewStyle().Foreground(colPrimary) + + m := &model{ + cfg: cfg, + version: version, + input: ti, + spin: sp, + } + m.commands = defaultCommands() + return m +} + +func (m *model) Init() tea.Cmd { + return tea.Batch(textinput.Blink, tea.Println(welcomeBlock(m.cfg, m.version))) +} + +func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.WindowSizeMsg: + m.width = msg.Width + m.input.Width = max(msg.Width-12, 20) + return m, nil + + case tea.KeyMsg: + switch msg.Type { + case tea.KeyCtrlC, tea.KeyCtrlD: + m.quitting = true + return m, tea.Quit + case tea.KeyTab: + if s := m.firstSuggestion(); s != "" { + m.input.SetValue(s) + m.input.CursorEnd() + } + return m, nil + case tea.KeyEnter: + if m.busy { + return m, nil + } + return m, m.submit() + } + + case outputMsg: + m.busy = false + return m, tea.Println(msg.text) + + case spinner.TickMsg: + var cmd tea.Cmd + m.spin, cmd = m.spin.Update(msg) + return m, cmd + } + + var cmd tea.Cmd + m.input, cmd = m.input.Update(msg) + return m, cmd +} + +func (m *model) View() string { + if m.quitting { + return "" + } + return "\n" + m.suggestionRow() + "\n" + m.promptLine() + "\n" +} + +func (m *model) submit() tea.Cmd { + raw := strings.TrimSpace(m.input.Value()) + m.input.Reset() + if raw == "" { + return nil + } + + echo := tea.Println(promptStyle.Render("benson ❯ ") + raw) + + name := strings.ToLower(strings.Fields(raw)[0]) + if name == "?" { + name = "help" + } + + if isExit(name) { + m.quitting = true + return tea.Sequence(echo, tea.Println(mutedStyle.Render("Goodbye!")), tea.Quit) + } + + if c := m.find(name); c != nil { + return tea.Sequence(echo, c.run(m)) + } + + unknown := tea.Println(errorStyle.Render("Unknown command: "+name) + mutedStyle.Render(" · type 'help'")) + return tea.Sequence(echo, unknown) +} + +func (m *model) find(name string) *command { + for i := range m.commands { + if m.commands[i].name == name { + return &m.commands[i] + } + } return nil } + +func isExit(name string) bool { + switch name { + case "exit", "quit", "q": + return true + } + return false +} + +func (m *model) promptLine() string { + if m.busy { + return " " + m.spin.View() + " " + mutedStyle.Render("working…") + } + return promptStyle.Render(" benson ❯ ") + m.input.View() +} + +func (m *model) suggestionRow() string { + cur := strings.ToLower(strings.TrimSpace(m.input.Value())) + + chips := make([]string, 0, len(m.commands)+1) + for _, c := range m.commands { + if cur != "" && strings.HasPrefix(c.name, cur) { + chips = append(chips, chipActiveStyle.Render(c.name)) + } else { + chips = append(chips, chipStyle.Render(c.name)) + } + } + chips = append(chips, chipStyle.Render("exit")) + + return " " + lipgloss.JoinHorizontal(lipgloss.Left, chips...) +} + +func (m *model) firstSuggestion() string { + cur := strings.ToLower(strings.TrimSpace(m.input.Value())) + if cur == "" { + return "" + } + for _, c := range m.commands { + if c.name != cur && strings.HasPrefix(c.name, cur) { + return c.name + } + } + return "" +} diff --git a/internal/cli/commands.go b/internal/cli/commands.go new file mode 100644 index 0000000..1f1f132 --- /dev/null +++ b/internal/cli/commands.go @@ -0,0 +1,77 @@ +package cli + +import ( + "fmt" + "strings" + + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" + + "github.com/hachem-wtf/benson/internal/config" +) + +func defaultCommands() []command { + return []command{ + { + name: "list-mods", + desc: "List the configured mods", + run: func(m *model) tea.Cmd { + return tea.Println(renderModList(m.cfg)) + }, + }, + { + name: "help", + desc: "Show available commands", + run: func(m *model) tea.Cmd { + return tea.Println(helpBlock(m.commands)) + }, + }, + } +} + +func welcomeBlock(cfg *config.Config, version string) string { + inner := lipgloss.JoinVertical( + lipgloss.Left, + titleStyle.Render("BENSON"), + subtitleStyle.Render("a Minecraft server manager from hell"), + "", + mutedStyle.Render(fmt.Sprintf("Fabric · MC %s · v%s", cfg.MinecraftVersion, version)), + ) + + hint := mutedStyle.Render("Type a command below. Tab autocompletes · Ctrl+C quits.") + + return lipgloss.JoinVertical(lipgloss.Left, bannerStyle.Render(inner), "", hint) +} + +func helpBlock(cmds []command) string { + rows := []string{headingStyle.Render("Commands"), ""} + for _, c := range cmds { + rows = append(rows, " "+bulletStyle.Render(padRight(c.name, 14))+mutedStyle.Render(c.desc)) + } + rows = append(rows, " "+bulletStyle.Render(padRight("exit", 14))+mutedStyle.Render("Exit Benson")) + return lipgloss.JoinVertical(lipgloss.Left, rows...) +} + +func renderModList(cfg *config.Config) string { + rows := []string{ + headingStyle.Render(fmt.Sprintf("Mods for MC %s", cfg.MinecraftVersion)), + "", + } + + if len(cfg.ModrinthMods) == 0 { + rows = append(rows, mutedStyle.Render(" (none configured)")) + } + for _, slug := range cfg.ModrinthMods { + rows = append(rows, " "+bulletStyle.Render("◆ ")+slug) + } + + rows = append(rows, "", mutedStyle.Render(fmt.Sprintf("Total: %d mods", len(cfg.ModrinthMods)))) + return lipgloss.JoinVertical(lipgloss.Left, rows...) +} + +func padRight(s string, n int) string { + if pad := n - len([]rune(s)); pad > 0 { + return s + strings.Repeat(" ", pad) + } + return s +} diff --git a/internal/cli/styles.go b/internal/cli/styles.go new file mode 100644 index 0000000..c0a61e5 --- /dev/null +++ b/internal/cli/styles.go @@ -0,0 +1,40 @@ +package cli + +import "github.com/charmbracelet/lipgloss" + +var ( + colPrimary = lipgloss.Color("#F97316") + colAccent = lipgloss.Color("#A855F7") + colSuccess = lipgloss.Color("#22C55E") + colError = lipgloss.Color("#EF4444") + colWarn = lipgloss.Color("#EAB308") + colMuted = lipgloss.Color("#6B7280") + colInk = lipgloss.Color("#111827") + + bannerStyle = lipgloss.NewStyle(). + Border(lipgloss.RoundedBorder()). + BorderForeground(colPrimary). + Padding(0, 3) + + titleStyle = lipgloss.NewStyle().Bold(true).Foreground(colPrimary) + subtitleStyle = lipgloss.NewStyle().Foreground(colMuted) + + promptStyle = lipgloss.NewStyle().Bold(true).Foreground(colAccent) + + chipStyle = lipgloss.NewStyle(). + Foreground(colMuted). + Padding(0, 1) + + chipActiveStyle = lipgloss.NewStyle(). + Foreground(colInk). + Background(colPrimary). + Bold(true). + Padding(0, 1) + + headingStyle = lipgloss.NewStyle().Bold(true).Foreground(colAccent) + bulletStyle = lipgloss.NewStyle().Foreground(colPrimary) + mutedStyle = lipgloss.NewStyle().Foreground(colMuted) + successStyle = lipgloss.NewStyle().Foreground(colSuccess) + errorStyle = lipgloss.NewStyle().Foreground(colError) + warnStyle = lipgloss.NewStyle().Foreground(colWarn) +) diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..35842a1 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,45 @@ +package config + +import ( + "encoding/json" + "os" + "path/filepath" +) + +type Config struct { + ChannelID string `json:"channel_id"` + CheckIntervalMinutes int `json:"check_interval_minutes"` + MinecraftVersion string `json:"minecraft_version"` + FabricLoaderVersion string `json:"fabric_loader_version"` + LoaderMetaVersion string `json:"loader_meta_version"` + JavaPath string `json:"java_path"` + ServerLocation string `json:"server_location"` + BackupLocation string `json:"backup_location"` + MergePreCommand string `json:"merge_pre_command"` + MergePostCommand string `json:"merge_post_command"` + ModrinthMods []string `json:"modrinth_mods"` +} + +const Dir = "config" + +func Path() string { + return filepath.Join(Dir, "config.json") +} + +func APIPath() string { + return filepath.Join(Dir, "api.json") +} + +func Load() (*Config, error) { + data, err := os.ReadFile(Path()) + if err != nil { + return nil, err + } + + var c Config + if err := json.Unmarshal(data, &c); err != nil { + return nil, err + } + + return &c, nil +} @@ -1,22 +1,13 @@ -// Command benson is a Minecraft server manager from hell. -// -// It is a single binary that exposes two modes: -// -// benson Start the interactive server-manager CLI -// benson bot Run the Discord bot -// -// Both modes share the same configuration and Modrinth client. package main import ( "fmt" "os" - "github.com/le-hachem/benson/internal/bot" - "github.com/le-hachem/benson/internal/cli" + "github.com/hachem-wtf/benson/internal/bot" + "github.com/hachem-wtf/benson/internal/cli" ) -// version is the Benson release version. const version = "1.1.0" func usage() { |
