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

Code before:
func TempStart(screenWasNil bool) {
	if !screenWasNil {
		Init()
		Unlock()

		if RestartCallback != nil {
			RestartCallback()
		}
	}
}

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

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

	if truecolor {
		if (config.GetGlobalOption("truecolor").(string) == "on") ||
			(os.Getenv("MICRO_TRUECOLOR") == "1") {
			os.Setenv("COLORTERM", "truecolor")
		}
	} else {
		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
	Screen, err = tcell.NewScreen()
	if err != nil {
		log.Println("Warning: during screen initialization:", err)
		log.Println("Falling back to TERM=xterm-256color")
		setXterm()
		Screen, err = tcell.NewScreen()
		if err != nil {
			return err

Review comment (suggestion):
Trying to revive this PR... So, why not implement a simple intuitive desirable yet backward-compatible behavior: set `truecolor` to `auto` by default, but let MICRO_TRUECOLOR=1 make truecolor=auto behave like truecolor=on (and ignore MICRO_TRUECOLOR in any other cases, treating is as legacy)? So that the existing behavior of MICRO_TRUECOLOR=1 doesn't change as long as the user doesn't change the default `auto` to some other value.

Actually, am I correct that this code already implements exactly that? So it's only the documentation that describes something different, and in a quite messy way? (I've only briefly glanced over it, but I see also that it is, for example, talking about tcell and its implementation details as if it was interesting to the user, and so on.)

The code looks quite messy too, though. Why not make it straightforward and readable, e.g.:

```go
truecolor := config.GetGlobalOption("truecolor").(string)
if truecolor == "on" || (truecolor == "auto" && os.Getenv("MICRO_TRUECOLOR") == "1") {
	// Force tcell to enable truecolor
	os.Setenv("COLORTERM", "truecolor")
} else if truecolor == "off" {
	os.Setenv("TCELL_TRUECOLOR", "disable")
} else {
	// For "auto", tcell already autodetects truecolor by default
}
```

Code after:
	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 {
		Init()
		Unlock()

		if RestartCallback != nil {
			RestartCallback()
		}
	}
}

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

	// Should we enable true color?
	truecolor := config.GetGlobalOption("truecolor").(string)