hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/internal/cli/commands.go
blob: f50036c28b3222279386e3f87249aa2bd055812d (plain)
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
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: "check",
			desc: "Check whether the configured mods are compatible",
			run:  runCheck,
		},
		{
			name: "generate",
			desc: "Download the server and mods and generate a server directory",
			run:  runGenerate,
		},
		{
			name: "merge",
			desc: "Back up the existing server and merge the generated files into it",
			run:  runMerge,
		},
		{
			name: "clean",
			desc: "Remove generated files and/or the latest backup (generated|latest|all)",
			run:  runClean,
		},
		{
			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 {
	header := lipgloss.JoinHorizontal(
		lipgloss.Bottom,
		titleStyle.Render("BENSON"),
		subtitleStyle.Render("  v"+version),
	)

	inner := lipgloss.JoinVertical(
		lipgloss.Left,
		header,
		mutedStyle.Render("Fabric · MC "+cfg.MinecraftVersion),
	)

	hint := mutedStyle.Render("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
}