From 6f8fd8e4da6b56295ba2127143aa5c3327af75b1 Mon Sep 17 00:00:00 2001 From: Kevin Miller Date: Tue, 20 Feb 2024 09:28:14 -0600 Subject: [PATCH] fix simple and debugging --- .vscode/launch.json | 4 +-- examples/simple/color/model.go | 21 ++++++++++------ examples/simple/main.go | 37 ++++++++++++++++++++++++++++ examples/simple/model.go | 45 ---------------------------------- 4 files changed, 52 insertions(+), 55 deletions(-) delete mode 100644 examples/simple/model.go diff --git a/.vscode/launch.json b/.vscode/launch.json index b29706d..96cbb0d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "debugAdapter": "dlv-dap", "request": "attach", "mode": "remote", - "remotePath": "${workspaceFolder}/examples/simple", + "remotePath": "${workspaceFolder}/examples/simple/", "port": 2345, "host": "127.0.0.1", "preLaunchTask": "Run simple headless dlv" // Here ! @@ -21,7 +21,7 @@ "debugAdapter": "dlv-dap", "request": "attach", "mode": "remote", - "remotePath": "${workspaceFolder}/examples/deeper", + "remotePath": "${workspaceFolder}/examples/deeper/", "port": 2345, "host": "127.0.0.1", "preLaunchTask": "Run deeper headless dlv" // Here ! diff --git a/examples/simple/color/model.go b/examples/simple/color/model.go index 6213425..88b9784 100644 --- a/examples/simple/color/model.go +++ b/examples/simple/color/model.go @@ -16,14 +16,19 @@ func (m Model) Init() tea.Cmd { } func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - - switch msg.(tea.KeyMsg).String() { - case "esc": - pop := cmdize(navstack.PopNavigation{}) - selected := cmdize(ColorSelected{m.RGB}) - return m, tea.Sequence(pop, selected) - case "ctrl+c": - return m, tea.Quit + switch msg := msg.(type) { + case tea.KeyMsg: + switch msg.String() { + case "esc": + pop := cmdize(navstack.PopNavigation{}) + return m, pop + case "enter": + pop := cmdize(navstack.PopNavigation{}) + selected := cmdize(ColorSelected{m.RGB}) + return m, tea.Sequence(pop, selected) + case "ctrl+c": + return m, tea.Quit + } } return m, nil diff --git a/examples/simple/main.go b/examples/simple/main.go index 13d0fcd..f100cb9 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -15,6 +15,43 @@ import ( var docStyle = lipgloss.NewStyle() +type model struct { + SelectedColor string + menu menu.Model + window *window.Model +} + +func (m model) Init() tea.Cmd { + return nil +} + +func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case color.ColorSelected: + m.SelectedColor = msg.RGB + return m, tea.Quit + case tea.KeyMsg: + if msg.String() == "ctrl+c" { + return m, tea.Quit + } + case tea.WindowSizeMsg: + h, v := docStyle.GetFrameSize() + m.window.Height = msg.Height + m.window.Width = msg.Width - h + m.window.TopOffset = v + m.menu.SetSize(m.window) + return m, nil + } + + updatedmenu, cmd := m.menu.Update(msg) + m.menu = updatedmenu.(menu.Model) + return m, cmd +} + +func (m model) View() string { + return docStyle.Render(m.menu.View()) +} + func main() { red := menu.Choice{ Title: "Red Envy", diff --git a/examples/simple/model.go b/examples/simple/model.go deleted file mode 100644 index b6afb1e..0000000 --- a/examples/simple/model.go +++ /dev/null @@ -1,45 +0,0 @@ -package main - -import ( - tea "github.com/charmbracelet/bubbletea" - "github.com/kevm/bubbleo/examples/simple/color" - "github.com/kevm/bubbleo/menu" - "github.com/kevm/bubbleo/window" -) - -type model struct { - SelectedColor string - menu menu.Model - window *window.Model -} - -func (m model) Init() tea.Cmd { - return nil -} - -func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - switch msg := msg.(type) { - case color.ColorSelected: - m.SelectedColor = msg.RGB - return m, tea.Quit - case tea.KeyMsg: - if msg.String() == "ctrl+c" { - return m, tea.Quit - } - case tea.WindowSizeMsg: - h, v := docStyle.GetFrameSize() - m.window.Height = msg.Height - m.window.Width = msg.Width - h - m.window.TopOffset = v - m.menu.SetSize(m.window) - return m, nil - } - - updatedmenu, cmd := m.menu.Update(msg) - m.menu = updatedmenu.(menu.Model) - return m, cmd -} - -func (m model) View() string { - return docStyle.Render(m.menu.View()) -}