From 9920adf3ede8e3962b5842bdcf208929924c5a7e Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Sat, 8 Oct 2022 23:18:46 -0400 Subject: [PATCH] split by mode --- gui/create.go | 25 ++++++++++++++ gui/draw.go | 25 ++++++++++++++ gui/gui.go | 19 +++++++++++ gui/{update.go => select.go} | 66 ++++++++++++++++++++---------------- gui/view.go | 48 -------------------------- 5 files changed, 105 insertions(+), 78 deletions(-) create mode 100644 gui/create.go create mode 100644 gui/draw.go rename gui/{update.go => select.go} (56%) delete mode 100644 gui/view.go diff --git a/gui/create.go b/gui/create.go new file mode 100644 index 0000000..a1a7db0 --- /dev/null +++ b/gui/create.go @@ -0,0 +1,25 @@ +package gui + +import tea "github.com/charmbracelet/bubbletea" + +func (m *model) updateCreate(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + + // Is it a key press? + case tea.KeyMsg: + switch msg.String() { + case "ctrl+c", "q": + return m, tea.Quit + case "esc": + m.switchMode(Select) + } + } + + return m, nil +} + +func (m *model) viewCreate() string { + s := "Create a card:\n\n" + + return s +} diff --git a/gui/draw.go b/gui/draw.go new file mode 100644 index 0000000..4085ea2 --- /dev/null +++ b/gui/draw.go @@ -0,0 +1,25 @@ +package gui + +import tea "github.com/charmbracelet/bubbletea" + +func (m *model) updateDraw(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + + // Is it a key press? + case tea.KeyMsg: + switch msg.String() { + case "ctrl+c", "q": + return m, tea.Quit + case "s": + m.switchMode(Select) + } + } + return m, nil +} +func (m *model) viewDraw() string { + s := "Draw Mode:\n\n" + + s += m.cardListToString() + + return s +} diff --git a/gui/gui.go b/gui/gui.go index 94bb684..6647109 100644 --- a/gui/gui.go +++ b/gui/gui.go @@ -68,6 +68,25 @@ func (m *model) cardListToString() string { return s } +func (m *model) View() string { + if m.mode == Draw { + return m.viewDraw() + } else if m.mode == Create { + return m.viewCreate() + } + // Select is default mode + return m.viewSelect() +} + +func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + if m.mode == Draw { + return m.updateDraw(msg) + } else if m.mode == Create { + return m.updateCreate(msg) + } + // Select is default mode + return m.updateSelect(msg) +} func Run() { initial := initialModel() p := tea.NewProgram(&initial) diff --git a/gui/update.go b/gui/select.go similarity index 56% rename from gui/update.go rename to gui/select.go index b85045d..ae425fb 100644 --- a/gui/update.go +++ b/gui/select.go @@ -1,33 +1,9 @@ package gui -import tea "github.com/charmbracelet/bubbletea" - -func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - if m.mode == Draw { - return m.updateDraw(msg) - } - // Select is default mode - return m.updateSelect(msg) -} - -func (m *model) updateDraw(msg tea.Msg) (tea.Model, tea.Cmd) { - switch msg := msg.(type) { - - // Is it a key press? - case tea.KeyMsg: - switch msg.String() { - - case "s": - m.switchMode(Select) - - // These keys should exit the program. - case "ctrl+c", "q": - return m, tea.Quit - } - } - - return m, nil -} +import ( + "fmt" + tea "github.com/charmbracelet/bubbletea" +) func (m *model) updateSelect(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { @@ -35,8 +11,6 @@ func (m *model) updateSelect(msg tea.Msg) (tea.Model, tea.Cmd) { // Is it a key press? case tea.KeyMsg: switch msg.String() { - - // These keys should exit the program. case "ctrl+c", "q": return m, tea.Quit @@ -51,8 +25,12 @@ func (m *model) updateSelect(msg tea.Msg) (tea.Model, tea.Cmd) { if m.cursor < len(m.cards)-1 { m.cursor++ } + + // MODES case "d": m.switchMode(Draw) + case "c": + m.switchMode(Create) // The "enter" key and the spacebar (a literal space) toggle // the selected state for the item that the cursor is pointing at. @@ -70,3 +48,31 @@ func (m *model) updateSelect(msg tea.Msg) (tea.Model, tea.Cmd) { // Note that we're not returning a command. return m, nil } +func (m *model) viewSelect() string { + s := "Select cards to query:\n\n" + + // Iterate over our choices + for i, choice := range m.cards { + + // Is the cursor pointing at this choice? + cursor := " " // no cursor + if m.cursor == i { + cursor = ">" // cursor! + } + + // Is this choice selected? + checked := " " // not selected + if _, ok := m.selected[i]; ok { + checked = "✓" // selected! + } + + // Render the row + s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice.Front) + } + + // The footer + s += "\nPress c to Create (_unimplemented_).\nPress d to Draw.\nPress q to quit.\n" + + // Send the UI for rendering + return s +} diff --git a/gui/view.go b/gui/view.go deleted file mode 100644 index cb47922..0000000 --- a/gui/view.go +++ /dev/null @@ -1,48 +0,0 @@ -package gui - -import "fmt" - -func (m *model) View() string { - if m.mode == Draw { - return m.viewDraw() - } - // Select is default mode - return m.viewSelect() -} - -func (m *model) viewDraw() string { - s := "Draw Mode:\n\n" - - s += m.cardListToString() - - return s -} - -func (m *model) viewSelect() string { - s := "Select cards to query:\n\n" - - // Iterate over our choices - for i, choice := range m.cards { - - // Is the cursor pointing at this choice? - cursor := " " // no cursor - if m.cursor == i { - cursor = ">" // cursor! - } - - // Is this choice selected? - checked := " " // not selected - if _, ok := m.selected[i]; ok { - checked = "✓" // selected! - } - - // Render the row - s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice.Front) - } - - // The footer - s += "\nPress c to Create (_unimplemented_).\nPress d to Draw.\nPress q to quit.\n" - - // Send the UI for rendering - return s -}