Skip to content
This repository has been archived by the owner on Aug 29, 2022. It is now read-only.

Commit

Permalink
Allow to change term size with WithTermSize(cols, rows)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhatthm committed Apr 22, 2021
1 parent a5985b4 commit 25ef3b3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
40 changes: 33 additions & 7 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/Netflix/go-expect"
"github.com/creack/pty"
"github.com/cucumber/godog"
"github.com/hinshun/vt10x"
"github.com/stretchr/testify/require"
Expand All @@ -30,6 +31,10 @@ type Manager struct {
sessions map[string]*session
current string

// Terminal size.
termCols int
termRows int

starters []Starter
closers []Closer

Expand Down Expand Up @@ -77,12 +82,7 @@ func (m *Manager) NewConsole(sc *godog.Scenario) (*expect.Console, *vt10x.State)
m.test.Logf("Console: %s (#%s)\n", sc.Name, sc.Id)

sess.output = new(Buffer)

console, state, err := vt10x.NewVT10XConsole(expect.WithStdout(sess.output))
require.NoError(m.test, err)

sess.console = console
sess.state = state
sess.console, sess.state = newVT10XConsole(m.test, m.termCols, m.termCols, expect.WithStdout(sess.output))

m.sessions[sc.Id] = sess
m.current = sc.Id
Expand Down Expand Up @@ -156,8 +156,10 @@ func (m *Manager) WithCloser(c Closer) *Manager {
// New initiates a new console Manager.
func New(t TestingT, options ...Option) *Manager {
m := &Manager{
test: t,
sessions: make(map[string]*session),
termCols: 80,
termRows: 100,
test: t,
}

for _, o := range options {
Expand All @@ -180,3 +182,27 @@ func WithCloser(c Closer) Option {
m.WithCloser(c)
}
}

// WithTermSize sets terminal size cols x rows. Default is 80 x 100.
func WithTermSize(cols, rows int) Option {
return func(m *Manager) {
m.termCols = cols
m.termRows = rows
}
}

func newVT10XConsole(t TestingT, cols, rows int, opts ...expect.ConsoleOpt) (*expect.Console, *vt10x.State) {
ptm, pts, err := pty.Open()
require.NoError(t, err)

var state vt10x.State
term, err := vt10x.Create(&state, pts)
require.NoError(t, err)

term.Resize(cols, rows)

c, err := expect.NewConsole(append(opts, expect.WithStdin(ptm), expect.WithStdout(term), expect.WithCloser(pts, ptm, term))...)
require.NoError(t, err)

return c, &state
}
9 changes: 6 additions & 3 deletions manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import (
func TestManager(t *testing.T) {
t.Parallel()

m := consoledog.New(t, consoledog.WithStarter(func(sc *godog.Scenario, console *expect.Console) {
console.Write([]byte(`hello world`)) // nolint: errcheck, gosec
}))
m := consoledog.New(t,
consoledog.WithTermSize(80, 24),
consoledog.WithStarter(func(sc *godog.Scenario, console *expect.Console) {
console.Write([]byte(`hello world`)) // nolint: errcheck, gosec
}),
)

scenario := &godog.Scenario{}
_, state := m.NewConsole(scenario)
Expand Down

0 comments on commit 25ef3b3

Please sign in to comment.