-
Notifications
You must be signed in to change notification settings - Fork 0
/
termecho.go
47 lines (41 loc) · 922 Bytes
/
termecho.go
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
// Copyright (c) 2016 Frank Braun <frank@cryptogroup.net>
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
// termecho is a test program that echos lines typed in a terminal.
package main
import (
"fmt"
"os"
"syscall"
"golang.org/x/crypto/ssh/terminal"
)
func termechoMain() error {
oldState, err := terminal.MakeRaw(int(syscall.Stdin))
if err != nil {
return err
}
defer terminal.Restore(int(syscall.Stdin), oldState)
fmt.Println("enter 'q' to quit")
t := terminal.NewTerminal(os.Stdin, "> ")
for {
line, err := t.ReadLine()
if err != nil {
return err
}
if line == "q" {
break
}
fmt.Println(line)
}
return nil
}
func fatal(err error) {
fmt.Fprintf(os.Stderr, "%s: error: %s\n", os.Args[0], err)
os.Exit(1)
}
func main() {
// work around defer not working after os.Exit()
if err := termechoMain(); err != nil {
fatal(err)
}
}