-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil_darwin.go
36 lines (30 loc) · 1.14 KB
/
util_darwin.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
package main
import (
"bufio"
"golang.org/x/sys/unix"
"os"
"strings"
)
const ioctlReadTermios = unix.TIOCGETA
const ioctlWriteTermios = unix.TIOCSETA
func readTokens() string {
// putting the terminal in noncanonical mode, as on macos, in canonical mode, the max length of
// a line is 1024 characters, which has the effect that only tokens less than 1023 characters can be read in canonical mode.
// Here are some useful links:
// https://unix.stackexchange.com/questions/204815/terminal-does-not-accept-pasted-or-typed-lines-of-more-than-1024-characters
// https://linux.die.net/man/1/stty
stdinFd := int(os.Stdin.Fd())
termios, err := unix.IoctlGetTermios(stdinFd, ioctlReadTermios)
if err != nil {
logger.Fatalf("error: cannot read token from terminal: %v", err)
}
defer unix.IoctlSetTermios(stdinFd, ioctlWriteTermios, termios)
newState := *termios
newState.Lflag &^= unix.ICANON
if err := unix.IoctlSetTermios(stdinFd, ioctlWriteTermios, &newState); err != nil {
logger.Fatalf("error: cannot read token from terminal: %v", err)
}
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
return strings.TrimSpace(scanner.Text())
}