forked from Mirrors/bubbletea
Add command for listening for window resizes
This commit is contained in:
parent
25f8b8c99b
commit
d2ae1b0fb7
2
boba.go
2
boba.go
|
@ -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
|
||||||
|
|
18
commands.go
18
commands.go
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue