Debian Codegraph @

internal/screen/screen.go

raw
1 package screen
2
3 import (
4 "errors"
5 "log"
6 "os"
7 "sync"
8
9 "github.com/micro-editor/tcell/v2"
10 "github.com/zyedidia/micro/v2/internal/config"
11 "github.com/zyedidia/micro/v2/internal/util"
12 )
13
14 // Screen is the tcell screen we use to draw to the terminal
15 // Synchronization is used because we poll the screen on a separate
16 // thread and sometimes the screen is shut down by the main thread
17 // (for example on TermMessage) so we don't want to poll a nil/shutdown
18 // screen. TODO: maybe we should worry about polling and drawing at the
19 // same time too.
20 var Screen tcell.Screen
21
22 // Events is the channel of tcell events
23 var Events chan (tcell.Event)
24
25 // RestartCallback is called when the screen is restarted after it was
26 // temporarily shut down
27 var RestartCallback func()
28
29 // The lock is necessary since the screen is polled on a separate thread
30 var lock sync.Mutex
31
32 // drawChan is a channel that will cause the screen to redraw when
33 // written to even if no event user event has occurred
34 var drawChan chan bool
35
36 // rawSeq is the list of raw escape sequences that are bound to some actions
37 // via keybindings and thus should be parsed by tcell. We need to register
38 // them in tcell every time we reinitialize the screen, so we need to remember
39 // them in a list
40 var rawSeq = make([]string, 0)
41
42 // Lock locks the screen lock
43 func Lock() {
44 lock.Lock()
45 }
46
47 // Unlock unlocks the screen lock
48 func Unlock() {
49 lock.Unlock()
50 }
51
52 // Redraw schedules a redraw with the draw channel
53 func Redraw() {
54 select {
55 case drawChan <- true:
56 default:
57 // channel is full
58 }
59 }
60
61 // DrawChan returns the draw channel
62 func DrawChan() chan bool {
63 return drawChan
64 }
65
66 type screenCell struct {
67 x, y int
68 r rune
69 combc []rune
70 style tcell.Style
71 }
72
73 var lastCursor screenCell
74
75 // ShowFakeCursor displays a cursor at the given position by modifying the
76 // style of the given column instead of actually using the terminal cursor
77 // This can be useful in certain terminals such as the windows console where
78 // modifying the cursor location is slow and frequent modifications cause flashing
79 // This keeps track of the most recent fake cursor location and resets it when
80 // a new fake cursor location is specified
81 func ShowFakeCursor(x, y int) {
82 r, combc, style, _ := Screen.GetContent(x, y)
83 Screen.SetContent(lastCursor.x, lastCursor.y, lastCursor.r, lastCursor.combc, lastCursor.style)
84 Screen.SetContent(x, y, r, combc, config.DefStyle.Reverse(true))
85
86 lastCursor.x, lastCursor.y = x, y
87 lastCursor.r = r
88 lastCursor.combc = combc
89 lastCursor.style = style
90 }
91
92 func UseFake() bool {
93 return util.FakeCursor || config.GetGlobalOption("fakecursor").(bool)
94 }
95
96 // ShowFakeCursorMulti is the same as ShowFakeCursor except it does not
97 // reset previous locations of the cursor
98 // Fake cursors are also necessary to display multiple cursors
99 func ShowFakeCursorMulti(x, y int) {
100 r, _, _, _ := Screen.GetContent(x, y)
101 Screen.SetContent(x, y, r, nil, config.DefStyle.Reverse(true))
102 }
103
104 // ShowCursor puts the cursor at the given location using a fake cursor
105 // if enabled or using the terminal cursor otherwise
106 // By default only the windows console will use a fake cursor
107 func ShowCursor(x, y int) {
108 if UseFake() {
109 ShowFakeCursor(x, y)
110 } else {
111 Screen.ShowCursor(x, y)
112 }
113 }
114
115 // SetContent sets a cell at a point on the screen and makes sure that it is
116 // synced with the last cursor location
117 func SetContent(x, y int, mainc rune, combc []rune, style tcell.Style) {
118 if !Screen.CanDisplay(mainc, true) {
119 mainc = '�'
120 }
121
122 Screen.SetContent(x, y, mainc, combc, style)
123 if UseFake() && lastCursor.x == x && lastCursor.y == y {
124 lastCursor.r = mainc
125 lastCursor.style = style
126 lastCursor.combc = combc
127 }
128 }
129
130 // RegisterRawSeq registers a raw escape sequence that should be parsed by tcell
131 func RegisterRawSeq(r string) {
132 for _, seq := range rawSeq {
133 if seq == r {
134 return
135 }
136 }
137 rawSeq = append(rawSeq, r)
138
139 if Screen != nil {
140 Screen.RegisterRawSeq(r)
141 }
142 }
143
144 // UnregisterRawSeq unregisters a raw escape sequence that should be parsed by tcell
145 func UnregisterRawSeq(r string) {
146 for i, seq := range rawSeq {
147 if seq == r {
148 rawSeq[i] = rawSeq[len(rawSeq)-1]
149 rawSeq = rawSeq[:len(rawSeq)-1]
150 }
151 }
152
153 if Screen != nil {
154 Screen.UnregisterRawSeq(r)
155 }
156 }
157
158 // TempFini shuts the screen down temporarily
159 func TempFini() bool {
160 screenWasNil := Screen == nil
161
162 if !screenWasNil {
163 Screen.Fini()
164 Lock()
165 Screen = nil
166 }
167 return screenWasNil
168 }
169
170 // TempStart restarts the screen after it was temporarily disabled
171 func TempStart(screenWasNil bool) {
172 if !screenWasNil {
173 Init()
174 Unlock()
175
176 if RestartCallback != nil {
177 RestartCallback()
178 }
179 }
180 }
181
182 // Init creates and initializes the tcell screen
183 func Init() error {
184 drawChan = make(chan bool, 8)
185
186 // Should we enable true color?
187 truecolor := config.GetGlobalOption("truecolor").(string)
188 if truecolor == "on" || (truecolor == "auto" && os.Getenv("MICRO_TRUECOLOR") == "1") {
189 os.Setenv("TCELL_TRUECOLOR", "enable")
190 } else if truecolor == "off" {
191 os.Setenv("TCELL_TRUECOLOR", "disable")
192 } else {
193 // For "auto", tcell already autodetects truecolor by default
194 }
195
196 var oldTerm string
197 modifiedTerm := false
198 setXterm := func() {
199 oldTerm = os.Getenv("TERM")
200 os.Setenv("TERM", "xterm-256color")
201 modifiedTerm = true
202 }
203
204 if config.GetGlobalOption("xterm").(bool) {
205 setXterm()
206 }
207
208 // Initilize tcell
209 var err error
210 Screen, err = tcell.NewScreen()
211 if err != nil {
212 log.Println("Warning: during screen initialization:", err)
213 log.Println("Falling back to TERM=xterm-256color")
214 setXterm()
215 Screen, err = tcell.NewScreen()
216 if err != nil {
217 return err
218 }
219 }
220 if err = Screen.Init(); err != nil {
221 return err
222 }
223
224 Screen.SetPaste(config.GetGlobalOption("paste").(bool))
225
226 // restore TERM
227 if modifiedTerm {
228 os.Setenv("TERM", oldTerm)
229 }
230
231 if config.GetGlobalOption("mouse").(bool) {
232 Screen.EnableMouse()
233 }
234
235 for _, r := range rawSeq {
236 Screen.RegisterRawSeq(r)
237 }
238
239 return nil
240 }
241
242 // InitSimScreen initializes a simulation screen for testing purposes
243 func InitSimScreen() (tcell.SimulationScreen, error) {
244 drawChan = make(chan bool, 8)
245
246 // Initilize tcell
247 var err error
248 s := tcell.NewSimulationScreen("")
249 if s == nil {
250 return nil, errors.New("Failed to get a simulation screen")
251 }
252 if err = s.Init(); err != nil {
253 return nil, err
254 }
255
256 s.SetSize(80, 24)
257 Screen = s
258
259 if config.GetGlobalOption("mouse").(bool) {
260 Screen.EnableMouse()
261 }
262
263 return s, nil
264 }