Debian Codegraph @

debian/vendor/github.com/micro-editor/tcell/v2/tscreen_unix.go

raw
1 // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris zos
2
3 // Copyright 2019 The TCell Authors
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use file except in compliance with the License.
7 // You may obtain a copy of the license at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 package tcell
18
19 import (
20 "os"
21 "os/signal"
22 "strconv"
23 "syscall"
24
25 "golang.org/x/sys/unix"
26 "golang.org/x/term"
27 )
28
29 func (t *tScreen) termioInit() error {
30 var e error
31 var state *term.State
32
33 if t.in, t.out, e = openTty(); e != nil {
34 goto failed
35 }
36
37 state, e = term.MakeRaw(t.fd())
38 if e != nil {
39 goto failed
40 }
41
42 t.saved = state
43
44 signal.Notify(t.sigwinch, syscall.SIGWINCH)
45
46 if w, h, e := t.getWinSize(); e == nil && w != 0 && h != 0 {
47 t.cells.Resize(w, h)
48 }
49
50 return nil
51
52 failed:
53 if t.in != nil {
54 closeTty(t.in)
55 }
56 if t.out != nil {
57 closeTty(t.out)
58 }
59 return e
60 }
61
62 func (t *tScreen) termioFini() {
63
64 signal.Stop(t.sigwinch)
65
66 <-t.indoneq
67
68 if t.out != nil && t.saved != nil {
69 term.Restore(t.fd(), t.saved)
70 closeTty(t.out)
71 }
72
73 if t.in != nil {
74 closeTty(t.in)
75 }
76 }
77
78 func (t *tScreen) getWinSize() (int, int, error) {
79
80 wsz, err := unix.IoctlGetWinsize(t.fd(), unix.TIOCGWINSZ)
81 if err != nil {
82 return -1, -1, err
83 }
84 cols := int(wsz.Col)
85 rows := int(wsz.Row)
86 if cols == 0 {
87 colsEnv := os.Getenv("COLUMNS")
88 if colsEnv != "" {
89 if cols, err = strconv.Atoi(colsEnv); err != nil {
90 return -1, -1, err
91 }
92 } else {
93 cols = t.ti.Columns
94 }
95 }
96 if rows == 0 {
97 rowsEnv := os.Getenv("LINES")
98 if rowsEnv != "" {
99 if rows, err = strconv.Atoi(rowsEnv); err != nil {
100 return -1, -1, err
101 }
102 } else {
103 rows = t.ti.Lines
104 }
105 }
106 return cols, rows, nil
107 }
108
109 func (t *tScreen) Beep() error {
110 t.writeString(string(byte(7)))
111 return nil
112 }