client line handling improvements

master
Jordan Orelli 10 years ago
parent 292313ad13
commit 397f7f8752

@ -78,7 +78,8 @@ func (c *Client) info(template string, args ...interface{}) {
} }
func (c *Client) trunc() { func (c *Client) trunc() {
fmt.Print("\r") fmt.Print("\033[1K") // clear to beginning of the line
fmt.Print("\r") // move to beginning of the line
} }
func (c *Client) err(template string, args ...interface{}) { func (c *Client) err(template string, args ...interface{}) {
@ -105,21 +106,27 @@ func (c *Client) run() {
} }
func (c *Client) renderLine() { func (c *Client) renderLine() {
fmt.Printf("\r%s%s", c.prompt, string(c.line)) fmt.Printf("\033[1K") // clear to beginning of current line
fmt.Printf("\r") // move to beginning of current line
fmt.Printf("%s%s", c.prompt, string(c.line)) // print the line with prompt
} }
func (c *Client) control(r rune) { func (c *Client) control(r rune) {
switch r { switch r {
case 13: // enter
c.enter()
case 12: // ctrl+l
c.clear()
case 3: // ctrl+c case 3: // ctrl+c
c.eof() c.eof()
case 4: // EOF case 4: // EOF
c.eof() c.eof()
case 12: // ctrl+l
c.clear()
case 13: // enter
c.enter()
case 21: // ctrl+u
c.clearLine()
case 127: // backspace
c.backspace()
default: default:
c.info("control: %v %d %c", r, r, r) c.info("undefined control sequence: %v %d %c", r, r, r)
} }
} }
@ -130,7 +137,8 @@ func (c *Client) enter() {
} }
func (c *Client) eof() { func (c *Client) eof() {
fmt.Print("\r") fmt.Print("\033[1K") // clear to beginning of current line
fmt.Print("\r") // move to beginning of current line
c.done <- 1 c.done <- 1
} }
@ -140,6 +148,19 @@ func (c *Client) clear() {
c.renderLine() c.renderLine()
} }
func (c *Client) clearLine() {
c.line = make([]rune, 0, 32)
c.renderLine()
}
func (c *Client) backspace() {
if len(c.line) == 0 {
return
}
c.line = c.line[:len(c.line)-1]
c.renderLine()
}
func (c *Client) term() { func (c *Client) term() {
old, err := terminal.MakeRaw(0) old, err := terminal.MakeRaw(0)
if err != nil { if err != nil {

Loading…
Cancel
Save