From 3e0830c0821268556bfd8def9da77404a412eefb Mon Sep 17 00:00:00 2001 From: rhpidfyre Date: Mon, 30 Dec 2024 14:27:04 -0500 Subject: [PATCH] type checking --- luau/examples/rainbowify.luau | 30 ++++++++-------------- luau/types.luau | 47 +++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 41 deletions(-) diff --git a/luau/examples/rainbowify.luau b/luau/examples/rainbowify.luau index 3303178..eeb809c 100644 --- a/luau/examples/rainbowify.luau +++ b/luau/examples/rainbowify.luau @@ -1,26 +1,18 @@ --!strict -local color_map = { - [1] = TERMINAL.OUT.STYLE.RED, - [2] = TERMINAL.OUT.STYLE.YELLOW, - [3] = TERMINAL.OUT.STYLE.GREEN, - [4] = TERMINAL.OUT.STYLE.BLUE, - [5] = TERMINAL.OUT.STYLE.MAGENTA, -} +local function rainbowify(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, + } -local function rainbowify(message) - local iter = 1 - local parsed_terminal_message = {} - - for _, char in message:split('') do - local styled = char - if char ~= ' ' then - styled = color_map[iter%5+1](char) - iter += 1 - end - table.insert(parsed_terminal_message, styled) + 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 diff --git a/luau/types.luau b/luau/types.luau index 5753797..239e472 100644 --- a/luau/types.luau +++ b/luau/types.luau @@ -5,18 +5,16 @@ type class_constructor = { __index: CON, new: F, } & EXT -type class_method = (self: Self, T...) -> R - -type map = { - [K]: V -} -type lambda = (f: (T...) -> ()) -> () type map_functions = { len: () -> number, len_keys: () -> number, len_values: () -> number, for_each: lambda } +type func_map = (A...) -> T +type class_method = (self: Self, T...) -> R +type map = {[K]: V} +type lambda = (f: (T...) -> ()) -> () -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- type SHELL = { @@ -51,23 +49,26 @@ type SHELL = { } -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- -type color = (string) -> T -type colors = { - RED: color, - ORANGE: color, - YELLOW: color, - GREEN: color, - BLUE: color, - PURPLE: color -} -type text_style = { - STYLE: { - BACKGROUND: colors, - FOREGROUND: colors - } +-- type text_on_foreground = { +-- ON_RED: func_map, +-- ON_ORANGE: func_map, +-- ON_YELLOW: func_map, +-- ON_GREEN: func_map, +-- ON_BLUE: func_map, +-- ON_PURPLE: func_map, +-- } +type text_colors = { + RED: func_map, + ORANGE: func_map, + YELLOW: func_map, + GREEN: func_map, + BLUE: func_map, + PURPLE: func_map, } type TERMINAL = { - TEXT: text_style + OUT: { + STYLE: text_colors, + } } -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- @@ -104,7 +105,9 @@ local SHELL = {} :: SHELL local TERMINAL = {} :: TERMINAL local Command = {} :: command_builder -local username = TERMINAL.TEXT.GREEN(SHELL.SYSTEM.USERNAME) +local OUT_COLOR = TERMINAL.OUT.COLOR.BLUE("hi") + +local username = SHELL.SYSTEM.USERNAME local hostname = SHELL.SYSTEM.HOSTNAME SHELL.PROMPT = `{username}@{hostname} λ `