1088b4f5fSWarner Losh-- 2*4d846d26SWarner Losh-- SPDX-License-Identifier: BSD-2-Clause 372e39d71SKyle Evans-- 4088b4f5fSWarner Losh-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org> 5088b4f5fSWarner Losh-- All rights reserved. 6088b4f5fSWarner Losh-- 7088b4f5fSWarner Losh-- Redistribution and use in source and binary forms, with or without 8088b4f5fSWarner Losh-- modification, are permitted provided that the following conditions 9088b4f5fSWarner Losh-- are met: 10088b4f5fSWarner Losh-- 1. Redistributions of source code must retain the above copyright 11088b4f5fSWarner Losh-- notice, this list of conditions and the following disclaimer. 12088b4f5fSWarner Losh-- 2. Redistributions in binary form must reproduce the above copyright 13088b4f5fSWarner Losh-- notice, this list of conditions and the following disclaimer in the 14088b4f5fSWarner Losh-- documentation and/or other materials provided with the distribution. 15088b4f5fSWarner Losh-- 16088b4f5fSWarner Losh-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17088b4f5fSWarner Losh-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18088b4f5fSWarner Losh-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19088b4f5fSWarner Losh-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20088b4f5fSWarner Losh-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21088b4f5fSWarner Losh-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22088b4f5fSWarner Losh-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23088b4f5fSWarner Losh-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24088b4f5fSWarner Losh-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25088b4f5fSWarner Losh-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26088b4f5fSWarner Losh-- SUCH DAMAGE. 27088b4f5fSWarner Losh-- 28088b4f5fSWarner Losh 29aedd6be5SKyle Evanslocal core = require("core") 301ce57df2SKyle Evanslocal hook = require("hook") 31088b4f5fSWarner Losh 32aedd6be5SKyle Evanslocal color = {} 331f5696c7SKyle Evans 341ce57df2SKyle Evanslocal function recalcDisabled() 351ce57df2SKyle Evans color.disabled = not color.isEnabled() 361ce57df2SKyle Evansend 371ce57df2SKyle Evans 38b5746545SKyle Evans-- Module exports 39aedd6be5SKyle Evanscolor.BLACK = 0 40aedd6be5SKyle Evanscolor.RED = 1 41aedd6be5SKyle Evanscolor.GREEN = 2 42aedd6be5SKyle Evanscolor.YELLOW = 3 43aedd6be5SKyle Evanscolor.BLUE = 4 44aedd6be5SKyle Evanscolor.MAGENTA = 5 45aedd6be5SKyle Evanscolor.CYAN = 6 46aedd6be5SKyle Evanscolor.WHITE = 7 47088b4f5fSWarner Losh 486dd078dfSToomas Soomecolor.DEFAULT = 9 49aedd6be5SKyle Evanscolor.BRIGHT = 1 50aedd6be5SKyle Evanscolor.DIM = 2 51088b4f5fSWarner Losh 52088b4f5fSWarner Loshfunction color.isEnabled() 53aedd6be5SKyle Evans local c = loader.getenv("loader_color") 549f71d421SKyle Evans if c ~= nil then 55bb7b569dSKyle Evans return c:lower() ~= "no" and c ~= "0" 56088b4f5fSWarner Losh end 573630506bSToomas Soome return true 58088b4f5fSWarner Loshend 59088b4f5fSWarner Losh 608ce1744fSKyle Evansfunction color.escapefg(color_value) 619f71d421SKyle Evans if color.disabled then 62a6cf4be0SKyle Evans return '' 63088b4f5fSWarner Losh end 642bb86aefSKyle Evans return core.KEYSTR_CSI .. "3" .. color_value .. "m" 65088b4f5fSWarner Loshend 66088b4f5fSWarner Losh 678ce1744fSKyle Evansfunction color.resetfg() 683224bb3fSKyle Evans if color.disabled then 693224bb3fSKyle Evans return '' 703224bb3fSKyle Evans end 716dd078dfSToomas Soome return color.escapefg(color.DEFAULT) 723224bb3fSKyle Evansend 733224bb3fSKyle Evans 748ce1744fSKyle Evansfunction color.escapebg(color_value) 759f71d421SKyle Evans if color.disabled then 76a6cf4be0SKyle Evans return '' 77088b4f5fSWarner Losh end 782bb86aefSKyle Evans return core.KEYSTR_CSI .. "4" .. color_value .. "m" 79088b4f5fSWarner Loshend 80088b4f5fSWarner Losh 818ce1744fSKyle Evansfunction color.resetbg() 823224bb3fSKyle Evans if color.disabled then 833224bb3fSKyle Evans return '' 843224bb3fSKyle Evans end 856dd078dfSToomas Soome return color.escapebg(color.DEFAULT) 863224bb3fSKyle Evansend 873224bb3fSKyle Evans 8804af4229SKyle Evansfunction color.escape(fg_color, bg_color, attribute) 899f71d421SKyle Evans if color.disabled then 90aedd6be5SKyle Evans return "" 91088b4f5fSWarner Losh end 9204af4229SKyle Evans if attribute == nil then 9304af4229SKyle Evans attribute = "" 94088b4f5fSWarner Losh else 9504af4229SKyle Evans attribute = attribute .. ";" 96088b4f5fSWarner Losh end 972bb86aefSKyle Evans return core.KEYSTR_CSI .. attribute .. 9804af4229SKyle Evans "3" .. fg_color .. ";4" .. bg_color .. "m" 99088b4f5fSWarner Loshend 100088b4f5fSWarner Losh 101088b4f5fSWarner Loshfunction color.default() 1029f71d421SKyle Evans if color.disabled then 103aedd6be5SKyle Evans return "" 104088b4f5fSWarner Losh end 1056dd078dfSToomas Soome return color.escape(color.DEFAULT, color.DEFAULT) 106088b4f5fSWarner Loshend 107088b4f5fSWarner Losh 108088b4f5fSWarner Loshfunction color.highlight(str) 1099f71d421SKyle Evans if color.disabled then 110aedd6be5SKyle Evans return str 111088b4f5fSWarner Losh end 1124a034badSKyle Evans -- We need to reset attributes as well as color scheme here, just in 1134a034badSKyle Evans -- case the terminal defaults don't match what we're expecting. 1143224bb3fSKyle Evans return core.KEYSTR_CSI .. "1m" .. str .. core.KEYSTR_CSI .. "22m" 115088b4f5fSWarner Loshend 116088b4f5fSWarner Losh 1171ce57df2SKyle EvansrecalcDisabled() 1181ce57df2SKyle Evanshook.register("config.loaded", recalcDisabled) 1191ce57df2SKyle Evans 120aedd6be5SKyle Evansreturn color 121