Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change selected in the tutorial from a map to a bool slice for simplicity #1223

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions tutorials/basics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ It can be any type, but a `struct` usually makes the most sense.

```go
type model struct {
choices []string // items on the to-do list
cursor int // which to-do list item our cursor is pointing at
selected map[int]struct{} // which to-do items are selected
choices []string // items on the to-do list
cursor int // which to-do list item our cursor is pointing at
selected []bool // which to-do items are selected
}
```

Expand All @@ -59,14 +59,13 @@ the initial model as a variable elsewhere, too.

```go
func initialModel() model {
// Our to-do list is a grocery list
choices := []string{"Buy carrots", "Buy celery", "Buy kohlrabi"}
return model{
// Our to-do list is a grocery list
choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},
choices: choices,

// A map which indicates which choices are selected. We're using
// the map like a mathematical set. The keys refer to the indexes
// of the `choices` slice, above.
selected: make(map[int]struct{}),
// A bool slice which indicates which choices are selected.
selected: make([]bool, len(choices)),
}
}
```
Expand Down Expand Up @@ -131,12 +130,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// 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{}{}
}
m.selected[m.cursor] = !m.selected[m.cursor]
}
}

Expand Down Expand Up @@ -176,7 +170,7 @@ func (m model) View() string {

// Is this choice selected?
checked := " " // not selected
if _, ok := m.selected[i]; ok {
if m.selected[i] {
checked = "x" // selected!
}

Expand Down
20 changes: 7 additions & 13 deletions tutorials/basics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ import (
type model struct {
cursor int
choices []string
selected map[int]struct{}
selected []bool
}

func initialModel() model {
choices := []string{"Buy carrots", "Buy celery", "Buy kohlrabi"}
return model{
choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},
choices: choices,

// A map which indicates which choices are selected. We're using
// the map like a mathematical set. The keys refer to the indexes
// of the `choices` slice, above.
selected: make(map[int]struct{}),
// A bool slice which indicates which choices are selected.
selected: make([]bool, len(choices)),
}
}

Expand All @@ -43,12 +42,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.cursor++
}
case "enter", " ":
_, ok := m.selected[m.cursor]
if ok {
delete(m.selected, m.cursor)
} else {
m.selected[m.cursor] = struct{}{}
}
m.selected[m.cursor] = !m.selected[m.cursor]
}
}

Expand All @@ -65,7 +59,7 @@ func (m model) View() string {
}

checked := " "
if _, ok := m.selected[i]; ok {
if m.selected[i] {
checked = "x"
}

Expand Down
Loading