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