Skip to content

Commit

Permalink
chore(examples): impoved model initialization consistancy
Browse files Browse the repository at this point in the history
  • Loading branch information
samyosm committed Jul 6, 2023
1 parent c1b0b19 commit e6eabca
Show file tree
Hide file tree
Showing 25 changed files with 246 additions and 190 deletions.
12 changes: 7 additions & 5 deletions examples/cellbuffer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ type model struct {
xVelocity, yVelocity float64
}

func initialModel() model {
return model{
spring: harmonica.NewSpring(harmonica.FPS(fps), frequency, damping),
}
}

func (m model) Init() tea.Cmd {
return animate()
}
Expand Down Expand Up @@ -183,11 +189,7 @@ func (m model) View() string {
}

func main() {
m := model{
spring: harmonica.NewSpring(harmonica.FPS(fps), frequency, damping),
}

p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())
p := tea.NewProgram(initialModel(), tea.WithAltScreen(), tea.WithMouseCellMotion())
if _, err := p.Run(); err != nil {
fmt.Println("Uh oh:", err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions examples/composable-views/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type mainModel struct {
index int
}

func newModel(timeout time.Duration) mainModel {
func initialModel(timeout time.Duration) mainModel {
m := mainModel{state: timerView}
m.timer = timer.New(timeout)
m.spinner = spinner.New()
Expand Down Expand Up @@ -154,7 +154,7 @@ func (m *mainModel) resetSpinner() {
}

func main() {
p := tea.NewProgram(newModel(defaultTime))
p := tea.NewProgram(initialModel(defaultTime))

if _, err := p.Run(); err != nil {
log.Fatal(err)
Expand Down
3 changes: 1 addition & 2 deletions examples/exec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ func (m model) View() string {
}

func main() {
m := model{}
if _, err := tea.NewProgram(m).Run(); err != nil {
if _, err := tea.NewProgram(model{}).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
Expand Down
20 changes: 12 additions & 8 deletions examples/file-picker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ func clearErrorAfter(t time.Duration) tea.Cmd {
})
}

func initialModel() model {
fp := filepicker.New()
fp.AllowedTypes = []string{".mod", ".sum", ".go", ".txt", ".md"}
fp.CurrentDirectory, _ = os.UserHomeDir()

return model{
filepicker: fp,
}
}

func (m model) Init() tea.Cmd {
return m.filepicker.Init()
}
Expand Down Expand Up @@ -81,14 +91,8 @@ func (m model) View() string {
}

func main() {
fp := filepicker.New()
fp.AllowedTypes = []string{".mod", ".sum", ".go", ".txt", ".md"}
fp.CurrentDirectory, _ = os.UserHomeDir()

m := model{
filepicker: fp,
}
tm, _ := tea.NewProgram(&m, tea.WithOutput(os.Stderr)).Run()
m := initialModel()
tm, _ := tea.NewProgram(m, tea.WithOutput(os.Stderr)).Run()
mm := tm.(model)
fmt.Println("\n You selected: " + m.filepicker.Styles.Selected.Render(mm.selectedFile) + "\n")
}
16 changes: 8 additions & 8 deletions examples/glamour/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ Bon appétit!

var helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Render

type example struct {
type model struct {
viewport viewport.Model
}

func newExample() (*example, error) {
func initialModel() (*model, error) {
const width = 78

vp := viewport.New(width, 20)
Expand All @@ -80,16 +80,16 @@ func newExample() (*example, error) {

vp.SetContent(str)

return &example{
return &model{
viewport: vp,
}, nil
}

func (e example) Init() tea.Cmd {
func (e model) Init() tea.Cmd {
return nil
}

func (e example) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (e model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
Expand All @@ -105,16 +105,16 @@ func (e example) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}

func (e example) View() string {
func (e model) View() string {
return e.viewport.View() + e.helpView()
}

func (e example) helpView() string {
func (e model) helpView() string {
return helpStyle("\n ↑/↓: Navigate • q: Quit\n")
}

func main() {
model, err := newExample()
model, err := initialModel()
if err != nil {
fmt.Println("Could not initialize Bubble Tea model:", err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions examples/help/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type model struct {
quitting bool
}

func newModel() model {
func initialModel() model {
return model{
keys: keys,
help: help.New(),
Expand Down Expand Up @@ -140,7 +140,7 @@ func main() {
defer f.Close() // nolint:errcheck
}

if _, err := tea.NewProgram(newModel()).Run(); err != nil {
if _, err := tea.NewProgram(initialModel()).Run(); err != nil {
fmt.Printf("Could not start program :(\n%v\n", err)
os.Exit(1)
}
Expand Down
62 changes: 34 additions & 28 deletions examples/list-default/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,7 @@ type model struct {
list list.Model
}

func (m model) Init() tea.Cmd {
return nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
}

var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}

func (m model) View() string {
return docStyle.Render(m.list.View())
}

func main() {
func initialModel() model {
items := []list.Item{
item{title: "Raspberry Pi’s", desc: "I have ’em all over my house"},
item{title: "Nutella", desc: "It's good on toast"},
Expand All @@ -74,10 +50,40 @@ func main() {
item{title: "Terrycloth", desc: "In other words, towel fabric"},
}

m := model{list: list.New(items, list.NewDefaultDelegate(), 0, 0)}
m.list.Title = "My Fave Things"
l := list.New(items, list.NewDefaultDelegate(), 0, 0)
l.Title = "My Fave Things"

p := tea.NewProgram(m, tea.WithAltScreen())
return model{
list: l,
}
}

func (m model) Init() tea.Cmd {
return nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
}

var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}

func (m model) View() string {
return docStyle.Render(m.list.View())
}

func main() {
p := tea.NewProgram(initialModel(), tea.WithAltScreen())

if _, err := p.Run(); err != nil {
fmt.Println("Error running program:", err)
Expand Down
4 changes: 2 additions & 2 deletions examples/list-fancy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type model struct {
delegateKeys *delegateKeyMap
}

func newModel() model {
func initialModel() model {
var (
itemGenerator randomItemGenerator
delegateKeys = newDelegateKeyMap()
Expand Down Expand Up @@ -183,7 +183,7 @@ func (m model) View() string {
func main() {
rand.Seed(time.Now().UTC().UnixNano())

if _, err := tea.NewProgram(newModel()).Run(); err != nil {
if _, err := tea.NewProgram(initialModel()).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
Expand Down
56 changes: 30 additions & 26 deletions examples/list-simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,35 @@ type model struct {
quitting bool
}

func initialModel() model {
items := []list.Item{
item("Ramen"),
item("Tomato Soup"),
item("Hamburgers"),
item("Cheeseburgers"),
item("Currywurst"),
item("Okonomiyaki"),
item("Pasta"),
item("Fillet Mignon"),
item("Caviar"),
item("Just Wine"),
}

const defaultWidth = 20

l := list.New(items, itemDelegate{}, defaultWidth, listHeight)
l.Title = "What do you want for dinner?"
l.SetShowStatusBar(false)
l.SetFilteringEnabled(false)
l.Styles.Title = titleStyle
l.Styles.PaginationStyle = paginationStyle
l.Styles.HelpStyle = helpStyle

return model{
list: l,
}
}

func (m model) Init() tea.Cmd {
return nil
}
Expand Down Expand Up @@ -96,32 +125,7 @@ func (m model) View() string {
}

func main() {
items := []list.Item{
item("Ramen"),
item("Tomato Soup"),
item("Hamburgers"),
item("Cheeseburgers"),
item("Currywurst"),
item("Okonomiyaki"),
item("Pasta"),
item("Fillet Mignon"),
item("Caviar"),
item("Just Wine"),
}

const defaultWidth = 20

l := list.New(items, itemDelegate{}, defaultWidth, listHeight)
l.Title = "What do you want for dinner?"
l.SetShowStatusBar(false)
l.SetFilteringEnabled(false)
l.Styles.Title = titleStyle
l.Styles.PaginationStyle = paginationStyle
l.Styles.HelpStyle = helpStyle

m := model{list: l}

if _, err := tea.NewProgram(m).Run(); err != nil {
if _, err := tea.NewProgram(initialModel()).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/package-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (
checkMark = lipgloss.NewStyle().Foreground(lipgloss.Color("42")).SetString("✓")
)

func newModel() model {
func initialModel() model {
p := progress.New(
progress.WithDefaultGradient(),
progress.WithWidth(40),
Expand Down Expand Up @@ -129,7 +129,7 @@ func max(a, b int) int {
}

func main() {
if _, err := tea.NewProgram(newModel()).Run(); err != nil {
if _, err := tea.NewProgram(initialModel()).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
Expand Down
22 changes: 14 additions & 8 deletions examples/pager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ type model struct {
viewport viewport.Model
}

func initialModel() model {
// Load some text for our viewport
content, err := os.ReadFile("artichoke.md")
if err != nil {
fmt.Println("could not load file:", err)
os.Exit(1)
}

return model{
content: string(content),
}
}

func (m model) Init() tea.Cmd {
return nil
}
Expand Down Expand Up @@ -127,15 +140,8 @@ func max(a, b int) int {
}

func main() {
// Load some text for our viewport
content, err := os.ReadFile("artichoke.md")
if err != nil {
fmt.Println("could not load file:", err)
os.Exit(1)
}

p := tea.NewProgram(
model{content: string(content)},
initialModel(),
tea.WithAltScreen(), // use the full size of the terminal in its "alternate screen buffer"
tea.WithMouseCellMotion(), // turn on mouse support so we can track the mouse wheel
)
Expand Down
4 changes: 2 additions & 2 deletions examples/paginator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
)

func newModel() model {
func initialModel() model {
var items []string
for i := 1; i < 101; i++ {
text := fmt.Sprintf("Item %d", i)
Expand Down Expand Up @@ -69,7 +69,7 @@ func (m model) View() string {
}

func main() {
p := tea.NewProgram(newModel())
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit e6eabca

Please sign in to comment.