--1-- SPDX-License-Identifier: BSD-2-Clause2--3-- Copyright (c) 2015 Pedro Souza <[email protected]>4-- All rights reserved.5--6-- Redistribution and use in source and binary forms, with or without7-- modification, are permitted provided that the following conditions8-- are met:9-- 1. Redistributions of source code must retain the above copyright10-- notice, this list of conditions and the following disclaimer.11-- 2. Redistributions in binary form must reproduce the above copyright12-- notice, this list of conditions and the following disclaimer in the13-- documentation and/or other materials provided with the distribution.14--15-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25-- SUCH DAMAGE.26--2728local core = require("core")29local hook = require("hook")3031local color = {}3233local function recalcDisabled()34color.disabled = not color.isEnabled()35end3637-- Module exports38color.BLACK = 039color.RED = 140color.GREEN = 241color.YELLOW = 342color.BLUE = 443color.MAGENTA = 544color.CYAN = 645color.WHITE = 74647color.DEFAULT = 948color.BRIGHT = 149color.DIM = 25051function color.isEnabled()52local c = loader.getenv("loader_color")53if c ~= nil then54return c:lower() ~= "no" and c ~= "0"55end56return true57end5859function color.escapefg(color_value)60if color.disabled then61return ''62end63return core.KEYSTR_CSI .. "3" .. color_value .. "m"64end6566function color.resetfg()67if color.disabled then68return ''69end70return color.escapefg(color.DEFAULT)71end7273function color.escapebg(color_value)74if color.disabled then75return ''76end77return core.KEYSTR_CSI .. "4" .. color_value .. "m"78end7980function color.resetbg()81if color.disabled then82return ''83end84return color.escapebg(color.DEFAULT)85end8687function color.escape(fg_color, bg_color, attribute)88if color.disabled then89return ""90end91if attribute == nil then92attribute = ""93else94attribute = attribute .. ";"95end96return core.KEYSTR_CSI .. attribute ..97"3" .. fg_color .. ";4" .. bg_color .. "m"98end99100function color.default()101if color.disabled then102return ""103end104return color.escape(color.DEFAULT, color.DEFAULT)105end106107function color.highlight(str)108if color.disabled then109return str110end111-- We need to reset attributes as well as color scheme here, just in112-- case the terminal defaults don't match what we're expecting.113return core.KEYSTR_CSI .. "1m" .. str .. core.KEYSTR_CSI .. "22m"114end115116recalcDisabled()117hook.register("config.loaded", recalcDisabled)118119return color120121122