more files

This commit is contained in:
Tyrel Souza 2022-10-08 23:10:26 -04:00
parent 881c6b4ded
commit e6394f12df
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E
3 changed files with 120 additions and 112 deletions

View File

@ -45,32 +45,6 @@ func (m *model) Init() tea.Cmd {
// Just return `nil`, which means "no I/O right now, please."
return nil
}
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
}
func (m *model) switchMode(mode Mode) {
// TODO: initialization stuff
@ -84,48 +58,6 @@ func (m *model) switchMode(mode Mode) {
}
}
func (m *model) updateSelect(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
// 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
// The "up" and "k" keys move the cursor up
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
// The "down" and "j" keys move the cursor down
case "down", "j":
if m.cursor < len(m.cards)-1 {
m.cursor++
}
case "d":
m.switchMode(Draw)
// The "enter" key and the spacebar (a literal space) toggle
// the selected state for the item that the cursor is pointing at.
case "enter", " ":
_, ok := m.selected[m.cursor]
if ok {
delete(m.selected, m.cursor)
} else {
m.selected[m.cursor] = struct{}{}
}
}
}
// Return the updated model to the Bubble Tea runtime for processing.
// Note that we're not returning a command.
return m, nil
}
func (m *model) cardListToString() string {
s := ""
for i, card := range m.cards {
@ -135,50 +67,6 @@ func (m *model) cardListToString() string {
}
return s
}
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
}
func Run() {
initial := initialModel()

72
gui/update.go Normal file
View File

@ -0,0 +1,72 @@
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
}
func (m *model) updateSelect(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
// 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
// The "up" and "k" keys move the cursor up
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
// The "down" and "j" keys move the cursor down
case "down", "j":
if m.cursor < len(m.cards)-1 {
m.cursor++
}
case "d":
m.switchMode(Draw)
// The "enter" key and the spacebar (a literal space) toggle
// the selected state for the item that the cursor is pointing at.
case "enter", " ":
_, ok := m.selected[m.cursor]
if ok {
delete(m.selected, m.cursor)
} else {
m.selected[m.cursor] = struct{}{}
}
}
}
// Return the updated model to the Bubble Tea runtime for processing.
// Note that we're not returning a command.
return m, nil
}

48
gui/view.go Normal file
View File

@ -0,0 +1,48 @@
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
}