scoop:scoop.terminal:0.1.0
src/style/color_policy.scoop
Selected production source · src/style/color_policy.scoop
1
package scoop.terminal.style3
import scoop.platform.env.environmentVariable4
import scoop.text.contains6
private val colorOverride: AtomicInt = AtomicInt(-1)8
/**9
* Sets a process-wide explicit color-output decision, overriding the environment-derived policy.10
*/11
public fun forceColorOutput(enabled: Boolean) { colorOverride.store(if (enabled) 1 else 0) }12
/**13
* Clears the explicit color decision so environment policy applies again.14
*/15
public fun resetColorOutput() { colorOverride.store(-1) }17
/**18
* Returns the explicit override when set; otherwise a nonempty NO_COLOR disables color output.19
*/20
public fun isColorOutputEnabled(): Boolean {21
val decision = colorOverride.load()22
if (decision >= 0) return decision != 023
return when (environmentVariable("NO_COLOR")) {24
case Some(val value) -> value.toByteArray().size == 0L25
case None -> true26
}27
}29
/**30
* Estimates color capability from COLORTERM or TERM: 65535 for true-color markers, 256 for a31
* 256-color marker, otherwise 8. This does not probe the terminal.32
*/33
public 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")) 65535L43
else if (text.contains("256")) 256L else 8L44
}