Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option for default win size #167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
14 changes: 14 additions & 0 deletions input_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions input_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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{}
Expand Down
8 changes: 8 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down