Skip to content

Commit 0318a2b

Browse files
committed
Added minterm package, a minimal terminal.
It uses GetSize and ReadPassword from ssh/terminal, but avoids all the other stuff we weren't using and that was intercepting all keypresses. So now ctrl-c, ctrl-z etc. should work everywhere and the awful autocomplete hack is no longer needed.
1 parent 1348494 commit 0318a2b

3 files changed

Lines changed: 117 additions & 12 deletions

File tree

‎go/client/terminal.go‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
11
package client
22

33
import (
4-
"github.com/keybase/client/protocol/go"
54
"io"
5+
6+
"github.com/keybase/client/go/minterm"
7+
keybase1 "github.com/keybase/client/protocol/go"
68
)
79

810
type Terminal struct {
9-
engine *TerminalEngine
11+
engine *minterm.MinTerm
1012
}
1113

12-
func NewTerminal() *Terminal {
13-
return &Terminal{NewTerminalEngine()}
14-
}
15-
func (t Terminal) Startup() error {
16-
return t.engine.Startup()
17-
}
18-
func (t Terminal) Init() error {
19-
return t.engine.Init()
14+
func NewTerminal() (*Terminal, error) {
15+
eng, err := minterm.New()
16+
if err != nil {
17+
return nil, err
18+
}
19+
return &Terminal{engine: eng}, nil
2020
}
21+
2122
func (t Terminal) Shutdown() error {
2223
return t.engine.Shutdown()
2324
}
25+
2426
func (t Terminal) PromptPassword(s string) (string, error) {
2527
return t.engine.PromptPassword(s)
2628
}
29+
2730
func (t Terminal) Write(s string) error {
2831
return t.engine.Write(s)
2932
}
33+
3034
func (t Terminal) Prompt(s string) (string, error) {
3135
return t.engine.Prompt(s)
3236
}
37+
3338
func (t Terminal) GetSize() (int, int) {
34-
return t.engine.GetSize()
39+
return t.engine.Size()
3540
}
3641

3742
func (t Terminal) GetSecret(arg *keybase1.SecretEntryArg) (res *keybase1.SecretEntryRes, err error) {

‎go/client/ui.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,11 @@ func (ui SecretUI) GetSecret(pinentry keybase1.SecretEntryArg, term *keybase1.Se
604604
}
605605

606606
func (ui *UI) Configure() error {
607-
ui.Terminal = NewTerminal()
607+
t, err := NewTerminal()
608+
if err != nil {
609+
return err
610+
}
611+
ui.Terminal = t
608612
ui.SecretEntry = NewSecretEntry(ui.Terminal)
609613
return nil
610614
}

‎go/minterm/minterm.go‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
2+
3+
// Package minterm implements minimal terminal functions.
4+
package minterm
5+
6+
import (
7+
"bufio"
8+
"fmt"
9+
"os"
10+
"strings"
11+
12+
"golang.org/x/crypto/ssh/terminal"
13+
)
14+
15+
// MinTerm is a minimal terminal interface.
16+
type MinTerm struct {
17+
tty *os.File
18+
width int
19+
height int
20+
}
21+
22+
// New creates a new MinTerm and opens the terminal file. Any
23+
// errors that happen while opening or getting the terminal size
24+
// are returned.
25+
func New() (*MinTerm, error) {
26+
m := &MinTerm{}
27+
if err := m.open(); err != nil {
28+
return nil, err
29+
}
30+
return m, nil
31+
}
32+
33+
func (m *MinTerm) open() error {
34+
f, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
35+
if err != nil {
36+
return err
37+
}
38+
m.tty = f
39+
fd := int(m.tty.Fd())
40+
w, h, err := terminal.GetSize(fd)
41+
if err != nil {
42+
return err
43+
}
44+
m.width, m.height = w, h
45+
return nil
46+
}
47+
48+
// Shutdown closes the terminal.
49+
func (m *MinTerm) Shutdown() error {
50+
if m.tty == nil {
51+
return nil
52+
}
53+
return m.tty.Close()
54+
}
55+
56+
// Size returns the width and height of the terminal.
57+
func (m *MinTerm) Size() (int, int) {
58+
return m.width, m.height
59+
}
60+
61+
// Write writes a string to the terminal.
62+
func (m *MinTerm) Write(s string) error {
63+
_, err := fmt.Fprint(m.tty, s)
64+
return err
65+
}
66+
67+
// Prompt gets a line of input from the terminal. It displays the text in
68+
// the prompt parameter first.
69+
func (m *MinTerm) Prompt(prompt string) (string, error) {
70+
m.Write(prompt)
71+
r := bufio.NewReader(m.tty)
72+
p, err := r.ReadString('\n')
73+
if err != nil {
74+
return "", err
75+
}
76+
// strip off the trailing newline
77+
if len(p) > 0 {
78+
p = p[:len(p)-1]
79+
}
80+
return p, nil
81+
}
82+
83+
// PromptPassword gets a line of input from the terminal, but
84+
// nothing is echoed to the terminal to hide the text.
85+
func (m *MinTerm) PromptPassword(prompt string) (string, error) {
86+
m.Write(prompt)
87+
if !strings.HasSuffix(prompt, ": ") {
88+
m.Write(": ")
89+
}
90+
b, err := terminal.ReadPassword(int(m.tty.Fd()))
91+
if err != nil {
92+
return "", err
93+
}
94+
m.Write("\n")
95+
return string(b), nil
96+
}

0 commit comments

Comments
 (0)