forked from fd/terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_windows.go
151 lines (127 loc) · 3.26 KB
/
util_windows.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
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
// Copyright 2010 Jonas mg
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// +build !plan9
package terminal
import (
// "fmt"
"path/filepath"
"os"
// "os/signal"
// "strconv"
"syscall"
)
var shellsNotANSI = []string{"cmd.exe", "command.com"}
// SupportANSI checks if the terminal supports ANSI escape sequences.
func SupportANSI() bool {
term := os.Getenv("ComSpec") // full path to the command processor
if term == "" {
return false
}
term = filepath.Base(term)
for _, v := range shellsNotANSI {
if v == term {
return false
}
}
return true
}
// Default values
const (
d_ROW = 24
d_COLUMN = 80
)
/*// #include <unistd.h>
import "C"*/
/*
//C char *ttyname(int fd)
// http://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/ttyname.c;hb=HEAD
// http://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/posix/ttyname.c;hb=HEAD
It uses readlink and the /proc filesystem to query the kernel, and not a
dedicated syscall. Armed with that information, you can probably find the
relevant kernel code which outputs the tty information in a format compatible
with readlink.
// GetName gets the name of a terminal.
func GetName(fd int) (string, error) {
name, errno := C.ttyname(C.int(fd))
if errno != nil {
return "", fmt.Errorf("terminal.TTYName: %s", errno)
}
return C.GoString(name), nil
}
*/
//C int isatty(int fd)
// http://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/posix/isatty.c;hb=HEAD
// IsTerminal returns true if the handler is a terminal.
func IsTerminal(handle syscall.Handle) bool {
var st State
return getConsoleMode(handle, &st) == nil
}
/*
// ReadPassword reads the input until '\n' without echo.
// Returns the number of bytes read.
func ReadPassword(fd int, pass []byte) (n int, err error) {
var oldState, newState State
if err = tcgetattr(fd, &oldState); err != nil {
return 0, err
}
// Turn off echo
newState = oldState
newState.Lflag &^= (ECHO | ECHOE | ECHOK | ECHONL)
if err = tcsetattr(fd, _TCSANOW, &newState); err != nil {
return 0, fmt.Errorf("terminal: could not turn off echo: %s", err)
}
// Block SIGINT & SIGTSTP (CTRL-C, CTRL-Z)
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTSTP)
go func() {
for {
select {
case <-sig:
// ignore
}
}
}()
tmpPass := make([]byte, len(pass))
for i, exit := 0, false; ; i++ { // to store all data read until '\n'
n, err = syscall.Read(fd, tmpPass)
if err != nil {
tcsetattr(fd, _TCSANOW, &oldState)
return 0, err
}
if tmpPass[n-1] == '\n' {
n--
exit = true
}
if i == 0 {
copy(pass, tmpPass[:n])
}
if exit {
if i != 0 {
n = len(pass)
}
break
}
}
tmpPass = tmpPass[:0] // reset
tcsetattr(fd, _TCSANOW, &oldState)
return
}
// WinSizeChan allocates a channel to know when the window size has changed
// through TrapSize.
var WinSizeChan = make(chan byte, 1)
// TrapSize caughts a signal named SIGWINCH whenever the window size changes.
func TrapSize() {
change := make(chan os.Signal, 1)
signal.Notify(change, syscall.SIGWINCH)
go func() {
for {
select {
case <-change:
WinSizeChan <- 1 // Send a signal
}
}
}()
}*/