micro/internal/screen/screen.go

96 lines
1.8 KiB
Go
Raw Normal View History

2018-08-27 22:53:10 +03:00
package screen
import (
"fmt"
"os"
2018-08-28 21:24:59 +03:00
"sync"
2018-08-27 22:53:10 +03:00
2019-02-04 07:17:24 +03:00
"github.com/zyedidia/micro/internal/config"
2020-01-02 05:29:18 +03:00
"github.com/zyedidia/micro/internal/util"
2020-01-01 04:15:45 +03:00
"github.com/zyedidia/tcell"
2018-08-27 22:53:10 +03:00
)
2018-12-31 22:46:04 +03: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 22:53:10 +03:00
var Screen tcell.Screen
2018-08-28 21:24:59 +03:00
var lock sync.Mutex
2019-01-11 00:37:05 +03:00
var DrawChan chan bool
2018-08-28 21:24:59 +03:00
func Lock() {
lock.Lock()
}
func Unlock() {
lock.Unlock()
}
2019-01-11 00:37:05 +03:00
func Redraw() {
DrawChan <- true
}
2018-08-28 21:24:59 +03:00
2020-01-02 05:29:18 +03:00
func ShowFakeCursor(x, y int) {
r, _, _, _ := Screen.GetContent(x, y)
Screen.SetContent(x, y, r, nil, config.DefStyle.Reverse(true))
}
func ShowCursor(x, y int) {
if util.FakeCursor {
ShowFakeCursor(x, y)
} else {
Screen.ShowCursor(x, y)
}
}
2018-12-31 22:46:04 +03:00
// TempFini shuts the screen down temporarily
2019-01-11 00:37:05 +03:00
func TempFini() bool {
screenWasNil := Screen == nil
2018-08-28 21:24:59 +03:00
if !screenWasNil {
Screen.Fini()
2019-01-11 00:37:05 +03:00
Lock()
2018-08-28 21:24:59 +03:00
Screen = nil
}
2019-01-11 00:37:05 +03:00
return screenWasNil
2018-08-28 21:24:59 +03:00
}
2018-12-31 22:46:04 +03:00
// TempStart restarts the screen after it was temporarily disabled
2019-01-11 00:37:05 +03:00
func TempStart(screenWasNil bool) {
2018-08-28 21:24:59 +03:00
if !screenWasNil {
Init()
Unlock()
}
}
2018-08-27 22:53:10 +03:00
// Init creates and initializes the tcell screen
func Init() {
2019-01-15 03:57:19 +03:00
DrawChan = make(chan bool, 8)
2019-01-11 00:37:05 +03:00
2018-08-27 22:53:10 +03:00
// Should we enable true color?
truecolor := os.Getenv("MICRO_TRUECOLOR") == "1"
2020-01-01 07:09:33 +03:00
if !truecolor {
os.Setenv("TCELL_TRUECOLOR", "disable")
2018-08-27 22:53:10 +03:00
}
// Initilize tcell
var err error
Screen, err = tcell.NewScreen()
if err != nil {
2020-01-01 07:09:33 +03:00
fmt.Println(err)
fmt.Println("Fatal: Micro could not initialize a Screen.")
os.Exit(1)
2018-08-27 22:53:10 +03:00
}
if err = Screen.Init(); err != nil {
fmt.Println(err)
os.Exit(1)
}
if config.GetGlobalOption("mouse").(bool) {
Screen.EnableMouse()
}
}