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