From 3c92a891909736604dc1b6c789d76f96d6047b20 Mon Sep 17 00:00:00 2001 From: rhpidfyre Date: Thu, 2 Jan 2025 01:54:29 -0500 Subject: [PATCH] rainbowify include background and foreground --- luau/examples/rainbowify.luau | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/luau/examples/rainbowify.luau b/luau/examples/rainbowify.luau index eeb809c..fffc2d1 100644 --- a/luau/examples/rainbowify.luau +++ b/luau/examples/rainbowify.luau @@ -1,13 +1,13 @@ --!strict -local function rainbowify(message: string): string +local function rainbowify_foreground(message: string): string local parsed_terminal_message: {string} = {} local color_map = { - TERMINAL.OUT.STYLE.RED, - TERMINAL.OUT.STYLE.YELLOW, - TERMINAL.OUT.STYLE.GREEN, - TERMINAL.OUT.STYLE.BLUE, - TERMINAL.OUT.STYLE.MAGENTA, + TERMINAL.OUT.STYLE.FOREGROUND.RED, + TERMINAL.OUT.STYLE.FOREGROUND.YELLOW, + TERMINAL.OUT.STYLE.FOREGROUND.GREEN, + TERMINAL.OUT.STYLE.FOREGROUND.BLUE, + TERMINAL.OUT.STYLE.FOREGROUND.MAGENTA, } for i, char in message:split('') do @@ -16,5 +16,22 @@ local function rainbowify(message: string): string return table.concat(parsed_terminal_message) end -print(rainbowify("rainbow text")) -print(rainbowify("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")) \ No newline at end of file +local function rainbowify_background(message: string): string + local parsed_terminal_message: {string} = {} + local color_map = { + TERMINAL.OUT.STYLE.BACKGROUND.RED, + TERMINAL.OUT.STYLE.BACKGROUND.YELLOW, + TERMINAL.OUT.STYLE.BACKGROUND.GREEN, + TERMINAL.OUT.STYLE.BACKGROUND.BLUE, + TERMINAL.OUT.STYLE.BACKGROUND.MAGENTA, + } + + for i, char in message:split('') do + table.insert(parsed_terminal_message, char ~= ' ' and color_map[i%5+1](char) or char) + end + return table.concat(parsed_terminal_message) +end + +print(rainbowify_background("Rainbow background")) +print(rainbowify_foreground("Rainbow foreground")) +print(rainbowify_background(rainbowify_foreground("Rainbow background and foreground"))) \ No newline at end of file