diff --git a/commands.go b/commands.go index 91116388a3..99007b2ce2 100644 --- a/commands.go +++ b/commands.go @@ -19,38 +19,38 @@ import ( // To produce the command, pass a duration and a function which returns // a message containing the time at which the tick occurred. // -// type TickMsg time.Time +// type TickMsg time.Time // -// cmd := Every(time.Second, func(t time.Time) Msg { -// return TickMsg(t) -// }) +// cmd := Every(time.Second, func(t time.Time) Msg { +// return TickMsg(t) +// }) // // Beginners' note: Every sends a single message and won't automatically // dispatch messages at an interval. To do that, you'll want to return another // Every command after receiving your tick message. For example: // -// type TickMsg time.Time -// -// // Send a message every second. -// func tickEvery() Cmd { -// return Every(time.Second, func(t time.Time) Msg { -// return TickMsg(t) -// }) -// } -// -// func (m model) Init() Cmd { -// // Start ticking. -// return tickEvery() -// } -// -// func (m model) Update(msg Msg) (Model, Cmd) { -// switch msg.(type) { -// case TickMsg: -// // Return your Every command again to loop. -// return m, tickEvery() -// } -// return m, nil -// } +// type TickMsg time.Time +// +// // Send a message every second. +// func tickEvery() Cmd { +// return Every(time.Second, func(t time.Time) Msg { +// return TickMsg(t) +// }) +// } +// +// func (m model) Init() Cmd { +// // Start ticking. +// return tickEvery() +// } +// +// func (m model) Update(msg Msg) (Model, Cmd) { +// switch msg.(type) { +// case TickMsg: +// // Return your Every command again to loop. +// return m, tickEvery() +// } +// return m, nil +// } // // Every is analogous to Tick in the Elm Architecture. func Every(duration time.Duration, fn func(time.Time) Msg) Cmd { @@ -69,37 +69,38 @@ func Every(duration time.Duration, fn func(time.Time) Msg) Cmd { // To produce the command, pass a duration and a function which returns // a message containing the time at which the tick occurred. // -// type TickMsg time.Time +// type TickMsg time.Time // -// cmd := Tick(time.Second, func(t time.Time) Msg { -// return TickMsg(t) -// }) +// cmd := Tick(time.Second, func(t time.Time) Msg { +// return TickMsg(t) +// }) // // Beginners' note: Tick sends a single message and won't automatically // dispatch messages at an interval. To do that, you'll want to return another // Tick command after receiving your tick message. For example: // -// type TickMsg time.Time -// -// func doTick() Cmd { -// return Tick(time.Second, func(t time.Time) Msg { -// return TickMsg(t) -// }) -// } -// -// func (m model) Init() Cmd { -// // Start ticking. -// return doTick() -// } -// -// func (m model) Update(msg Msg) (Model, Cmd) { -// switch msg.(type) { -// case TickMsg: -// // Return your Tick command again to loop. -// return m, doTick() -// } -// return m, nil -// } +// type TickMsg time.Time +// +// func doTick() Cmd { +// return Tick(time.Second, func(t time.Time) Msg { +// return TickMsg(t) +// }) +// } +// +// func (m model) Init() Cmd { +// // Start ticking. +// return doTick() +// } +// +// func (m model) Update(msg Msg) (Model, Cmd) { +// switch msg.(type) { +// case TickMsg: +// // Return your Tick command again to loop. +// return m, doTick() +// } +// return m, nil +// } +// func Tick(d time.Duration, fn func(time.Time) Msg) Cmd { return func() Msg { t := time.NewTimer(d) @@ -111,14 +112,15 @@ func Tick(d time.Duration, fn func(time.Time) Msg) Cmd { // commands. // The Msg returned is the first non-nil message returned by a Cmd. // -// func saveStateCmd() Msg { -// if err := save(); err != nil { -// return errMsg{err} -// } -// return nil -// } +// func saveStateCmd() Msg { +// if err := save(); err != nil { +// return errMsg{err} +// } +// return nil +// } +// +// cmd := Sequentially(saveStateCmd, Quit) // -// cmd := Sequentially(saveStateCmd, Quit) func Sequentially(cmds ...Cmd) Cmd { return func() Msg { for _, cmd := range cmds { diff --git a/exec.go b/exec.go index 587d91a8ca..d1d9d69a29 100644 --- a/exec.go +++ b/exec.go @@ -34,17 +34,17 @@ func Exec(c ExecCommand, fn ExecCallback) Cmd { // a message containing the error which may have occurred when running the // ExecCommand. // -// type VimFinishedMsg struct { err error } +// type VimFinishedMsg struct { err error } // -// c := exec.Command("vim", "file.txt") +// c := exec.Command("vim", "file.txt") // -// cmd := ExecProcess(c, func(err error) Msg { -// return VimFinishedMsg{err: error} -// }) +// cmd := ExecProcess(c, func(err error) Msg { +// return VimFinishedMsg{err: error} +// }) // // Or, if you don't care about errors, you could simply: // -// cmd := ExecProcess(exec.Command("vim", "file.txt"), nil) +// cmd := ExecProcess(exec.Command("vim", "file.txt"), nil) // // For non-interactive i/o you should use a Cmd (that is, a tea.Cmd). func ExecProcess(c *exec.Cmd, fn ExecCallback) Cmd { diff --git a/key.go b/key.go index 07bc651930..12446a2878 100644 --- a/key.go +++ b/key.go @@ -13,30 +13,30 @@ import ( // the program's update function. There are a couple general patterns you could // use to check for keypresses: // -// // Switch on the string representation of the key (shorter) -// switch msg := msg.(type) { -// case KeyMsg: -// switch msg.String() { -// case "enter": -// fmt.Println("you pressed enter!") -// case "a": -// fmt.Println("you pressed a!") -// } -// } +// // Switch on the string representation of the key (shorter) +// switch msg := msg.(type) { +// case KeyMsg: +// switch msg.String() { +// case "enter": +// fmt.Println("you pressed enter!") +// case "a": +// fmt.Println("you pressed a!") +// } +// } // -// // Switch on the key type (more foolproof) -// switch msg := msg.(type) { -// case KeyMsg: -// switch msg.Type { -// case KeyEnter: -// fmt.Println("you pressed enter!") -// case KeyRunes: -// switch string(msg.Runes) { -// case "a": -// fmt.Println("you pressed a!") -// } -// } -// } +// // Switch on the key type (more foolproof) +// switch msg := msg.(type) { +// case KeyMsg: +// switch msg.Type { +// case KeyEnter: +// fmt.Println("you pressed enter!") +// case KeyRunes: +// switch string(msg.Runes) { +// case "a": +// fmt.Println("you pressed a!") +// } +// } +// } // // Note that Key.Runes will always contain at least one character, so you can // always safely call Key.Runes[0]. In most cases Key.Runes will only contain @@ -60,9 +60,10 @@ type Key struct { // String returns a friendly string representation for a key. It's safe (and // encouraged) for use in key comparison. // -// k := Key{Type: KeyEnter} -// fmt.Println(k) -// // Output: enter +// k := Key{Type: KeyEnter} +// fmt.Println(k) +// // Output: enter +// func (k Key) String() (str string) { if k.Alt { str += "alt+" @@ -81,16 +82,16 @@ func (k Key) String() (str string) { // All other keys will be type KeyRunes. To get the rune value, check the Rune // method on a Key struct, or use the Key.String() method: // -// k := Key{Type: KeyRunes, Runes: []rune{'a'}, Alt: true} -// if k.Type == KeyRunes { +// k := Key{Type: KeyRunes, Runes: []rune{'a'}, Alt: true} +// if k.Type == KeyRunes { // -// fmt.Println(k.Runes) -// // Output: a +// fmt.Println(k.Runes) +// // Output: a // -// fmt.Println(k.String()) -// // Output: alt+a +// fmt.Println(k.String()) +// // Output: alt+a // -// } +// } type KeyType int func (k KeyType) String() (str string) { diff --git a/logging.go b/logging.go index 54a4350aca..bca66ca5a2 100644 --- a/logging.go +++ b/logging.go @@ -12,12 +12,12 @@ import ( // // Don't forget to close the file when you're done with it. // -// f, err := LogToFile("debug.log", "debug") -// if err != nil { -// fmt.Println("fatal:", err) -// os.Exit(1) -// } -// defer f.Close() +// f, err := LogToFile("debug.log", "debug") +// if err != nil { +// fmt.Println("fatal:", err) +// os.Exit(1) +// } +// defer f.Close() func LogToFile(path string, prefix string) (*os.File, error) { f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) if err != nil { diff --git a/mouse.go b/mouse.go index 5ff3a2faf0..5fb7427c9c 100644 --- a/mouse.go +++ b/mouse.go @@ -63,7 +63,7 @@ var mouseEventTypes = map[MouseEventType]string{ // // X10 mouse events look like: // -// ESC [M Cb Cx Cy +// ESC [M Cb Cx Cy // // See: http://www.xfree86.org/current/ctlseqs.html#Mouse%20Tracking func parseX10MouseEvents(buf []byte) ([]MouseEvent, error) { diff --git a/options.go b/options.go index b60d23c232..09e5ecbd73 100644 --- a/options.go +++ b/options.go @@ -7,7 +7,8 @@ import "io" // // Example usage: // -// p := NewProgram(model, WithInput(someInput), WithOutput(someOutput)) +// p := NewProgram(model, WithInput(someInput), WithOutput(someOutput)) +// type ProgramOption func(*Program) // WithOutput sets the output which, by default, is stdout. In most cases you @@ -50,11 +51,11 @@ func WithoutCatchPanics() ProgramOption { // // Example: // -// p := tea.NewProgram(Model{}, tea.WithAltScreen()) -// if err := p.Start(); err != nil { -// fmt.Println("Error running program:", err) -// os.Exit(1) -// } +// p := tea.NewProgram(Model{}, tea.WithAltScreen()) +// if err := p.Start(); err != nil { +// fmt.Println("Error running program:", err) +// os.Exit(1) +// } // // To enter the altscreen once the program has already started running use the // EnterAltScreen command. diff --git a/tea.go b/tea.go index 10bfd013a3..fcbf822af4 100644 --- a/tea.go +++ b/tea.go @@ -124,9 +124,10 @@ type Program struct { // // Example: // -// func (m model) Init() Cmd { -// return tea.Batch(someCommand, someOtherCommand) -// } +// func (m model) Init() Cmd { +// return tea.Batch(someCommand, someOtherCommand) +// } +// func Batch(cmds ...Cmd) Cmd { var validCmds []Cmd for _, c := range cmds {