github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/backend/display/internal/terminal/mock.go (about) 1 package terminal 2 3 import ( 4 "io" 5 "sync" 6 ) 7 8 type MockTerminal struct { 9 m sync.Mutex 10 11 width, height int 12 raw bool 13 info Info 14 15 keys chan string 16 17 dest io.Writer 18 } 19 20 func NewMockTerminal(dest io.Writer, width, height int, raw bool) *MockTerminal { 21 return &MockTerminal{ 22 width: width, 23 height: height, 24 raw: raw, 25 info: info{noTermInfo(0)}, 26 keys: make(chan string), 27 dest: dest, 28 } 29 } 30 31 func (t *MockTerminal) IsRaw() bool { 32 return t.raw 33 } 34 35 func (t *MockTerminal) Close() error { 36 close(t.keys) 37 return nil 38 } 39 40 func (t *MockTerminal) Size() (width, height int, err error) { 41 t.m.Lock() 42 defer t.m.Unlock() 43 44 return t.width, t.height, nil 45 } 46 47 func (t *MockTerminal) Write(b []byte) (int, error) { 48 return t.dest.Write(b) 49 } 50 51 func (t *MockTerminal) ClearLine() { 52 t.info.ClearLine(t) 53 } 54 55 func (t *MockTerminal) CursorUp(count int) { 56 t.info.CursorUp(t, count) 57 } 58 59 func (t *MockTerminal) CursorDown(count int) { 60 t.info.CursorDown(t, count) 61 } 62 63 func (t *MockTerminal) ReadKey() (string, error) { 64 k, ok := <-t.keys 65 if !ok { 66 return "", io.EOF 67 } 68 return k, nil 69 } 70 71 func (t *MockTerminal) SetSize(width, height int) { 72 t.m.Lock() 73 defer t.m.Unlock() 74 75 t.width, t.height = width, height 76 } 77 78 func (t *MockTerminal) SendKey(key string) { 79 t.keys <- key 80 }