diff --git a/keybind/keybind.go b/keybind/keybind.go index 6918446..8341766 100644 --- a/keybind/keybind.go +++ b/keybind/keybind.go @@ -3,34 +3,29 @@ package keybind import "github.com/charmbracelet/bubbles/key" type Shortcuts struct { - Quit key.Binding - Increment key.Binding - Decrement key.Binding - Reset key.Binding - Start key.Binding + Up key.Binding + Down key.Binding + Enter key.Binding + Quit key.Binding } func New() Shortcuts { return Shortcuts{ - Quit: key.NewBinding( - key.WithKeys("q", "esc", "ctrl+c"), - key.WithHelp("q/esc/ctrl+c", "quit"), - ), - Increment: key.NewBinding( + Up: key.NewBinding( key.WithKeys("up", "k"), - key.WithHelp("↑/k", "increment"), + key.WithHelp("↑/k", "move up"), ), - Decrement: key.NewBinding( + Down: key.NewBinding( key.WithKeys("down", "j"), - key.WithHelp("↓/j", "decrement"), + key.WithHelp("↓/j", "move down"), ), - Reset: key.NewBinding( - key.WithKeys("r"), - key.WithHelp("r", "reset"), + Enter: key.NewBinding( + key.WithKeys("enter"), + key.WithHelp("enter", "select"), ), - Start: key.NewBinding( - key.WithKeys("s"), - key.WithHelp("s", "start"), + Quit: key.NewBinding( + key.WithKeys("q", "esc", "ctrl+c"), + key.WithHelp("q/esc/ctrl+c", "quit"), ), } } diff --git a/layout/layout.go b/layout/layout.go index 9850aff..e9408be 100644 --- a/layout/layout.go +++ b/layout/layout.go @@ -21,7 +21,7 @@ func New(width, height int) *Layout { width: width, height: height, leftPaneWidth: 30, - bottomHeight: 7, + bottomHeight: 8, heightOffset: 1, } } diff --git a/main.go b/main.go index e936243..6d03646 100644 --- a/main.go +++ b/main.go @@ -3,16 +3,14 @@ package main import ( "fmt" "github.com/AlbertClo/pylon/color" + "github.com/AlbertClo/pylon/keybind" "github.com/AlbertClo/pylon/layout" "github.com/AlbertClo/pylon/view" - "os" - "os/exec" - - "github.com/AlbertClo/pylon/keybind" "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/spinner" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" + "os" ) func main() { @@ -30,6 +28,8 @@ type Model struct { quitting bool err error keys keybind.Shortcuts + menuItems []string + selected int windowSize struct { width int height int @@ -48,15 +48,25 @@ func (m Model) GetKeys() keybind.Shortcuts { return m.keys } +func (m Model) GetMenuItems() []string { + return m.menuItems +} + +func (m Model) GetSelectedItem() int { + return m.selected +} + func initialModel() Model { s := spinner.New() s.Spinner = spinner.Dot s.Style = lipgloss.NewStyle().Foreground(color.Primary) return Model{ - spinner: s, - counter: 0, - message: "", - keys: keybind.New(), + spinner: s, + counter: 0, + message: "", + keys: keybind.New(), + menuItems: []string{"Start", "Stop", "Quit"}, + selected: 0, // Start with first item selected } } @@ -67,27 +77,35 @@ func (m Model) Init() tea.Cmd { func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: - if key.Matches(msg, m.keys.Quit) { + switch { + case key.Matches(msg, m.keys.Quit): m.quitting = true return m, tea.Quit - } - if key.Matches(msg, m.keys.Increment) { - m.counter++ - } - if key.Matches(msg, m.keys.Decrement) { - m.counter-- - } - if key.Matches(msg, m.keys.Reset) { - m.counter = 0 - } - if key.Matches(msg, m.keys.Start) { - cmd := exec.Command("touch", "test") - err := cmd.Run() - if err != nil { - m.err = err + case key.Matches(msg, m.keys.Up): + if m.selected > 0 { + m.selected-- + } else { + m.selected = len(m.menuItems) - 1 + } + return m, nil + case key.Matches(msg, m.keys.Down): + if m.selected < len(m.menuItems)-1 { + m.selected++ + } else { + m.selected = 0 + } + return m, nil + case key.Matches(msg, m.keys.Enter): + switch m.menuItems[m.selected] { + case "Start": + m.message = "Starting..." + case "Stop": + m.message = "Stopping..." + case "Quit": + m.quitting = true + return m, tea.Quit } } - return m, nil case tea.WindowSizeMsg: m.windowSize.width = msg.Width m.windowSize.height = msg.Height @@ -96,12 +114,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case error: m.err = msg return m, nil - - default: - var cmd tea.Cmd - m.spinner, cmd = m.spinner.Update(msg) - return m, cmd } + + var cmd tea.Cmd + m.spinner, cmd = m.spinner.Update(msg) + return m, cmd } func (m Model) View() string { diff --git a/view/bottom.go b/view/bottom.go index c005b6b..31b0882 100644 --- a/view/bottom.go +++ b/view/bottom.go @@ -5,13 +5,14 @@ import "fmt" func RenderBottomContent(m Model) string { keys := m.GetKeys() return fmt.Sprintf(`Controls: + %s %s %s %s`, + keys.Up.Help(), + keys.Down.Help(), + keys.Enter.Help(), keys.Quit.Help(), - keys.Increment.Help(), - keys.Decrement.Help(), - keys.Reset.Help(), ) } diff --git a/view/left.go b/view/left.go index 08e068d..e3f1fad 100644 --- a/view/left.go +++ b/view/left.go @@ -1,7 +1,22 @@ package view -import "fmt" +import ( + "fmt" + "strings" +) func RenderLeftContent(m Model) string { - return fmt.Sprintf(`Counter: %d`, m.GetCounter()) + var menuStr strings.Builder + + for i, item := range m.GetMenuItems() { + if i == m.GetSelectedItem() { + // Selected item + menuStr.WriteString(fmt.Sprintf("> %s\n", item)) + } else { + // Unselected item + menuStr.WriteString(fmt.Sprintf(" %s\n", item)) + } + } + + return menuStr.String() } diff --git a/view/right.go b/view/right.go index af05b6b..f7300e5 100644 --- a/view/right.go +++ b/view/right.go @@ -3,5 +3,5 @@ package view import "fmt" func RenderRightContent(m Model) string { - return fmt.Sprintf(`Window Size: %s`, m.GetMessage()) + return fmt.Sprintf(m.GetMessage()) } diff --git a/view/view.go b/view/view.go index 3063d11..9268988 100644 --- a/view/view.go +++ b/view/view.go @@ -8,4 +8,6 @@ type Model interface { GetCounter() int GetMessage() string GetKeys() keybind.Shortcuts + GetMenuItems() []string + GetSelectedItem() int }