mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-17 02:25:12 -07:00
fix: avoid global state (#126)
Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
This commit is contained in:
committed by
GitHub
parent
59e5d8e2c9
commit
60ddf33992
23
README.md
23
README.md
@@ -89,20 +89,22 @@ type model struct {
|
|||||||
## Initialization
|
## Initialization
|
||||||
|
|
||||||
Next we'll define our application’s initial state. We’ll store our initial
|
Next we'll define our application’s initial state. We’ll store our initial
|
||||||
model in a simple variable, and then define the `Init` method. `Init` can
|
model in a simple variable, and then define the `Init` method. `Init` can
|
||||||
return a `Cmd` that could perform some initial I/O. For now, we don't need to
|
return a `Cmd` that could perform some initial I/O. For now, we don't need to
|
||||||
do any I/O, so for the command we'll just return `nil`, which translates to "no
|
do any I/O, so for the command we'll just return `nil`, which translates to "no
|
||||||
command."
|
command."
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var initialModel = model{
|
func main() {
|
||||||
// Our to-do list is just a grocery list
|
initialModel := model{
|
||||||
choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},
|
// Our to-do list is just a grocery list
|
||||||
|
choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},
|
||||||
|
|
||||||
// A map which indicates which choices are selected. We're using
|
// A map which indicates which choices are selected. We're using
|
||||||
// the map like a mathematical set. The keys refer to the indexes
|
// the map like a mathematical set. The keys refer to the indexes
|
||||||
// of the `choices` slice, above.
|
// of the `choices` slice, above.
|
||||||
selected: make(map[int]struct{}),
|
selected: make(map[int]struct{}),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) Init() tea.Cmd {
|
func (m model) Init() tea.Cmd {
|
||||||
@@ -228,6 +230,11 @@ The last step is to simply run our program. We pass our initial model to
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
func main() {
|
func main() {
|
||||||
|
initialModel := model{
|
||||||
|
choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},
|
||||||
|
selected: make(map[int]struct{}),
|
||||||
|
}
|
||||||
|
|
||||||
p := tea.NewProgram(initialModel)
|
p := tea.NewProgram(initialModel)
|
||||||
if err := p.Start(); err != nil {
|
if err := p.Start(); err != nil {
|
||||||
fmt.Printf("Alas, there's been an error: %v", err)
|
fmt.Printf("Alas, there's been an error: %v", err)
|
||||||
|
|||||||
@@ -59,14 +59,16 @@ do any I/O, so for the command we'll just return `nil`, which translates to "no
|
|||||||
command."
|
command."
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var initialModel = model{
|
func main() {
|
||||||
// Our to-do list is just a grocery list
|
initialModel := model{
|
||||||
choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},
|
// Our to-do list is just a grocery list
|
||||||
|
choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},
|
||||||
|
|
||||||
// A map which indicates which choices are selected. We're using
|
// A map which indicates which choices are selected. We're using
|
||||||
// the map like a mathematical set. The keys refer to the indexes
|
// the map like a mathematical set. The keys refer to the indexes
|
||||||
// of the `choices` slice, above.
|
// of the `choices` slice, above.
|
||||||
selected: make(map[int]struct{}),
|
selected: make(map[int]struct{}),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) Init() tea.Cmd {
|
func (m model) Init() tea.Cmd {
|
||||||
@@ -192,6 +194,11 @@ The last step is to simply run our program. We pass our initial model to
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
func main() {
|
func main() {
|
||||||
|
initialModel := model{
|
||||||
|
choices: []string{"Carrots", "Celery", "Kohlrabi"},
|
||||||
|
selected: make(map[int]struct{}),
|
||||||
|
}
|
||||||
|
|
||||||
p := tea.NewProgram(initialModel)
|
p := tea.NewProgram(initialModel)
|
||||||
if err := p.Start(); err != nil {
|
if err := p.Start(); err != nil {
|
||||||
fmt.Printf("Alas, there's been an error: %v", err)
|
fmt.Printf("Alas, there's been an error: %v", err)
|
||||||
|
|||||||
@@ -13,11 +13,6 @@ type model struct {
|
|||||||
selected map[int]struct{}
|
selected map[int]struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
var initialModel = model{
|
|
||||||
choices: []string{"Carrots", "Celery", "Kohlrabi"},
|
|
||||||
selected: make(map[int]struct{}),
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m model) Init() tea.Cmd {
|
func (m model) Init() tea.Cmd {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -72,6 +67,11 @@ func (m model) View() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
initialModel := model{
|
||||||
|
choices: []string{"Carrots", "Celery", "Kohlrabi"},
|
||||||
|
selected: make(map[int]struct{}),
|
||||||
|
}
|
||||||
|
|
||||||
p := tea.NewProgram(initialModel)
|
p := tea.NewProgram(initialModel)
|
||||||
if err := p.Start(); err != nil {
|
if err := p.Start(); err != nil {
|
||||||
fmt.Printf("Alas, there's been an error: %v", err)
|
fmt.Printf("Alas, there's been an error: %v", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user