Debian Codegraph @

tty/stdin_unix.go

raw symbols SCIP
1 // Copyright 2021 The TCell Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
16 // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris zos
17
18 package tty
19
20 import (
21 "errors"
22 "fmt"
23 "os"
24 "os/signal"
25 "strconv"
26 "syscall"
27 "time"
28
29 "golang.org/x/sys/unix"
30 "golang.org/x/term"
31 )
32
33 // stdIoTty is an implementation of the Tty API based upon stdin/stdout.
34 type stdIoTty struct {
35 fd int
36 in *os.File
37 out *os.File
38 saved *term.State
39 sig chan os.Signal
40 started bool
41 }
42
43 func (tty *stdIoTty) Read(b []byte) (int, error) {
44 return tty.in.Read(b)
45 }
46
47 func (tty *stdIoTty) Write(b []byte) (int, error) {
48 return tty.out.Write(b)
49 }
50
51 func (tty *stdIoTty) Close() error {
52 return nil
53 }
54
55 func (tty *stdIoTty) Start() error {
56 if tty.started {
57 return nil
58 }
59
60 var err error
61 tty.in = os.Stdin
62 tty.out = os.Stdout
63 tty.fd = int(tty.in.Fd())
64
65 if !term.IsTerminal(tty.fd) {
66 return errors.New("device is not a terminal")
67 }
68
69 _ = tty.in.SetReadDeadline(time.Time{})
70 saved, err := term.MakeRaw(tty.fd) // also sets vMin and vTime
71 if err != nil {
72 return err
73 }
74 if err = tcFlushInput(tty.fd); err != nil {
75 _ = term.Restore(tty.fd, saved)
76 return err
77 }
78 tty.saved = saved
79 tty.started = true
80
81 return nil
82 }
83
84 func (tty *stdIoTty) Drain() error {
85 _ = tty.in.SetReadDeadline(time.Now())
86 if err := tcSetBufParams(tty.fd, 0, 0); err != nil {
87 return err
88 }
89 return nil
90 }
91
92 func (tty *stdIoTty) Stop() error {
93 if err := term.Restore(tty.fd, tty.saved); err != nil {
94 return err
95 }
96 _ = tty.in.SetReadDeadline(time.Now())
97
98 tty.NotifyResize(nil)
99
100 tty.started = false
101
102 return nil
103 }
104
105 func (tty *stdIoTty) WindowSize() (WindowSize, error) {
106 size := WindowSize{}
107 ws, err := unix.IoctlGetWinsize(tty.fd, unix.TIOCGWINSZ)
108 if err != nil {
109 return size, err
110 }
111 w := int(ws.Col)
112 h := int(ws.Row)
113 if w == 0 {
114 w, _ = strconv.Atoi(os.Getenv("COLUMNS"))
115 }
116 if w == 0 {
117 w = 80 // default
118 }
119 if h == 0 {
120 h, _ = strconv.Atoi(os.Getenv("LINES"))
121 }
122 if h == 0 {
123 h = 25 // default
124 }
125 size.Width = w
126 size.Height = h
127 size.PixelWidth = int(ws.Xpixel)
128 size.PixelHeight = int(ws.Ypixel)
129 return size, nil
130 }
131
132 func (tty *stdIoTty) NotifyResize(resizeQ chan<- bool) {
133
134 sigQ := tty.sig
135 tty.sig = nil
136
137 if sigQ != nil {
138 signal.Stop(sigQ)
139 close(sigQ)
140 }
141
142 if resizeQ == nil {
143 return
144 }
145
146 sigQ = make(chan os.Signal, 1)
147 signal.Notify(sigQ, syscall.SIGWINCH)
148
149 tty.sig = sigQ
150
151 go func() {
152 for range sigQ {
153 select {
154 case resizeQ <- true:
155 default: // queue full, so nvm.
156 }
157 }
158 }()
159 }
160
161 // NewStdioTty opens a tty using standard input/output.
162 func NewStdIoTty() (Tty, error) {
163 tty := &stdIoTty{
164 sig: make(chan os.Signal),
165 in: os.Stdin,
166 out: os.Stdout,
167 }
168 var err error
169 tty.fd = int(tty.in.Fd())
170 if !term.IsTerminal(tty.fd) {
171 return nil, errors.New("not a terminal")
172 }
173 if tty.saved, err = term.GetState(tty.fd); err != nil {
174 return nil, fmt.Errorf("failed to get state: %w", err)
175 }
176 return tty, nil
177 }