From d2ae1b0fb7cb66b997a82f9119e490f102ccea81 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Sun, 17 May 2020 19:28:12 -0400 Subject: [PATCH] Add command for listening for window resizes --- boba.go | 2 +- commands.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/boba.go b/boba.go index 8374672..2cb1faa 100644 --- a/boba.go +++ b/boba.go @@ -15,7 +15,7 @@ type Msg interface{} // Model contains the program's state. 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 // Batch peforms a bunch of commands concurrently with no ordering guarantees diff --git a/commands.go b/commands.go index 4e6cbe8..f6f7d7b 100644 --- a/commands.go +++ b/commands.go @@ -1,7 +1,11 @@ package boba +// Convenience commands. Note part of the Boba runtime, but potentially handy. + import ( "os" + "os/signal" + "syscall" "time" "golang.org/x/crypto/ssh/terminal" @@ -58,3 +62,17 @@ func GetTerminalSize(newMsgFunc func(int, int, error) TerminalSizeMsg) Cmd { 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() + } +}