mirror of
https://github.com/0x1d/rcond.git
synced 2025-12-16 02:48:46 +01:00
feat: basic configuration UI
This commit is contained in:
179
tea.go
Normal file
179
tea.go
Normal file
@@ -0,0 +1,179 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/huh"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
type model struct {
|
||||
Tabs []string
|
||||
TabContent []string
|
||||
activeTab int
|
||||
form *huh.Form
|
||||
}
|
||||
|
||||
func (m model) Init() tea.Cmd {
|
||||
return m.form.Init()
|
||||
}
|
||||
|
||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch keypress := msg.String(); keypress {
|
||||
case "ctrl+c", "q":
|
||||
return m, tea.Quit
|
||||
case "right", "l", "n", "tab":
|
||||
m.activeTab = min(m.activeTab+1, len(m.Tabs)-1)
|
||||
return m, nil
|
||||
case "left", "h", "p", "shift+tab":
|
||||
m.activeTab = max(m.activeTab-1, 0)
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
||||
var cmds []tea.Cmd
|
||||
|
||||
// Process the form
|
||||
form, cmd := m.form.Update(msg)
|
||||
if f, ok := form.(*huh.Form); ok {
|
||||
m.form = f
|
||||
cmds = append(cmds, cmd)
|
||||
}
|
||||
|
||||
if m.form.State == huh.StateCompleted {
|
||||
// Quit when the form is done.
|
||||
cmds = append(cmds, tea.Quit)
|
||||
}
|
||||
|
||||
return m, tea.Batch(cmds...)
|
||||
}
|
||||
|
||||
func tabBorderWithBottom(left, middle, right string) lipgloss.Border {
|
||||
border := lipgloss.RoundedBorder()
|
||||
border.BottomLeft = left
|
||||
border.Bottom = middle
|
||||
border.BottomRight = right
|
||||
return border
|
||||
}
|
||||
|
||||
var (
|
||||
inactiveTabBorder = tabBorderWithBottom("┴", "─", "┴")
|
||||
activeTabBorder = tabBorderWithBottom("┘", " ", "└")
|
||||
docStyle = lipgloss.NewStyle().Padding(1, 2, 1, 2)
|
||||
highlightColor = lipgloss.AdaptiveColor{Light: "#874BFD", Dark: "#7D56F4"}
|
||||
inactiveTabStyle = lipgloss.NewStyle().Border(inactiveTabBorder, true).BorderForeground(highlightColor).Padding(0, 1)
|
||||
activeTabStyle = inactiveTabStyle.Border(activeTabBorder, true)
|
||||
windowStyle = lipgloss.NewStyle().BorderForeground(highlightColor).Padding(2, 0).Align(lipgloss.Center).Border(lipgloss.NormalBorder()).UnsetBorderTop()
|
||||
)
|
||||
|
||||
func (m model) View() string {
|
||||
doc := strings.Builder{}
|
||||
|
||||
var renderedTabs []string
|
||||
|
||||
for i, t := range m.Tabs {
|
||||
var style lipgloss.Style
|
||||
isFirst, isLast, isActive := i == 0, i == len(m.Tabs)-1, i == m.activeTab
|
||||
if isActive {
|
||||
style = activeTabStyle
|
||||
} else {
|
||||
style = inactiveTabStyle
|
||||
}
|
||||
border, _, _, _, _ := style.GetBorder()
|
||||
if isFirst && isActive {
|
||||
border.BottomLeft = "│"
|
||||
} else if isFirst && !isActive {
|
||||
border.BottomLeft = "├"
|
||||
} else if isLast && isActive {
|
||||
border.BottomRight = "│"
|
||||
} else if isLast && !isActive {
|
||||
border.BottomRight = "┤"
|
||||
}
|
||||
style = style.Border(border)
|
||||
renderedTabs = append(renderedTabs, style.Render(t))
|
||||
}
|
||||
|
||||
row := lipgloss.JoinHorizontal(lipgloss.Top, renderedTabs...)
|
||||
doc.WriteString(row)
|
||||
doc.WriteString("\n")
|
||||
|
||||
if m.activeTab == 0 {
|
||||
doc.WriteString(windowStyle.Width((lipgloss.Width(row) - windowStyle.GetHorizontalFrameSize())).Render(m.form.View()))
|
||||
}
|
||||
|
||||
return docStyle.Render(doc.String())
|
||||
}
|
||||
|
||||
func main() {
|
||||
tabs := []string{"Access Point Configuration"}
|
||||
m := model{
|
||||
Tabs: tabs,
|
||||
form: huh.NewForm(
|
||||
huh.NewGroup(
|
||||
huh.NewInput().
|
||||
Key("ssid").
|
||||
Placeholder("Access Point SSID").
|
||||
Title("SSID").
|
||||
Description("The name of your access point"),
|
||||
|
||||
huh.NewInput().
|
||||
Key("password").
|
||||
Placeholder("Access Point Password").
|
||||
Title("Password").
|
||||
EchoMode(huh.EchoModePassword).
|
||||
Description("The password for your access point").
|
||||
Validate(func(v string) error {
|
||||
if len(v) < 8 {
|
||||
return fmt.Errorf("password must be at least 8 characters long")
|
||||
}
|
||||
return nil
|
||||
}),
|
||||
|
||||
huh.NewSelect[string]().
|
||||
Key("encryption").
|
||||
Options(huh.NewOptions("WPA2", "WPA3", "WEP")...).
|
||||
Title("Encryption").
|
||||
Description("The encryption method for your access point"),
|
||||
|
||||
huh.NewConfirm().
|
||||
Key("done").
|
||||
Title("All done?").
|
||||
Validate(func(v bool) error {
|
||||
if !v {
|
||||
return fmt.Errorf("Welp, finish up then")
|
||||
}
|
||||
return nil
|
||||
}).
|
||||
Affirmative("Yep").
|
||||
Negative("Wait, no"),
|
||||
),
|
||||
).
|
||||
WithWidth(45).
|
||||
WithShowHelp(false).
|
||||
WithShowErrors(false),
|
||||
}
|
||||
|
||||
if _, err := tea.NewProgram(m, tea.WithAltScreen()).Run(); err != nil {
|
||||
fmt.Println("Error running program:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
Reference in New Issue
Block a user