diff --git a/input.go b/input.go index 307e7471..0c322cf4 100644 --- a/input.go +++ b/input.go @@ -16,6 +16,8 @@ type ConsoleParser interface { TearDown() error // GetWinSize returns WinSize object to represent width and height of terminal. GetWinSize() *WinSize + // SetWinSize sets default width and height of terminal when can not be optained automatically. + SetWinSize(*WinSize) // Read returns byte array. Read() ([]byte, error) } diff --git a/input_posix.go b/input_posix.go index f1043031..9c2fc194 100644 --- a/input_posix.go +++ b/input_posix.go @@ -14,6 +14,8 @@ const maxReadBytes = 1024 // PosixParser is a ConsoleParser implementation for POSIX environment. type PosixParser struct { fd int + row uint16 + col uint16 origTermios syscall.Termios } @@ -56,12 +58,24 @@ func (t *PosixParser) GetWinSize() *WinSize { if err != nil { panic(err) } + if ws.Row == 0 { + ws.Row = t.row + } + if ws.Col == 0 { + ws.Col = t.col + } return &WinSize{ Row: ws.Row, Col: ws.Col, } } +// SetWinSize sets default width and height of terminal when can not be optained automatically. +func (t *PosixParser) SetWinSize(ws *WinSize) { + t.col = ws.Col + t.row = ws.Row +} + var _ ConsoleParser = &PosixParser{} // NewStandardInputParser returns ConsoleParser object to read from stdin. diff --git a/input_windows.go b/input_windows.go index 11b7679d..b23501a9 100644 --- a/input_windows.go +++ b/input_windows.go @@ -19,6 +19,8 @@ var procGetNumberOfConsoleInputEvents = kernel32.NewProc("GetNumberOfConsoleInpu // WindowsParser is a ConsoleParser implementation for Win32 console. type WindowsParser struct { + row int + col int tty *tty.TTY } @@ -71,12 +73,24 @@ func (p *WindowsParser) GetWinSize() *WinSize { if err != nil { panic(err) } + if h == 0 { + h = p.row + } + if w == 0 { + w = p.col + } return &WinSize{ Row: uint16(h), Col: uint16(w), } } +// SetWinSize sets default width and height of terminal when can not be optained automatically. +func (t *PosixParser) SetWinSize(ws *WinSize) { + t.col = int(ws.Col) + t.row = int(ws.Row) +} + // NewStandardInputParser returns ConsoleParser object to read from stdin. func NewStandardInputParser() *WindowsParser { return &WindowsParser{} diff --git a/option.go b/option.go index 9fbd6806..5694aa11 100644 --- a/option.go +++ b/option.go @@ -266,6 +266,14 @@ func OptionSetExitCheckerOnInput(fn ExitChecker) Option { } } +// OptionSetDefaultWinSize set fallback for win size when not provided by tty +func OptionSetDefaultWinSize(ws *WinSize) Option { + return func(p *Prompt) error { + p.in.SetWinSize(ws) + return nil + } +} + // New returns a Prompt with powerful auto-completion. func New(executor Executor, completer Completer, opts ...Option) *Prompt { defaultWriter := NewStdoutWriter()