From c0c0519f1a4d9b5db6a342edffd202c7deeed7fb Mon Sep 17 00:00:00 2001 From: Cam <21029087+cxmeel@users.noreply.github.com> Date: Thu, 14 Mar 2024 19:38:45 +0000 Subject: [PATCH] Add foreground color logic in Crayon module --- lune/lib/_index/crayon/init.luau | 149 +++++++++++++++++-------------- 1 file changed, 81 insertions(+), 68 deletions(-) diff --git a/lune/lib/_index/crayon/init.luau b/lune/lib/_index/crayon/init.luau index d2f4f6f..fe9ca3a 100644 --- a/lune/lib/_index/crayon/init.luau +++ b/lune/lib/_index/crayon/init.luau @@ -4,91 +4,104 @@ local process = require("@lune/process") local env, args = process.env, process.args local isColorSupported = (function() - local isNoColor = env.NO_COLOR - or table.find(args, "--no-color") - or table.find(args, "--color=false") - local isForcedColor = env.FORCE_COLOR - or table.find(args, "--color") - or table.find(args, "--color=true") - local isWindows = process.os == "windows" - local isDumbTerminal = env.TERM == "dumb" + local isNoColor = env.NO_COLOR + or table.find(args, "--no-color") + or table.find(args, "--color=false") + local isForcedColor = env.FORCE_COLOR + or table.find(args, "--color") + or table.find(args, "--color=true") + local isWindows = process.os == "windows" + local isDumbTerminal = env.TERM == "dumb" - local isCompatible = env.TERM and env.TERM ~= "dumb" - local isCI = env.CI and (env.GITHUB_ACTIONS or env.GITLAB_CI or env.CIRCLECI) + local isCompatible = env.TERM and env.TERM ~= "dumb" + local isCI = env.CI and (env.GITHUB_ACTIONS or env.GITLAB_CI or env.CIRCLECI) - -- selene: allow(shadowing) - local isColorSupported = not isNoColor - and (isForcedColor or (isWindows and not isDumbTerminal) or isCompatible or isCI) + -- selene: allow(shadowing) + local isColorSupported = not isNoColor + and (isForcedColor or (isWindows and not isDumbTerminal) or isCompatible or isCI) - return isColorSupported + return isColorSupported end)() local function styler(codepoint: { number }) - local open, close = codepoint[1], codepoint[2] + local open, close = codepoint[1], codepoint[2] - return function(text: string) - if not isColorSupported then - return text - end + return function(text: string) + if not isColorSupported then + return text + end - return `\27[{open}m{text}\27[{close}m` - end + return `\27[{open}m{text}\27[{close}m` + end end local Crayon = { - -- Modifiers - reset = styler(ANSI_STYLES.MODIFIERS.RESET), - bold = styler(ANSI_STYLES.MODIFIERS.BOLD), - dim = styler(ANSI_STYLES.MODIFIERS.DIM), - italic = styler(ANSI_STYLES.MODIFIERS.ITALIC), - underline = styler(ANSI_STYLES.MODIFIERS.UNDERLINE), - overline = styler(ANSI_STYLES.MODIFIERS.OVERLINE), - inverse = styler(ANSI_STYLES.MODIFIERS.INVERSE), - hidden = styler(ANSI_STYLES.MODIFIERS.HIDDEN), - strikethrough = styler(ANSI_STYLES.MODIFIERS.STRIKETHROUGH), + -- Modifiers + reset = styler(ANSI_STYLES.MODIFIERS.RESET), + bold = styler(ANSI_STYLES.MODIFIERS.BOLD), + dim = styler(ANSI_STYLES.MODIFIERS.DIM), + italic = styler(ANSI_STYLES.MODIFIERS.ITALIC), + underline = styler(ANSI_STYLES.MODIFIERS.UNDERLINE), + overline = styler(ANSI_STYLES.MODIFIERS.OVERLINE), + inverse = styler(ANSI_STYLES.MODIFIERS.INVERSE), + hidden = styler(ANSI_STYLES.MODIFIERS.HIDDEN), + strikethrough = styler(ANSI_STYLES.MODIFIERS.STRIKETHROUGH), - -- Foreground colors - black = styler(ANSI_STYLES.FOREGROUND_COLORS.BLACK), - red = styler(ANSI_STYLES.FOREGROUND_COLORS.RED), - green = styler(ANSI_STYLES.FOREGROUND_COLORS.GREEN), - yellow = styler(ANSI_STYLES.FOREGROUND_COLORS.YELLOW), - blue = styler(ANSI_STYLES.FOREGROUND_COLORS.BLUE), - magenta = styler(ANSI_STYLES.FOREGROUND_COLORS.MAGENTA), - cyan = styler(ANSI_STYLES.FOREGROUND_COLORS.CYAN), - white = styler(ANSI_STYLES.FOREGROUND_COLORS.WHITE), - grey = styler(ANSI_STYLES.FOREGROUND_COLORS.GREY), - blackBright = styler(ANSI_STYLES.FOREGROUND_COLORS.BLACK_BRIGHT), - redBright = styler(ANSI_STYLES.FOREGROUND_COLORS.RED_BRIGHT), - greenBright = styler(ANSI_STYLES.FOREGROUND_COLORS.GREEN_BRIGHT), - yellowBright = styler(ANSI_STYLES.FOREGROUND_COLORS.YELLOW_BRIGHT), - blueBright = styler(ANSI_STYLES.FOREGROUND_COLORS.BLUE_BRIGHT), - magentaBright = styler(ANSI_STYLES.FOREGROUND_COLORS.MAGENTA_BRIGHT), - cyanBright = styler(ANSI_STYLES.FOREGROUND_COLORS.CYAN_BRIGHT), - whiteBright = styler(ANSI_STYLES.FOREGROUND_COLORS.WHITE_BRIGHT), + -- Foreground colors + black = styler(ANSI_STYLES.FOREGROUND_COLORS.BLACK), + red = styler(ANSI_STYLES.FOREGROUND_COLORS.RED), + green = styler(ANSI_STYLES.FOREGROUND_COLORS.GREEN), + yellow = styler(ANSI_STYLES.FOREGROUND_COLORS.YELLOW), + blue = styler(ANSI_STYLES.FOREGROUND_COLORS.BLUE), + magenta = styler(ANSI_STYLES.FOREGROUND_COLORS.MAGENTA), + cyan = styler(ANSI_STYLES.FOREGROUND_COLORS.CYAN), + white = styler(ANSI_STYLES.FOREGROUND_COLORS.WHITE), + grey = styler(ANSI_STYLES.FOREGROUND_COLORS.GREY), + blackBright = styler(ANSI_STYLES.FOREGROUND_COLORS.BLACK_BRIGHT), + redBright = styler(ANSI_STYLES.FOREGROUND_COLORS.RED_BRIGHT), + greenBright = styler(ANSI_STYLES.FOREGROUND_COLORS.GREEN_BRIGHT), + yellowBright = styler(ANSI_STYLES.FOREGROUND_COLORS.YELLOW_BRIGHT), + blueBright = styler(ANSI_STYLES.FOREGROUND_COLORS.BLUE_BRIGHT), + magentaBright = styler(ANSI_STYLES.FOREGROUND_COLORS.MAGENTA_BRIGHT), + cyanBright = styler(ANSI_STYLES.FOREGROUND_COLORS.CYAN_BRIGHT), + whiteBright = styler(ANSI_STYLES.FOREGROUND_COLORS.WHITE_BRIGHT), - -- Background colors - bgBlack = styler(ANSI_STYLES.BACKGROUND_COLORS.BLACK), - bgRed = styler(ANSI_STYLES.BACKGROUND_COLORS.RED), - bgGreen = styler(ANSI_STYLES.BACKGROUND_COLORS.GREEN), - bgYellow = styler(ANSI_STYLES.BACKGROUND_COLORS.YELLOW), - bgBlue = styler(ANSI_STYLES.BACKGROUND_COLORS.BLUE), - bgMagenta = styler(ANSI_STYLES.BACKGROUND_COLORS.MAGENTA), - bgCyan = styler(ANSI_STYLES.BACKGROUND_COLORS.CYAN), - bgWhite = styler(ANSI_STYLES.BACKGROUND_COLORS.WHITE), - bgGrey = styler(ANSI_STYLES.BACKGROUND_COLORS.GREY), - bgBlackBright = styler(ANSI_STYLES.BACKGROUND_COLORS.BLACK_BRIGHT), - bgRedBright = styler(ANSI_STYLES.BACKGROUND_COLORS.RED_BRIGHT), - bgGreenBright = styler(ANSI_STYLES.BACKGROUND_COLORS.GREEN_BRIGHT), - bgYellowBright = styler(ANSI_STYLES.BACKGROUND_COLORS.YELLOW_BRIGHT), - bgBlueBright = styler(ANSI_STYLES.BACKGROUND_COLORS.BLUE_BRIGHT), - bgMagentaBright = styler(ANSI_STYLES.BACKGROUND_COLORS.MAGENTA_BRIGHT), - bgCyanBright = styler(ANSI_STYLES.BACKGROUND_COLORS.CYAN_BRIGHT), - bgWhiteBright = styler(ANSI_STYLES.BACKGROUND_COLORS.WHITE_BRIGHT), + -- Background colors + bgBlack = styler(ANSI_STYLES.BACKGROUND_COLORS.BLACK), + bgRed = styler(ANSI_STYLES.BACKGROUND_COLORS.RED), + bgGreen = styler(ANSI_STYLES.BACKGROUND_COLORS.GREEN), + bgYellow = styler(ANSI_STYLES.BACKGROUND_COLORS.YELLOW), + bgBlue = styler(ANSI_STYLES.BACKGROUND_COLORS.BLUE), + bgMagenta = styler(ANSI_STYLES.BACKGROUND_COLORS.MAGENTA), + bgCyan = styler(ANSI_STYLES.BACKGROUND_COLORS.CYAN), + bgWhite = styler(ANSI_STYLES.BACKGROUND_COLORS.WHITE), + bgGrey = styler(ANSI_STYLES.BACKGROUND_COLORS.GREY), + bgBlackBright = styler(ANSI_STYLES.BACKGROUND_COLORS.BLACK_BRIGHT), + bgRedBright = styler(ANSI_STYLES.BACKGROUND_COLORS.RED_BRIGHT), + bgGreenBright = styler(ANSI_STYLES.BACKGROUND_COLORS.GREEN_BRIGHT), + bgYellowBright = styler(ANSI_STYLES.BACKGROUND_COLORS.YELLOW_BRIGHT), + bgBlueBright = styler(ANSI_STYLES.BACKGROUND_COLORS.BLUE_BRIGHT), + bgMagentaBright = styler(ANSI_STYLES.BACKGROUND_COLORS.MAGENTA_BRIGHT), + bgCyanBright = styler(ANSI_STYLES.BACKGROUND_COLORS.CYAN_BRIGHT), + bgWhiteBright = styler(ANSI_STYLES.BACKGROUND_COLORS.WHITE_BRIGHT), +} + +local LIGHT_BACKGROUNDS = { + Crayon.bgGrey, + Crayon.bgBlackBright, + Crayon.bgRedBright, + Crayon.bgGreenBright, + Crayon.bgYellowBright, + Crayon.bgBlueBright, + Crayon.bgMagentaBright, + Crayon.bgCyanBright, + Crayon.bgWhiteBright, } -- selene: allow(shadowing) function Crayon.label(text: string, styler: (string) -> string) - return styler(Crayon.bold(` {text:upper()} `)) + local foreground = table.find(LIGHT_BACKGROUNDS, styler) and Crayon.black or Crayon.white + return styler(Crayon.bold(foreground(` {text:upper()} `))) end return Crayon