8 "github.com/zyedidia/micro/v2/internal/config"
9 "github.com/zyedidia/micro/v2/internal/util"
10 "github.com/zyedidia/tcell"
13 // Screen is the tcell screen we use to draw to the terminal
14 // Synchronization is used because we poll the screen on a separate
15 // thread and sometimes the screen is shut down by the main thread
16 // (for example on TermMessage) so we don't want to poll a nil/shutdown
17 // screen. TODO: maybe we should worry about polling and drawing at the
19 var Screen tcell.Screen
21 // The lock is necessary since the screen is polled on a separate thread
24 // drawChan is a channel that will cause the screen to redraw when
25 // written to even if no event user event has occurred
26 var drawChan chan bool
28 // Lock locks the screen lock
33 // Unlock unlocks the screen lock
38 // Redraw schedules a redraw with the draw channel
41 case drawChan <- true:
47 // DrawChan returns the draw channel
48 func DrawChan() chan bool {
52 type screenCell struct {
59 var lastCursor screenCell
61 // ShowFakeCursor displays a cursor at the given position by modifying the
62 // style of the given column instead of actually using the terminal cursor
63 // This can be useful in certain terminals such as the windows console where
64 // modifying the cursor location is slow and frequent modifications cause flashing
65 // This keeps track of the most recent fake cursor location and resets it when
66 // a new fake cursor location is specified
67 func ShowFakeCursor(x, y int) {
68 r, combc, style, _ := Screen.GetContent(x, y)
69 Screen.SetContent(lastCursor.x, lastCursor.y, lastCursor.r, lastCursor.combc, lastCursor.style)
70 Screen.SetContent(x, y, r, combc, config.DefStyle.Reverse(true))
72 lastCursor.x, lastCursor.y = x, y
74 lastCursor.combc = combc
75 lastCursor.style = style
78 // ShowFakeCursorMulti is the same as ShowFakeCursor except it does not
79 // reset previous locations of the cursor
80 // Fake cursors are also necessary to display multiple cursors
81 func ShowFakeCursorMulti(x, y int) {
82 r, _, _, _ := Screen.GetContent(x, y)
83 Screen.SetContent(x, y, r, nil, config.DefStyle.Reverse(true))
86 // ShowCursor puts the cursor at the given location using a fake cursor
87 // if enabled or using the terminal cursor otherwise
88 // By default only the windows console will use a fake cursor
89 func ShowCursor(x, y int) {
93 Screen.ShowCursor(x, y)
97 // SetContent sets a cell at a point on the screen and makes sure that it is
98 // synced with the last cursor location
99 func SetContent(x, y int, mainc rune, combc []rune, style tcell.Style) {
100 Screen.SetContent(x, y, mainc, combc, style)
101 if util.FakeCursor && lastCursor.x == x && lastCursor.y == y {
103 lastCursor.style = style
104 lastCursor.combc = combc
108 // TempFini shuts the screen down temporarily
109 func TempFini() bool {
110 screenWasNil := Screen == nil
120 // TempStart restarts the screen after it was temporarily disabled
121 func TempStart(screenWasNil bool) {
128 // Init creates and initializes the tcell screen
130 drawChan = make(chan bool, 8)
132 // Should we enable true color?
133 truecolor := os.Getenv("MICRO_TRUECOLOR") == "1"
136 os.Setenv("TCELL_TRUECOLOR", "disable")
140 if config.GetGlobalOption("xterm").(bool) {
141 oldTerm = os.Getenv("TERM")
142 os.Setenv("TERM", "xterm-256color")
147 Screen, err = tcell.NewScreen()
150 fmt.Println("Fatal: Micro could not initialize a Screen.")
153 if err = Screen.Init(); err != nil {
159 if config.GetGlobalOption("xterm").(bool) {
160 os.Setenv("TERM", oldTerm)
163 if config.GetGlobalOption("mouse").(bool) {