Add command for listening for window resizes

This commit is contained in:
Christian Rocha 2020-05-17 19:28:12 -04:00
parent 25f8b8c99b
commit d2ae1b0fb7
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
2 changed files with 19 additions and 1 deletions

View File

@ -15,7 +15,7 @@ type Msg interface{}
// Model contains the program's state. // Model contains the program's state.
type Model interface{} type Model interface{}
// Cmd is an IO operation that runs once. 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
// Batch peforms a bunch of commands concurrently with no ordering guarantees // Batch peforms a bunch of commands concurrently with no ordering guarantees

View File

@ -1,7 +1,11 @@
package boba package boba
// Convenience commands. Note part of the Boba runtime, but potentially handy.
import ( import (
"os" "os"
"os/signal"
"syscall"
"time" "time"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
@ -58,3 +62,17 @@ func GetTerminalSize(newMsgFunc func(int, int, error) TerminalSizeMsg) Cmd {
return newMsgFunc(w, h, err) return newMsgFunc(w, h, err)
} }
} }
// OnResize is used to listen for window resizes. Use GetTerminalSize to get
// the windows dimensions. We don't fetch the window size with this command to
// avoid a potential performance hit making the necessary system calls, since
// this command could potentially run a lot. On that note, consider debouncing
// this function.
func OnResize(newMsgFunc func() Msg) Cmd {
return func() Msg {
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGWINCH)
<-sig
return newMsgFunc()
}
}