micro/internal/screen/screen.go

111 lines
2.4 KiB
Go
Raw Normal View History

2018-08-27 19:53:10 +00:00
package screen
import (
"fmt"
"os"
2018-08-28 18:24:59 +00:00
"sync"
2018-08-27 19:53:10 +00:00
2019-02-04 04:17:24 +00:00
"github.com/zyedidia/micro/internal/config"
"github.com/zyedidia/micro/pkg/terminfo"
2018-08-27 19:53:10 +00:00
"github.com/zyedidia/tcell"
)
2018-12-31 19:46:04 +00:00
// Screen is the tcell screen we use to draw to the terminal
// Synchronization is used because we poll the screen on a separate
// thread and sometimes the screen is shut down by the main thread
// (for example on TermMessage) so we don't want to poll a nil/shutdown
// screen. TODO: maybe we should worry about polling and drawing at the
// same time too.
2018-08-27 19:53:10 +00:00
var Screen tcell.Screen
2018-08-28 18:24:59 +00:00
var lock sync.Mutex
2019-01-10 21:37:05 +00:00
var DrawChan chan bool
2018-08-28 18:24:59 +00:00
func Lock() {
lock.Lock()
}
func Unlock() {
lock.Unlock()
}
2019-01-10 21:37:05 +00:00
func Redraw() {
DrawChan <- true
}
2018-08-28 18:24:59 +00:00
2018-12-31 19:46:04 +00:00
// TempFini shuts the screen down temporarily
2019-01-10 21:37:05 +00:00
func TempFini() bool {
screenWasNil := Screen == nil
2018-08-28 18:24:59 +00:00
if !screenWasNil {
Screen.Fini()
2019-01-10 21:37:05 +00:00
Lock()
2018-08-28 18:24:59 +00:00
Screen = nil
}
2019-01-10 21:37:05 +00:00
return screenWasNil
2018-08-28 18:24:59 +00:00
}
2018-12-31 19:46:04 +00:00
// TempStart restarts the screen after it was temporarily disabled
2019-01-10 21:37:05 +00:00
func TempStart(screenWasNil bool) {
2018-08-28 18:24:59 +00:00
if !screenWasNil {
Init()
Unlock()
}
}
2018-08-27 19:53:10 +00:00
// Init creates and initializes the tcell screen
func Init() {
2019-01-15 00:57:19 +00:00
DrawChan = make(chan bool, 8)
2019-01-10 21:37:05 +00:00
2018-08-27 19:53:10 +00:00
// Should we enable true color?
truecolor := os.Getenv("MICRO_TRUECOLOR") == "1"
tcelldb := os.Getenv("TCELLDB")
os.Setenv("TCELLDB", config.ConfigDir+"/.tcelldb")
// In order to enable true color, we have to set the TERM to `xterm-truecolor` when
// initializing tcell, but after that, we can set the TERM back to whatever it was
oldTerm := os.Getenv("TERM")
if truecolor {
os.Setenv("TERM", "xterm-truecolor")
}
// Initilize tcell
var err error
Screen, err = tcell.NewScreen()
if err != nil {
if err == tcell.ErrTermNotFound {
err = terminfo.WriteDB(config.ConfigDir + "/.tcelldb")
if err != nil {
fmt.Println(err)
fmt.Println("Fatal: Micro could not create terminal database file", config.ConfigDir+"/.tcelldb")
os.Exit(1)
}
Screen, err = tcell.NewScreen()
if err != nil {
fmt.Println(err)
2018-08-27 21:55:28 +00:00
fmt.Println("Fatal: Micro could not initialize a Screen.")
2018-08-27 19:53:10 +00:00
os.Exit(1)
}
} else {
fmt.Println(err)
2018-08-27 21:55:28 +00:00
fmt.Println("Fatal: Micro could not initialize a Screen.")
2018-08-27 19:53:10 +00:00
os.Exit(1)
}
}
if err = Screen.Init(); err != nil {
fmt.Println(err)
os.Exit(1)
}
// Now we can put the TERM back to what it was before
if truecolor {
os.Setenv("TERM", oldTerm)
}
if config.GetGlobalOption("mouse").(bool) {
Screen.EnableMouse()
}
os.Setenv("TCELLDB", tcelldb)
}