]> git.lizzy.rs Git - micro.git/blob - internal/screen/screen.go
Merge pull request #1412 from tommyshem/batSyntaxHighlighting
[micro.git] / internal / screen / screen.go
1 package screen
2
3 import (
4         "fmt"
5         "os"
6         "sync"
7
8         "github.com/zyedidia/micro/internal/config"
9         "github.com/zyedidia/micro/pkg/terminfo"
10         "github.com/zyedidia/tcell"
11 )
12
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
18 // same time too.
19 var Screen tcell.Screen
20 var lock sync.Mutex
21 var DrawChan chan bool
22
23 func Lock() {
24         lock.Lock()
25 }
26
27 func Unlock() {
28         lock.Unlock()
29 }
30
31 func Redraw() {
32         DrawChan <- true
33 }
34
35 // TempFini shuts the screen down temporarily
36 func TempFini() bool {
37         screenWasNil := Screen == nil
38
39         if !screenWasNil {
40                 Screen.Fini()
41                 Lock()
42                 Screen = nil
43         }
44         return screenWasNil
45 }
46
47 // TempStart restarts the screen after it was temporarily disabled
48 func TempStart(screenWasNil bool) {
49         if !screenWasNil {
50                 Init()
51                 Unlock()
52         }
53 }
54
55 // Init creates and initializes the tcell screen
56 func Init() {
57         DrawChan = make(chan bool, 8)
58
59         // Should we enable true color?
60         truecolor := os.Getenv("MICRO_TRUECOLOR") == "1"
61
62         tcelldb := os.Getenv("TCELLDB")
63         os.Setenv("TCELLDB", config.ConfigDir+"/.tcelldb")
64
65         // In order to enable true color, we have to set the TERM to `xterm-truecolor` when
66         // initializing tcell, but after that, we can set the TERM back to whatever it was
67         oldTerm := os.Getenv("TERM")
68         if truecolor {
69                 os.Setenv("TERM", "xterm-truecolor")
70         }
71
72         // Initilize tcell
73         var err error
74         Screen, err = tcell.NewScreen()
75         if err != nil {
76                 if err == tcell.ErrTermNotFound {
77                         err = terminfo.WriteDB(config.ConfigDir + "/.tcelldb")
78                         if err != nil {
79                                 fmt.Println(err)
80                                 fmt.Println("Fatal: Micro could not create terminal database file", config.ConfigDir+"/.tcelldb")
81                                 os.Exit(1)
82                         }
83                         Screen, err = tcell.NewScreen()
84                         if err != nil {
85                                 fmt.Println(err)
86                                 fmt.Println("Fatal: Micro could not initialize a Screen.")
87                                 os.Exit(1)
88                         }
89                 } else {
90                         fmt.Println(err)
91                         fmt.Println("Fatal: Micro could not initialize a Screen.")
92                         os.Exit(1)
93                 }
94         }
95         if err = Screen.Init(); err != nil {
96                 fmt.Println(err)
97                 os.Exit(1)
98         }
99
100         // Now we can put the TERM back to what it was before
101         if truecolor {
102                 os.Setenv("TERM", oldTerm)
103         }
104
105         if config.GetGlobalOption("mouse").(bool) {
106                 Screen.EnableMouse()
107         }
108
109         os.Setenv("TCELLDB", tcelldb)
110 }