Skip to content

Commit

Permalink
Add menu in left view.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertClo committed Nov 17, 2024
1 parent 977ff42 commit 206ed69
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 56 deletions.
33 changes: 14 additions & 19 deletions keybind/keybind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
}
}
2 changes: 1 addition & 1 deletion layout/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func New(width, height int) *Layout {
width: width,
height: height,
leftPaneWidth: 30,
bottomHeight: 7,
bottomHeight: 8,
heightOffset: 1,
}
}
Expand Down
77 changes: 47 additions & 30 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -30,6 +28,8 @@ type Model struct {
quitting bool
err error
keys keybind.Shortcuts
menuItems []string
selected int
windowSize struct {
width int
height int
Expand All @@ -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
}
}

Expand All @@ -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
Expand All @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions view/bottom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
}
19 changes: 17 additions & 2 deletions view/left.go
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 1 addition & 1 deletion view/right.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Check failure on line 6 in view/right.go

View workflow job for this annotation

GitHub Actions / lint

printf: non-constant format string in call to fmt.Sprintf (govet)
}
2 changes: 2 additions & 0 deletions view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ type Model interface {
GetCounter() int
GetMessage() string
GetKeys() keybind.Shortcuts
GetMenuItems() []string
GetSelectedItem() int
}

0 comments on commit 206ed69

Please sign in to comment.