9 "github.com/zyedidia/micro/v2/internal/config"
10 "github.com/zyedidia/micro/v2/internal/util"
11 "github.com/zyedidia/tcell/v2"
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
20 var Screen tcell.Screen
22 // Events is the channel of tcell events
23 var Events chan (tcell.Event)
25 // The lock is necessary since the screen is polled on a separate thread
28 // drawChan is a channel that will cause the screen to redraw when
29 // written to even if no event user event has occurred
30 var drawChan chan bool
32 // Lock locks the screen lock
37 // Unlock unlocks the screen lock
42 // Redraw schedules a redraw with the draw channel
45 case drawChan <- true:
51 // DrawChan returns the draw channel
52 func DrawChan() chan bool {
56 type screenCell struct {
63 var lastCursor screenCell
65 // ShowFakeCursor displays a cursor at the given position by modifying the
66 // style of the given column instead of actually using the terminal cursor
67 // This can be useful in certain terminals such as the windows console where
68 // modifying the cursor location is slow and frequent modifications cause flashing
69 // This keeps track of the most recent fake cursor location and resets it when
70 // a new fake cursor location is specified
71 func ShowFakeCursor(x, y int) {
72 r, combc, style, _ := Screen.GetContent(x, y)
73 Screen.SetContent(lastCursor.x, lastCursor.y, lastCursor.r, lastCursor.combc, lastCursor.style)
74 Screen.SetContent(x, y, r, combc, config.DefStyle.Reverse(true))
76 lastCursor.x, lastCursor.y = x, y
78 lastCursor.combc = combc
79 lastCursor.style = style
82 // ShowFakeCursorMulti is the same as ShowFakeCursor except it does not
83 // reset previous locations of the cursor
84 // Fake cursors are also necessary to display multiple cursors
85 func ShowFakeCursorMulti(x, y int) {
86 r, _, _, _ := Screen.GetContent(x, y)
87 Screen.SetContent(x, y, r, nil, config.DefStyle.Reverse(true))
90 // ShowCursor puts the cursor at the given location using a fake cursor
91 // if enabled or using the terminal cursor otherwise
92 // By default only the windows console will use a fake cursor
93 func ShowCursor(x, y int) {
97 Screen.ShowCursor(x, y)
101 // SetContent sets a cell at a point on the screen and makes sure that it is
102 // synced with the last cursor location
103 func SetContent(x, y int, mainc rune, combc []rune, style tcell.Style) {
104 if !Screen.CanDisplay(mainc, true) {
108 Screen.SetContent(x, y, mainc, combc, style)
109 if util.FakeCursor && lastCursor.x == x && lastCursor.y == y {
111 lastCursor.style = style
112 lastCursor.combc = combc
116 // TempFini shuts the screen down temporarily
117 func TempFini() bool {
118 screenWasNil := Screen == nil
128 // TempStart restarts the screen after it was temporarily disabled
129 func TempStart(screenWasNil bool) {
136 // Init creates and initializes the tcell screen
138 drawChan = make(chan bool, 8)
140 // Should we enable true color?
141 truecolor := os.Getenv("MICRO_TRUECOLOR") == "1"
144 os.Setenv("TCELL_TRUECOLOR", "disable")
148 modifiedTerm := false
150 oldTerm = os.Getenv("TERM")
151 os.Setenv("TERM", "xterm-256color")
155 if config.GetGlobalOption("xterm").(bool) {
161 Screen, err = tcell.NewScreen()
163 log.Println("Warning: during screen initialization:", err)
164 log.Println("Falling back to TERM=xterm-256color")
166 Screen, err = tcell.NewScreen()
171 if err = Screen.Init(); err != nil {
175 Screen.SetPaste(config.GetGlobalOption("paste").(bool))
179 os.Setenv("TERM", oldTerm)
182 if config.GetGlobalOption("mouse").(bool) {
189 // InitSimScreen initializes a simulation screen for testing purposes
190 func InitSimScreen() (tcell.SimulationScreen, error) {
191 drawChan = make(chan bool, 8)
195 s := tcell.NewSimulationScreen("")
197 return nil, errors.New("Failed to get a simulation screen")
199 if err = s.Init(); err != nil {
206 if config.GetGlobalOption("mouse").(bool) {