Improve examples in docs

This commit is contained in:
Christian Rocha 2020-07-30 11:29:20 -04:00
parent 17035473db
commit 6fcdf9908f
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
3 changed files with 39 additions and 11 deletions

View File

@ -15,6 +15,15 @@ import (
// the entire specified duration. For example, if we're ticking for one minute // the entire specified duration. For example, if we're ticking for one minute
// and the clock is at 12:34:20 then the next tick will happen at 12:35:00, 40 // and the clock is at 12:34:20 then the next tick will happen at 12:35:00, 40
// seconds later. // seconds later.
//
// To produce the command, pass a duration and a fnuction which returns
// a message containing the time at which the tick occurred.
//
// type TickMsg time.Time
//
// cmd := Every(time.Second, func(t time.Time) Msg {
// return TickMsg(t)
// })
func Every(duration time.Duration, fn func(time.Time) Msg) Cmd { func Every(duration time.Duration, fn func(time.Time) Msg) Cmd {
return func() Msg { return func() Msg {
n := time.Now() n := time.Now()
@ -24,9 +33,18 @@ func Every(duration time.Duration, fn func(time.Time) Msg) Cmd {
} }
} }
// Tick is a command that at an interval independent of the system clock at the // Tick produces a command that at an interval independent of the system clock
// given duration. That is, the timer begins when precisely when invoked, and // at the given duration. That is, the timer begins when precisely when
// runs for its entire duration. // invoked, and runs for its entire duration.
//
// To produce the command, pass a duration and a fnuction which returns
// a message containing the time at which the tick occurred.
//
// type TickMsg time.Time
//
// cmd := Tick(time.Second, func(t time.Time) Msg {
// return TickMsg(t)
// })
func Tick(d time.Duration, fn func(time.Time) Msg) Cmd { func Tick(d time.Duration, fn func(time.Time) Msg) Cmd {
return func() Msg { return func() Msg {
t := time.NewTimer(d) t := time.NewTimer(d)

17
key.go
View File

@ -11,19 +11,28 @@ import (
// the program's update function. There are a couple general patterns you could // the program's update function. There are a couple general patterns you could
// use to check for keypresses: // use to check for keypresses:
// //
// // Switch on the type (safer)
// switch msg := msg.(type) { // switch msg := msg.(type) {
// case KeyMsg: // case KeyMsg:
// switch msg.Type { // switch msg.Type {
// case KeyEnter: // case KeyEnter:
// fmt.Println("you pressed enter!") // fmt.Println("you pressed enter!")
// case KeyRune:
// switch msg.Rune {
// case 'a':
// fmt.Println("you pressed a!")
// }
// } // }
// } // }
// //
// // Switch on the string representation of the key (shorter)
// switch msg := msg.(type) { // switch msg := msg.(type) {
// case KeyMsg: // case KeyMsg:
// switch msg.String() { // switch msg.String() {
// case "enter": // case "enter":
// fmt.Println("you pressed enter!") // fmt.Println("you pressed enter!")
// case "a':
// fmt.Println("you pressed a!")
// } // }
// } // }
type KeyMsg Key type KeyMsg Key

9
tea.go
View File

@ -1,7 +1,8 @@
// Package tea provides an Elm-inspired framework for building rich terminal // Package tea provides a framework for building rich terminal user interfaces
// user interfaces. It's well-suited for simple and complex terminal // based on the paradigms of The Elm Architecture. It's well-suited for simple
// applications, either inline, full-window, or a mix of both. It's been // and complex terminal applications, either inline, full-window, or a mix of
// battle-tested in several large projects and is production-ready. // both. It's been battle-tested in several large projects and is
// production-ready.
// //
// A tutorial is available at https://github.com/charmbracelet/bubbletea/tree/master/tutorials // A tutorial is available at https://github.com/charmbracelet/bubbletea/tree/master/tutorials
// //