term.go 647 Bytes
Newer Older
Jonny Schäfer's avatar
Jonny Schäfer committed
1
2
3
4
5
6
7
8
9
10
package main

import (
	"golang.org/x/crypto/ssh/terminal"
	"io"
	"os"
)

var (
	termTerminal *terminal.Terminal
11
	termRestore  *terminal.State
Jonny Schäfer's avatar
Jonny Schäfer committed
12
13
14
15
16
17
18
19
20
21
22
)

func init() {
	stdio := struct {
		io.Reader
		io.Writer
	}{
		os.Stdin,
		os.Stdout,
	}

23
24
	var err error
	termRestore, err = terminal.MakeRaw(int(os.Stdin.Fd()))
Jonny Schäfer's avatar
Jonny Schäfer committed
25
26
27
28
29
30
31
32
33
	errorCare(err)

	termTerminal = terminal.NewTerminal(stdio, "")
}

// termReadLine reads a line from stdin
func termReadLine() (string, error) {
	return termTerminal.ReadLine()
}
34
35
36
37
38
39
40

// termClose restores the terminal to its original state
func termClose() {
	if termRestore != nil {
		terminal.Restore(int(os.Stdin.Fd()), termRestore)
	}
}