Scoop API

scoop:scoop.terminal:0.1.0

src/style/color_policy.scoop

Selected production source · src/style/color_policy.scoop

1package scoop.terminal.style
3import scoop.platform.env.environmentVariable
4import scoop.text.contains
6private val colorOverride: AtomicInt = AtomicInt(-1)
8/**
9 * Sets a process-wide explicit color-output decision, overriding the environment-derived policy.
10 */
11public fun forceColorOutput(enabled: Boolean) { colorOverride.store(if (enabled) 1 else 0) }
12/**
13 * Clears the explicit color decision so environment policy applies again.
14 */
15public fun resetColorOutput() { colorOverride.store(-1) }
17/**
18 * Returns the explicit override when set; otherwise a nonempty NO_COLOR disables color output.
19 */
20public fun isColorOutputEnabled(): Boolean {
21 val decision = colorOverride.load()
22 if (decision >= 0) return decision != 0
23 return when (environmentVariable("NO_COLOR")) {
24 case Some(val value) -> value.toByteArray().size == 0L
25 case None -> true
26 }
29/**
30 * Estimates color capability from COLORTERM or TERM: 65535 for true-color markers, 256 for a
31 * 256-color marker, otherwise 8. This does not probe the terminal.
32 */
33public fun availableColorCount(): Long {
34 val value = when (environmentVariable("COLORTERM")) {
35 case Some(val text) -> Some(text)
36 case None -> environmentVariable("TERM")
37 }
38 val text = when (value) {
39 case Some(val text) -> text.decodeUtf8Lossy()
40 case None -> ""
41 }
42 return if (text.contains("24bit") || text.contains("truecolor")) 65535L
43 else if (text.contains("256")) 256L else 8L