forked from Mirrors/bubbletea
Remove commands for manually getting terminal size; no longer needed
Tea core now sends the terminal dimensions on start and when the window is resized (except on Windows where SIGWINCH, the resize signal, is not supported).
This commit is contained in:
parent
a2897e9dd1
commit
6bd34bdd14
34
commands.go
34
commands.go
|
@ -1,23 +1,20 @@
|
||||||
package tea
|
package tea
|
||||||
|
|
||||||
// Convenience commands. Note part of the Bubble Tea runtime, but potentially
|
// Convenience commands. Note part of the Bubble Tea core, but potentially
|
||||||
// handy.
|
// handy.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/crypto/ssh/terminal"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Every is a command that ticks in sync with the system clock. So, if you
|
// Every is a command that ticks in sync with the system clock. So, if you
|
||||||
// wanted to tick with the system clock every second, minute or hour you
|
// wanted to tick with the system clock every second, minute or hour you
|
||||||
// could use this. It's also handy for having different things tick in sync.
|
// could use this. It's also handy for having different things tick in sync.
|
||||||
//
|
//
|
||||||
// Note that because we're ticking with the system clock the tick will likely
|
// Because we're ticking with the system clock the tick will likely not run for
|
||||||
// not run for the entire specified duration. For example, if we're ticking for
|
// the entire specified duration. For example, if we're ticking for one minute
|
||||||
// one minute and the clock is at 12:34:20 then the next tick will happen at
|
// and the clock is at 12:34:20 then the next tick will happen at 12:35:00, 40
|
||||||
// 12:35:00, 40 seconds later.
|
// seconds later.
|
||||||
func Every(duration time.Duration, fn func(time.Time) Msg) Cmd {
|
func Every(duration time.Duration, fn func(time.Time) Msg) Cmd {
|
||||||
return func() Msg {
|
return func() Msg {
|
||||||
n := time.Now()
|
n := time.Now()
|
||||||
|
@ -36,24 +33,3 @@ func Tick(d time.Duration, fn func(time.Time) Msg) Cmd {
|
||||||
return fn(<-t.C)
|
return fn(<-t.C)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TerminalSizeMsg defines an interface for a message that contains terminal
|
|
||||||
// sizing as sent by GetTerminalSize.
|
|
||||||
type TerminalSizeMsg interface {
|
|
||||||
|
|
||||||
// Size returns the terminal width and height, in that order
|
|
||||||
Size() (int, int)
|
|
||||||
|
|
||||||
// Error returns the error, if any, received when fetching the terminal size
|
|
||||||
Error() error
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetTerminalSize is a command used to retrieve the terminal dimensions. Pass
|
|
||||||
// a function used to construct your message, which would implement the
|
|
||||||
// TerminalSizeMsg interaface.
|
|
||||||
func GetTerminalSize(newMsgFunc func(int, int, error) TerminalSizeMsg) Cmd {
|
|
||||||
w, h, err := terminal.GetSize(int(os.Stdout.Fd()))
|
|
||||||
return func() Msg {
|
|
||||||
return newMsgFunc(w, h, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
||||||
|
|
||||||
package tea
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"syscall"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 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, 1)
|
|
||||||
signal.Notify(sig, syscall.SIGWINCH)
|
|
||||||
<-sig
|
|
||||||
return newMsgFunc()
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,9 +0,0 @@
|
||||||
// +build windows
|
|
||||||
|
|
||||||
package tea
|
|
||||||
|
|
||||||
// OnResize is not supported on Windows at this time as Windows does not
|
|
||||||
// support the SIGWINCH signal.
|
|
||||||
func OnResize(newMsgFunc func() Msg) Cmd {
|
|
||||||
return nil
|
|
||||||
}
|
|
2
tea.go
2
tea.go
|
@ -59,7 +59,7 @@ type quitMsg struct{}
|
||||||
// can send a batchMsg with Batch.
|
// can send a batchMsg with Batch.
|
||||||
type batchMsg []Cmd
|
type batchMsg []Cmd
|
||||||
|
|
||||||
// WindowSizeMsg is used to report on the terminal size. It's fired once
|
// WindowSizeMsg is used to report on the terminal size. It's sent once
|
||||||
// initially and then on every terminal resize.
|
// initially and then on every terminal resize.
|
||||||
type WindowSizeMsg struct {
|
type WindowSizeMsg struct {
|
||||||
Width int
|
Width int
|
||||||
|
|
Loading…
Reference in New Issue