Repository: zyedidia/micro
File: internal/screen/screen.go (Go)

Code before:
	screenWasNil := Screen == nil

	if !screenWasNil {
		Screen.Fini()
		Lock()
		Screen = nil
	}
	return screenWasNil
}

// TempStart restarts the screen after it was temporarily disabled
func TempStart(screenWasNil bool) {
	if !screenWasNil {
		Init()
		Unlock()
	}
}

// Init creates and initializes the tcell screen
func Init() error {
	drawChan = make(chan bool, 8)

	// Is true color support available?
	truecolorAvailable := (os.Getenv("COLORTERM") == "truecolor") ||
		(os.Getenv("COLORTERM") == "24bit") ||
		(os.Getenv("COLORTERM") == "24-bit")

	// Should we enable true color?
	truecolor := (truecolorAvailable &&
		(config.GetGlobalOption("truecolor").(string) == "auto")) ||
		(config.GetGlobalOption("truecolor").(string) == "on") ||
		(os.Getenv("MICRO_TRUECOLOR") == "1")

	if !truecolor {
		os.Setenv("TCELL_TRUECOLOR", "disable")
	}

	var oldTerm string
	modifiedTerm := false
	setXterm := func() {
		oldTerm = os.Getenv("TERM")
		os.Setenv("TERM", "xterm-256color")
		modifiedTerm = true
	}

	if config.GetGlobalOption("xterm").(bool) {
		setXterm()
	}

	// Initilize tcell
	var err error

Review comment (bug):
I've found that this detection is actually already done by tcell: https://github.com/gdamore/tcell/blob/08c7757cd1c98810e46975da457c033dc8657ad8/terminfo/terminfo.go#L688
See also its readme: https://github.com/gdamore/tcell/blob/08c7757cd1c98810e46975da457c033dc8657ad8/README.md#24-bit-color

So no need to do it again in micro itself. All micro needs to do (and what it already does, a few lines below) is to set or not to set `TCELL_TRUECOLOR=disable`.

Also it means that in your implementation there is no difference between `auto` and `on`, since `on` does not really force truecolor, it only enables tcell's truecolor detection by not setting `TCELL_TRUECOLOR=disable`.

IOW, it turns out that your PR does not provide new functionality. The automation is already there, only that it is disabled by default (but your PR does not enable it by default either). The user can enable truecolor autodetection by setting `MICRO_TRUECOLOR=1`, and if tcell fails to detect truecolor, the user can force it by setting `COLORTERM=truecolor`.

Although your PR adds possibility to use an option instead of env vars for that, but I'm not sure if that brings a real improvement in user experience, e.g. considering that it still takes no effect until the next start of micro.

Code after:
	Screen.SetContent(x, y, mainc, combc, style)
	if UseFake() && lastCursor.x == x && lastCursor.y == y {
		lastCursor.r = mainc
		lastCursor.style = style
		lastCursor.combc = combc
	}
}

// RegisterRawSeq registers a raw escape sequence that should be parsed by tcell
func RegisterRawSeq(r string) {
	for _, seq := range rawSeq {
		if seq == r {
			return
		}
	}
	rawSeq = append(rawSeq, r)

	if Screen != nil {
		Screen.RegisterRawSeq(r)
	}
}

// UnregisterRawSeq unregisters a raw escape sequence that should be parsed by tcell
func UnregisterRawSeq(r string) {
	for i, seq := range rawSeq {
		if seq == r {
			rawSeq[i] = rawSeq[len(rawSeq)-1]
			rawSeq = rawSeq[:len(rawSeq)-1]
		}
	}

	if Screen != nil {
		Screen.UnregisterRawSeq(r)
	}
}

// TempFini shuts the screen down temporarily
func TempFini() bool {
	screenWasNil := Screen == nil

	if !screenWasNil {
		Screen.Fini()
		Lock()
		Screen = nil
	}
	return screenWasNil
}

// TempStart restarts the screen after it was temporarily disabled
func TempStart(screenWasNil bool) {
	if !screenWasNil {