Skip to content

Commit

Permalink
Fix an issue with BubbleTea commands execution order
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea committed Mar 14, 2024
1 parent 7f0d8d0 commit da25a85
Showing 1 changed file with 49 additions and 22 deletions.
71 changes: 49 additions & 22 deletions examples/starwars/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"
"os"
"strings"

Expand Down Expand Up @@ -42,16 +43,16 @@ const characterTpl = `
// model represents the application state.
type model struct {
group *group.Model
statusBar *statusbar.Model
table *table.Model
viewport *viewport.Model
statusBar *statusbar.Model
spinner spinner.Model
renderer *glamour.TermRenderer
document *layout.Layout

bindings *keyBindings

characters map[string]character
characters []character

api *swDbApi
state int
Expand Down Expand Up @@ -164,7 +165,7 @@ func initialModel() model {
document: document,
renderer: renderer,
bindings: bindings,
characters: make(map[string]character),
characters: make([]character, 0),
api: &swDbApi{1, 20},
}
}
Expand Down Expand Up @@ -208,15 +209,18 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit

}

case onlineMsg:
cmds = append(cmds, m.api.getCharacters, m.spinner.Tick)
m.state = DownloadingState

case charactersResponseMsg:
log.Println("Got Response")
m.state = GotResponseState

}

return m, tea.Batch(append(cmds, m.handleState(msg, cmds)...)...)
return m, tea.Batch(m.handleState(msg, cmds)...)
}

// View renders the application UI.
Expand All @@ -225,14 +229,27 @@ func (m model) View() string {
}

// handleState updates the application state based on the current state.
func (m model) handleState(msg tea.Msg, cmds []tea.Cmd) []tea.Cmd {
func (m *model) handleState(msg tea.Msg, cmds []tea.Cmd) []tea.Cmd {
_, cmd := m.group.Update(msg)
switch m.state {
case BrowseState:
m.updateViewport()
currRow := m.table.Model.Cursor() + 1
if currRow < len(m.characters) {
browseStatus := fmt.Sprintf(
"Browse the results using arrow keys - Item %d/%d - Page %d",
currRow,
len(m.characters),
m.api.page,
)
m.statusBar.SetContent("Browse 📖", browseStatus, "API 🟢")
} else {
m.api.page++
cmds = append(cmds, m.api.ping)
}

case GotResponseState:
m.setTableRows(msg.(charactersResponseMsg).Data)
m.updateTableRows(msg.(charactersResponseMsg).Data)
m.updateViewport()
m.statusBar.SetContent("Browse 📖", "Browse the results using arrow keys", "API 🟢")
m.state = BrowseState
Expand All @@ -249,32 +266,42 @@ func (m model) handleState(msg tea.Msg, cmds []tea.Cmd) []tea.Cmd {

// updateViewport updates the viewport with the selected character's details.
func (m model) updateViewport() {
if currentRow := m.table.SelectedRow(); currentRow != nil {
currentId := currentRow[0]
character := m.characters[currentId]

// FIXME: The use of a standard '-' character causes rendering
// issues within the viewport. Further investigation is
// required to resolve this issue.
description := strings.Replace(character.Description, "-", "–", -1)

md, _ := m.renderer.Render(fmt.Sprintf(characterTpl, character.ID, character.Name, description))
m.viewport.SetContent(md)
}
character := m.characters[m.table.Cursor()]
md, _ := m.renderer.Render(
fmt.Sprintf(
characterTpl,
character.ID,
sanitize(character.Name),
sanitize(character.Description),
),
)
m.viewport.SetContent(md)
}

// setTableRows sets the table rows with the provided character data.
func (m model) setTableRows(data []character) {
func (m *model) updateTableRows(data []character) {
for _, c := range data {
m.characters = append(m.characters, c)
}

rows := make([]btTable.Row, 0)

for _, c := range data {
rows = append(rows, btTable.Row{c.ID, c.Name})
m.characters[c.ID] = c
for _, character := range m.characters {
rows = append(rows, btTable.Row{character.ID, sanitize(character.Name)})
}

log.Println(len(rows))

m.table.Model.SetRows(rows)
}

func sanitize(text string) string {
// FIXME: The use of a standard '-' character causes rendering
// issues within the viewport. Further investigation is
// required to resolve this problem.
return strings.Replace(text, "-", "–", -1)
}

// main is the entry point of the application.
func main() {
if len(os.Getenv("DEBUG")) > 0 {
Expand Down

0 comments on commit da25a85

Please sign in to comment.