Skip to content

Commit

Permalink
Add comments in starwars/main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea committed Mar 13, 2024
1 parent ee5e4b3 commit 7f0d8d0
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion examples/starwars/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/remogatto/sugarfoam/layout/tiled"
)

// Application states.
const (
GotConnectionState = iota
GotResponseState
Expand All @@ -28,6 +29,7 @@ const (
BrowseState
)

// characterTpl is a template for displaying character information.
const characterTpl = `
* **ID**: %s
* **Name**: %s
Expand All @@ -37,6 +39,7 @@ const characterTpl = `
%s
`

// model represents the application state.
type model struct {
group *group.Model
statusBar *statusbar.Model
Expand All @@ -54,11 +57,13 @@ type model struct {
state int
}

// keyBindings holds the key bindings for the application.
type keyBindings struct {
group *group.Model
quit key.Binding
}

// ShortHelp returns a list of key bindings for the current state.
func (k *keyBindings) ShortHelp() []key.Binding {
keys := make([]key.Binding, 0)

Expand All @@ -81,6 +86,7 @@ func (k *keyBindings) ShortHelp() []key.Binding {
return keys
}

// FullHelp returns a list of all key bindings.
func (k keyBindings) FullHelp() [][]key.Binding {
return [][]key.Binding{
{
Expand All @@ -89,6 +95,7 @@ func (k keyBindings) FullHelp() [][]key.Binding {
}
}

// newBindings creates a new set of key bindings for the application.
func newBindings(group *group.Model) *keyBindings {
return &keyBindings{
group: group,
Expand All @@ -98,6 +105,7 @@ func newBindings(group *group.Model) *keyBindings {
}
}

// initialModel initializes the application model.
func initialModel() model {
viewport := viewport.New()

Expand Down Expand Up @@ -161,6 +169,7 @@ func initialModel() model {
}
}

// Init initializes the application.
func (m model) Init() tea.Cmd {
var cmds []tea.Cmd

Expand All @@ -171,6 +180,7 @@ func (m model) Init() tea.Cmd {
return tea.Batch(cmds...)
}

// Update updates the application state based on messages.
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd

Expand Down Expand Up @@ -209,11 +219,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Batch(append(cmds, m.handleState(msg, cmds)...)...)
}

// View renders the application UI.
func (m model) View() string {
return m.document.View()
}

func (m *model) handleState(msg tea.Msg, cmds []tea.Cmd) []tea.Cmd {
// handleState updates the application state based on the current state.
func (m model) handleState(msg tea.Msg, cmds []tea.Cmd) []tea.Cmd {
_, cmd := m.group.Update(msg)
switch m.state {
case BrowseState:
Expand All @@ -235,6 +247,7 @@ func (m *model) handleState(msg tea.Msg, cmds []tea.Cmd) []tea.Cmd {
return append(cmds, 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]
Expand All @@ -250,6 +263,7 @@ func (m model) updateViewport() {
}
}

// setTableRows sets the table rows with the provided character data.
func (m model) setTableRows(data []character) {
rows := make([]btTable.Row, 0)

Expand All @@ -261,6 +275,7 @@ func (m model) setTableRows(data []character) {
m.table.Model.SetRows(rows)
}

// main is the entry point of the application.
func main() {
if len(os.Getenv("DEBUG")) > 0 {
f, err := tea.LogToFile("debug.log", "debug")
Expand Down

0 comments on commit 7f0d8d0

Please sign in to comment.