Remove extraneous select{} and improve comments

This commit is contained in:
Christian Rocha 2020-01-14 17:19:44 -05:00
parent 466d16b5a4
commit c7f1302943
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
2 changed files with 13 additions and 12 deletions

2
key.go
View File

@ -15,7 +15,7 @@ const (
keyLF = 9 // line-feed, \n keyLF = 9 // line-feed, \n
keyCR = 13 // carriage return, \r keyCR = 13 // carriage return, \r
keyESC = 27 keyESC = 27
keyUS = 31 keyUS = 31 // unit separator
) )
var controlNames = map[int]string{ var controlNames = map[int]string{

23
tea.go
View File

@ -14,14 +14,13 @@ const esc = "\033["
// Msg represents an action. It's used by Update to update the UI. // Msg represents an action. It's used by Update to update the UI.
type Msg interface{} type Msg interface{}
// Model contains the data for an application // Model contains the updatable data for an application
type Model interface{} type Model interface{}
// Cmd is an IO operation. If it's nil it's considered a no-op. // Cmd is an IO operation. If it's nil it's considered a no-op.
type Cmd func() Msg type Cmd func() Msg
// Sub is an event subscription. If it's nil it's considered a no-op. But why // Sub is an event subscription. If it returns nil it's considered a no-op.
// would you subscribe to nil?
type Sub func(Model) Msg type Sub func(Model) Msg
// Update is called when a message is received. It may update the model and/or // Update is called when a message is received. It may update the model and/or
@ -40,7 +39,7 @@ type Program struct {
rw io.ReadWriter rw io.ReadWriter
} }
// Quit command // Quit is a command that tells the program to exit
func Quit() Msg { func Quit() Msg {
return quitMsg{} return quitMsg{}
} }
@ -91,15 +90,12 @@ func (p *Program) Start() error {
// users typically need? // users typically need?
go func() { go func() {
for { for {
select { msg, _ := ReadKey(p.rw)
default: msgs <- KeyPressMsg(msg)
msg, _ := ReadKey(p.rw)
msgs <- KeyPressMsg(msg)
}
} }
}() }()
// Process subscriptions // Initialize subscriptions
go func() { go func() {
if len(p.subscriptions) > 0 { if len(p.subscriptions) > 0 {
for _, sub := range p.subscriptions { for _, sub := range p.subscriptions {
@ -145,7 +141,7 @@ func (p *Program) Start() error {
} }
} }
// Render a view // Render a view to the terminal
func (p *Program) render(model Model, init bool) { func (p *Program) render(model Model, init bool) {
view := p.view(model) view := p.view(model)
@ -159,22 +155,27 @@ func (p *Program) render(model Model, init bool) {
io.WriteString(p.rw, view) io.WriteString(p.rw, view)
} }
// Hide the cursor
func hideCursor() { func hideCursor() {
fmt.Printf(esc + "?25l") fmt.Printf(esc + "?25l")
} }
// Show the cursor
func showCursor() { func showCursor() {
fmt.Printf(esc + "?25h") fmt.Printf(esc + "?25h")
} }
// Move the cursor up a given number of lines
func cursorUp(n int) { func cursorUp(n int) {
fmt.Printf(esc+"%dF", n) fmt.Printf(esc+"%dF", n)
} }
// Clear the current line
func clearLine() { func clearLine() {
fmt.Printf(esc + "2K") fmt.Printf(esc + "2K")
} }
// Clear a given number of lines
func clearLines(n int) { func clearLines(n int) {
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
cursorUp(1) cursorUp(1)