File: readline.go

package info (click to toggle)
golang-github-mattn-go-tty 0.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 128 kB
  • sloc: makefile: 3
file content (210 lines) | stat: -rw-r--r-- 3,699 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package ttyutil

import (
	"bytes"
	"fmt"
	"io"
	"os"
	"os/signal"

	"github.com/mattn/go-colorable"
	"github.com/mattn/go-runewidth"
	"github.com/mattn/go-tty"
)

type ctx struct {
	w        io.Writer
	input    []rune
	last     []rune
	prompt   string
	cursor_x int
	old_row  int
	old_crow int
	size     int
}

func (c *ctx) redraw(dirty bool, passwordChar rune) error {
	var buf bytes.Buffer

	buf.WriteString("\x1b[5>h")

	buf.WriteString("\x1b[1G")
	if dirty {
		buf.WriteString("\x1b[0K")
	}
	for i := 0; i < c.old_row-c.old_crow; i++ {
		buf.WriteString("\x1b[B")
	}
	for i := 0; i < c.old_row; i++ {
		if dirty {
			buf.WriteString("\x1b[2K")
		}
		buf.WriteString("\x1b[A")
	}

	var rs []rune
	if passwordChar != 0 {
		for i := 0; i < len(c.input); i++ {
			rs = append(rs, passwordChar)
		}
	} else {
		rs = c.input
	}

	ccol, crow, col, row := -1, 0, 0, 0
	plen := len([]rune(c.prompt))
	for i, r := range []rune(c.prompt + string(rs)) {
		if i == plen+c.cursor_x {
			ccol = col
			crow = row
		}
		rw := runewidth.RuneWidth(r)
		if col+rw > c.size {
			col = 0
			row++
			if dirty {
				buf.WriteString("\n\r\x1b[0K")
			}
		}
		if dirty {
			buf.WriteString(string(r))
		}
		col += rw
	}
	if dirty {
		buf.WriteString("\x1b[1G")
		for i := 0; i < row; i++ {
			buf.WriteString("\x1b[A")
		}
	}
	if ccol == -1 {
		ccol = col
		crow = row
	}
	for i := 0; i < crow; i++ {
		buf.WriteString("\x1b[B")
	}
	buf.WriteString(fmt.Sprintf("\x1b[%dG", ccol+1))

	buf.WriteString("\x1b[5>l")
	io.Copy(c.w, &buf)

	c.old_row = row
	c.old_crow = crow

	return nil
}

func ReadLine(tty *tty.TTY) (string, error) {
	c := new(ctx)
	c.w = colorable.NewColorableStdout()
	quit := false
	sc := make(chan os.Signal, 1)
	signal.Notify(sc, os.Interrupt)
	go func() {
		<-sc
		c.input = nil
		quit = true
	}()
	c.size = 80

	dirty := true
loop:
	for !quit {
		err := c.redraw(dirty, 0)
		if err != nil {
			return "", err
		}
		dirty = false

		r, err := tty.ReadRune()
		if err != nil {
			break
		}
		switch r {
		case 0:
		case 1: // CTRL-A
			c.cursor_x = 0
		case 2: // CTRL-B
			if c.cursor_x > 0 {
				c.cursor_x--
			}
		case 3: // BREAK
			return "", nil
		case 4: // CTRL-D
			if len(c.input) > 0 {
				continue
			}
			return "", io.EOF
		case 5: // CTRL-E
			c.cursor_x = len(c.input)
		case 6: // CTRL-F
			if c.cursor_x < len(c.input) {
				c.cursor_x++
			}
		case 8, 0x7F: // BS
			if c.cursor_x > 0 {
				c.input = append(c.input[0:c.cursor_x-1], c.input[c.cursor_x:len(c.input)]...)
				c.cursor_x--
				dirty = true
			}
		case 27:
			if !tty.Buffered() {
				return "", io.EOF
			}
			r, err = tty.ReadRune()
			if err == nil && r == 0x5b {
				r, err = tty.ReadRune()
				if err != nil {
					panic(err)
				}
				switch r {
				case 'C':
					if c.cursor_x < len(c.input) {
						c.cursor_x++
					}
				case 'D':
					if c.cursor_x > 0 {
						c.cursor_x--
					}
				}
			}
		case 10: // LF
			break loop
		case 11: // CTRL-K
			c.input = c.input[:c.cursor_x]
			dirty = true
		case 12: // CTRL-L
			dirty = true
		case 13: // CR
			break loop
		case 21: // CTRL-U
			c.input = c.input[c.cursor_x:]
			c.cursor_x = 0
			dirty = true
		case 23: // CTRL-W
			for i := len(c.input) - 1; i >= 0; i-- {
				if i == 0 || c.input[i] == ' ' || c.input[i] == '\t' {
					c.input = append(c.input[:i], c.input[c.cursor_x:]...)
					c.cursor_x = i
					dirty = true
					break
				}
			}
		default:
			tmp := []rune{}
			tmp = append(tmp, c.input[0:c.cursor_x]...)
			tmp = append(tmp, r)
			c.input = append(tmp, c.input[c.cursor_x:len(c.input)]...)
			c.cursor_x++
			dirty = true
		}
	}
	os.Stdout.WriteString("\n")

	if c.input == nil {
		return "", io.EOF
	}

	return string(c.input), nil
}