1329167Simp-- 2344220Skevans-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3344220Skevans-- 4329167Simp-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org> 5329167Simp-- All rights reserved. 6329167Simp-- 7329167Simp-- Redistribution and use in source and binary forms, with or without 8329167Simp-- modification, are permitted provided that the following conditions 9329167Simp-- are met: 10329167Simp-- 1. Redistributions of source code must retain the above copyright 11329167Simp-- notice, this list of conditions and the following disclaimer. 12329167Simp-- 2. Redistributions in binary form must reproduce the above copyright 13329167Simp-- notice, this list of conditions and the following disclaimer in the 14329167Simp-- documentation and/or other materials provided with the distribution. 15329167Simp-- 16329167Simp-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17329167Simp-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18329167Simp-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19329167Simp-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20329167Simp-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21329167Simp-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22329167Simp-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23329167Simp-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24329167Simp-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25329167Simp-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26329167Simp-- SUCH DAMAGE. 27329167Simp-- 28329167Simp-- $FreeBSD: stable/11/stand/lua/color.lua 360597 2020-05-03 03:54:49Z kevans $ 29329167Simp-- 30329167Simp 31344220Skevanslocal core = require("core") 32354059Skevanslocal hook = require("hook") 33329167Simp 34344220Skevanslocal color = {} 35329167Simp 36354059Skevanslocal function recalcDisabled() 37354059Skevans color.disabled = not color.isEnabled() 38354059Skevansend 39354059Skevans 40344220Skevans-- Module exports 41344220Skevanscolor.BLACK = 0 42344220Skevanscolor.RED = 1 43344220Skevanscolor.GREEN = 2 44344220Skevanscolor.YELLOW = 3 45344220Skevanscolor.BLUE = 4 46344220Skevanscolor.MAGENTA = 5 47344220Skevanscolor.CYAN = 6 48344220Skevanscolor.WHITE = 7 49329167Simp 50360597Skevanscolor.DEFAULT = 9 51344220Skevanscolor.BRIGHT = 1 52344220Skevanscolor.DIM = 2 53329167Simp 54329167Simpfunction color.isEnabled() 55344220Skevans local c = loader.getenv("loader_color") 56329167Simp if c ~= nil then 57353137Skevans return c:lower() ~= "no" and c ~= "0" 58329167Simp end 59344220Skevans return not core.isSerialBoot() 60329167Simpend 61329167Simp 62344220Skevansfunction color.escapefg(color_value) 63344220Skevans if color.disabled then 64354059Skevans return '' 65344220Skevans end 66344220Skevans return core.KEYSTR_CSI .. "3" .. color_value .. "m" 67344220Skevansend 68329167Simp 69344220Skevansfunction color.resetfg() 70329167Simp if color.disabled then 71344220Skevans return '' 72329167Simp end 73360597Skevans return color.escapefg(color.DEFAULT) 74329167Simpend 75329167Simp 76344220Skevansfunction color.escapebg(color_value) 77329167Simp if color.disabled then 78354059Skevans return '' 79329167Simp end 80344220Skevans return core.KEYSTR_CSI .. "4" .. color_value .. "m" 81329167Simpend 82329167Simp 83344220Skevansfunction color.resetbg() 84329167Simp if color.disabled then 85344220Skevans return '' 86329167Simp end 87360597Skevans return color.escapebg(color.DEFAULT) 88344220Skevansend 89344220Skevans 90344220Skevansfunction color.escape(fg_color, bg_color, attribute) 91344220Skevans if color.disabled then 92344220Skevans return "" 93344220Skevans end 94344220Skevans if attribute == nil then 95344220Skevans attribute = "" 96329167Simp else 97344220Skevans attribute = attribute .. ";" 98329167Simp end 99344220Skevans return core.KEYSTR_CSI .. attribute .. 100344220Skevans "3" .. fg_color .. ";4" .. bg_color .. "m" 101329167Simpend 102329167Simp 103329167Simpfunction color.default() 104329167Simp if color.disabled then 105344220Skevans return "" 106329167Simp end 107360597Skevans return color.escape(color.DEFAULT, color.DEFAULT) 108329167Simpend 109329167Simp 110329167Simpfunction color.highlight(str) 111329167Simp if color.disabled then 112344220Skevans return str 113329167Simp end 114344220Skevans -- We need to reset attributes as well as color scheme here, just in 115344220Skevans -- case the terminal defaults don't match what we're expecting. 116344220Skevans return core.KEYSTR_CSI .. "1m" .. str .. core.KEYSTR_CSI .. "22m" 117329167Simpend 118329167Simp 119354059SkevansrecalcDisabled() 120354059Skevanshook.register("config.loaded", recalcDisabled) 121354059Skevans 122329167Simpreturn color 123