diff --git a/luau/examples/rainbowify.luau b/luau/examples/rainbowify.luau index 8c12603..e4fea66 100644 --- a/luau/examples/rainbowify.luau +++ b/luau/examples/rainbowify.luau @@ -1,39 +1,39 @@ --!strict +type term_color_func = (string) -> string + +local function rainbowify(str: string, color_map: {term_color_func}): string + local chars = str:split('') + for i, char in chars do + chars[i] = char ~= ' ' and color_map[i%5+1](char) or char + end + return table.concat(chars) +end + local function rainbowify_foreground(message: string): string - local parsed_terminal_message: {string} = {} - local color_map = { + return rainbowify(message, { TERMINAL.OUT.FOREGROUND.RED, TERMINAL.OUT.FOREGROUND.YELLOW, TERMINAL.OUT.FOREGROUND.GREEN, TERMINAL.OUT.FOREGROUND.BLUE, TERMINAL.OUT.FOREGROUND.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 local function rainbowify_background(message: string): string - local parsed_terminal_message: {string} = {} - local color_map = { + return rainbowify(message, { TERMINAL.OUT.BACKGROUND.RED, TERMINAL.OUT.BACKGROUND.YELLOW, TERMINAL.OUT.BACKGROUND.GREEN, TERMINAL.OUT.BACKGROUND.BLUE, TERMINAL.OUT.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_foreground("Rainbow foreground")) -print(rainbowify_background("Rainbow background")) +print( + TERMINAL.OUT.FOREGROUND.BLACK(rainbowify_background("Rainbow background with black text")) +) print( TERMINAL.OUT.BACKGROUND.DARK_GREY(rainbowify_foreground("Rainbow foreground on a dark grey background")) ) \ No newline at end of file