From 348f2b351a2e53040ae3687ccbfdb40a8298caa6 Mon Sep 17 00:00:00 2001 From: Mikhail Tavarez Date: Thu, 4 Jul 2024 17:29:03 -0500 Subject: [PATCH] import new mist (#16) --- benchmarks/basic_styling.mojo | 12 +- benchmarks/layout.mojo | 30 +- benchmarks/run.mojo | 2 +- examples/readme/basic.mojo | 4 +- examples/readme/layout.mojo | 30 +- examples/table/ansi.mojo | 2 +- examples/table/pokemon.mojo | 62 ++-- external/mist/ansi_colors.mojo | 517 ++++++++++++++++---------------- external/mist/color.mojo | 192 ++++++------ external/mist/profile.mojo | 33 +- external/mist/renderers.mojo | 22 +- external/mist/style.mojo | 111 ++----- mog/color.mojo | 42 +-- mog/style.mojo | 104 +++---- mog/whitespace.mojo | 8 +- tests/integration/test_mog.mojo | 10 +- 16 files changed, 547 insertions(+), 634 deletions(-) diff --git a/benchmarks/basic_styling.mojo b/benchmarks/basic_styling.mojo index 82b1b4f..12b2634 100644 --- a/benchmarks/basic_styling.mojo +++ b/benchmarks/basic_styling.mojo @@ -6,8 +6,8 @@ fn basic_styling(): var style = ( mog.new_style() .bold(True) - .foreground(mog.Color("#FAFAFA")) - .background(mog.Color("#7D56F4")) + .foreground(mog.Color(0xFAFAFA)) + .background(mog.Color(0x7D56F4)) .padding_top(2) .padding_left(4) .width(22) @@ -20,8 +20,8 @@ fn basic_styling(): var file_style = ( mog.new_style() .bold(True) - .foreground(mog.Color("#FAFAFA")) - .background(mog.Color("#7D56F4")) + .foreground(mog.Color(0xFAFAFA)) + .background(mog.Color(0x7D56F4)) .padding_top(2) .padding_left(4) .width(22) @@ -38,8 +38,8 @@ fn basic_styling_big_file(): try: with open("./benchmarks/data/big.txt", "r") as file: content = file.read() - var style = mog.new_style().bold(True).foreground(mog.Color("#FAFAFA")).background( - mog.Color("#7D56F4") + var style = mog.new_style().bold(True).foreground(mog.Color(0xFAFAFA)).background( + mog.Color(0x7D56F4) ).width(100) var output = style.render(content) keep(output) diff --git a/benchmarks/layout.mojo b/benchmarks/layout.mojo index 14c090f..4132a1f 100644 --- a/benchmarks/layout.mojo +++ b/benchmarks/layout.mojo @@ -17,9 +17,9 @@ import mog alias width = 96 alias column_width = 30 -alias subtle = mog.AdaptiveColor(light="#D9DCCF", dark="#383838") -alias highlight = mog.AdaptiveColor(light="#874BFD", dark="#7D56F4") -alias special = mog.AdaptiveColor(light="#43BF6D", dark="#73F59F") +alias subtle = mog.AdaptiveColor(light=0xD9DCCF, dark=0x383838) +alias highlight = mog.AdaptiveColor(light=0x874BFD, dark=0x7D56F4) +alias special = mog.AdaptiveColor(light=0x43BF6D, dark=0x73F59F) fn build_tabs() -> String: @@ -79,16 +79,16 @@ fn build_description() -> String: fn build_dialog_box() -> String: var dialog_box_style = mog.new_style().alignment(position.center).border(rounded_border()).border_foreground( - mog.Color("#874BFD") + mog.Color(0x874BFD) ).padding(1, 0) - var button_style = mog.new_style().foreground(mog.Color("#FFF7DB")).background(mog.Color("#888B7E")).padding( + var button_style = mog.new_style().foreground(mog.Color(0xFFF7DB)).background(mog.Color(0x888B7E)).padding( 0, 3 ).margin_top(1) - var active_button_style = button_style.foreground(mog.Color("#FFF7DB")).background( - mog.Color("#F25D94") - ).margin_right(2).underline() + var active_button_style = button_style.foreground(mog.Color(0xFFF7DB)).background(mog.Color(0xF25D94)).margin_right( + 2 + ).underline() var ok_button = active_button_style.render("Yes") var cancel_button = button_style.render("Maybe") @@ -125,7 +125,7 @@ fn build_lists() -> String: var check_mark = mog.new_style().foreground(special).padding_right(1).render("✔") - var list_done = mog.new_style().crossout().foreground(mog.AdaptiveColor(light="#969B86", dark="#696969")) + var list_done = mog.new_style().crossout().foreground(mog.AdaptiveColor(light=0x969B86, dark=0x696969)) var lists = join_horizontal( position.top, @@ -170,7 +170,7 @@ fn build_lists() -> String: fn build_history() -> String: var history_style = mog.new_style().height(20).width(column_width).padding(1, 2).margin(1, 3, 0, 0).alignment( position.left - ).foreground(mog.Color("#FFFDF5")).background(highlight) + ).foreground(mog.Color(0xFFFDF5)).background(highlight) alias history_a = "The Romans learned from the Greeks that quinces slowly cooked with honey would “set” when cool. The Apicius gives a recipe for preserving whole quinces, stems and leaves attached, in a bath of honey diluted with defrutum: Roman marmalade. Preserves of quince and lemon appear (along with rose, apple, plum and pear) in the Book of ceremonies of the Byzantine Emperor Constantine VII Porphyrogennetos." alias history_b = "Medieval quince preserves, which went by the French name cotignac, produced in a clear version and a fruit pulp version, began to lose their medieval seasoning of spices in the 16th century. In the 17th century, La Varenne provided recipes for both thick and clear cotignac." @@ -185,17 +185,17 @@ fn build_history() -> String: fn build_status_bar() -> String: - var status_nugget_style = mog.new_style().foreground(mog.Color("#FFFDF5")).padding(0, 1) + var status_nugget_style = mog.new_style().foreground(mog.Color(0xFFFDF5)).padding(0, 1) - var status_bar_style = mog.new_style().foreground(mog.Color("#C1C6B2")).background(mog.Color("#353533")) + var status_bar_style = mog.new_style().foreground(mog.Color(0xC1C6B2)).background(mog.Color(0x353533)) - var status_style = mog.new_style().foreground(mog.Color("#FFFDF5")).background(mog.Color("#FF5F87")).padding(0, 1) + var status_style = mog.new_style().foreground(mog.Color(0xFFFDF5)).background(mog.Color(0xFF5F87)).padding(0, 1) # .margin_right(1) - var encoding_style = status_nugget_style.background(mog.Color("#A550DF")).horizontal_alignment(position.right) + var encoding_style = status_nugget_style.background(mog.Color(0xA550DF)).horizontal_alignment(position.right) var status_text_style = status_bar_style.padding_left(1) - var fish_cake_style = status_nugget_style.background(mog.Color("#6124DF")) + var fish_cake_style = status_nugget_style.background(mog.Color(0x6124DF)) var status_key = status_style.render("STATUS") var encoding = encoding_style.render("UTF-8") diff --git a/benchmarks/run.mojo b/benchmarks/run.mojo index ed196e0..9bfb65e 100644 --- a/benchmarks/run.mojo +++ b/benchmarks/run.mojo @@ -9,7 +9,7 @@ import mog var style = mog.new_style().horizontal_alignment(mog.center).vertical_alignment(mog.center).padding(0, 1) -var header_style = style.foreground(mog.Color("#39E506")) +var header_style = style.foreground(mog.Color(0x39E506)) fn table_styling(row: Int, col: Int) -> mog.Style: diff --git a/examples/readme/basic.mojo b/examples/readme/basic.mojo index 0f65dec..76b9c2e 100644 --- a/examples/readme/basic.mojo +++ b/examples/readme/basic.mojo @@ -5,8 +5,8 @@ fn main(): var style = ( mog.new_style() .bold(True) - .foreground(mog.Color("#FAFAFA")) - .background(mog.Color("#7D56F4")) + .foreground(mog.Color(0xFAFAFA)) + .background(mog.Color(0x7D56F4)) .padding_top(2) .padding_left(4) .width(22) diff --git a/examples/readme/layout.mojo b/examples/readme/layout.mojo index 3d7fa2b..7f20c41 100644 --- a/examples/readme/layout.mojo +++ b/examples/readme/layout.mojo @@ -15,9 +15,9 @@ import mog alias width = 96 alias column_width = 30 -alias subtle = mog.AdaptiveColor(light="#D9DCCF", dark="#383838") -alias highlight = mog.AdaptiveColor(light="#874BFD", dark="#7D56F4") -alias special = mog.AdaptiveColor(light="#43BF6D", dark="#73F59F") +alias subtle = mog.AdaptiveColor(light=0xD9DCCF, dark=0x383838) +alias highlight = mog.AdaptiveColor(light=0x874BFD, dark=0x7D56F4) +alias special = mog.AdaptiveColor(light=0x43BF6D, dark=0x73F59F) fn build_tabs() -> String: @@ -75,16 +75,16 @@ fn build_description() -> String: fn build_dialog_box() -> String: var dialog_box_style = mog.new_style().alignment(position.center).border(rounded_border()).border_foreground( - mog.Color("#874BFD") + mog.Color(0x874BFD) ).padding(1, 0) - var button_style = mog.new_style().foreground(mog.Color("#FFF7DB")).background(mog.Color("#888B7E")).padding( + var button_style = mog.new_style().foreground(mog.Color(0xFFF7DB)).background(mog.Color(0x888B7E)).padding( 0, 3 ).margin_top(1) - var active_button_style = button_style.foreground(mog.Color("#FFF7DB")).background( - mog.Color("#F25D94") - ).margin_right(2).underline() + var active_button_style = button_style.foreground(mog.Color(0xFFF7DB)).background(mog.Color(0xF25D94)).margin_right( + 2 + ).underline() var ok_button = active_button_style.render("Yes") var cancel_button = button_style.render("Maybe") @@ -121,7 +121,7 @@ fn build_lists() -> String: var check_mark = mog.new_style().foreground(special).padding_right(1).render("✔") - var list_done = mog.new_style().crossout().foreground(mog.AdaptiveColor(light="#969B86", dark="#696969")) + var list_done = mog.new_style().crossout().foreground(mog.AdaptiveColor(light=0x969B86, dark=0x696969)) var lists = join_horizontal( position.top, @@ -166,7 +166,7 @@ fn build_lists() -> String: fn build_history() -> String: var history_style = mog.new_style().height(20).width(column_width).padding(1, 2).margin(1, 3, 0, 0).alignment( position.left - ).foreground(mog.Color("#FFFDF5")).background(highlight) + ).foreground(mog.Color(0xFFFDF5)).background(highlight) alias history_a = "The Romans learned from the Greeks that quinces slowly cooked with honey would “set” when cool. The Apicius gives a recipe for preserving whole quinces, stems and leaves attached, in a bath of honey diluted with defrutum: Roman marmalade. Preserves of quince and lemon appear (along with rose, apple, plum and pear) in the Book of ceremonies of the Byzantine Emperor Constantine VII Porphyrogennetos." alias history_b = "Medieval quince preserves, which went by the French name cotignac, produced in a clear version and a fruit pulp version, began to lose their medieval seasoning of spices in the 16th century. In the 17th century, La Varenne provided recipes for both thick and clear cotignac." @@ -181,17 +181,17 @@ fn build_history() -> String: fn build_status_bar() -> String: - var status_nugget_style = mog.new_style().foreground(mog.Color("#FFFDF5")).padding(0, 1) + var status_nugget_style = mog.new_style().foreground(mog.Color(0xFFFDF5)).padding(0, 1) - var status_bar_style = mog.new_style().foreground(mog.Color("#C1C6B2")).background(mog.Color("#353533")) + var status_bar_style = mog.new_style().foreground(mog.Color(0xC1C6B2)).background(mog.Color(0x353533)) - var status_style = mog.new_style().foreground(mog.Color("#FFFDF5")).background(mog.Color("#FF5F87")).padding(0, 1) + var status_style = mog.new_style().foreground(mog.Color(0xFFFDF5)).background(mog.Color(0xFF5F87)).padding(0, 1) # .margin_right(1) - var encoding_style = status_nugget_style.background(mog.Color("#A550DF")).horizontal_alignment(position.right) + var encoding_style = status_nugget_style.background(mog.Color(0xA550DF)).horizontal_alignment(position.right) var status_text_style = status_bar_style.padding_left(1) - var fish_cake_style = status_nugget_style.background(mog.Color("#6124DF")) + var fish_cake_style = status_nugget_style.background(mog.Color(0x6124DF)) var status_key = status_style.render("STATUS") var encoding = encoding_style.render("UTF-8") diff --git a/examples/table/ansi.mojo b/examples/table/ansi.mojo index 55e2f89..ffbb06d 100644 --- a/examples/table/ansi.mojo +++ b/examples/table/ansi.mojo @@ -2,7 +2,7 @@ import mog fn main(): - var s = mog.new_style().foreground(mog.Color("240")) + var s = mog.new_style().foreground(mog.Color(240)) var t = mog.new_table() t.width = 50 diff --git a/examples/table/pokemon.mojo b/examples/table/pokemon.mojo index f80e2ba..da74c52 100644 --- a/examples/table/pokemon.mojo +++ b/examples/table/pokemon.mojo @@ -7,15 +7,15 @@ # fn build_color_mapping() -> Dict[mog.Color]: # var type_colors = Dict[mog.Color]() -# type_colors.put("Bug", mog.Color("#D7FF87")) -# type_colors.put("Electric", mog.Color("#FDFF90")) -# type_colors.put("Fire", mog.Color("#FF7698")) -# type_colors.put("Flying", mog.Color("#FF87D7")) -# type_colors.put("Grass", mog.Color("#75FBAB")) -# type_colors.put("Ground", mog.Color("#FF875F")) -# type_colors.put("Normal", mog.Color("#929292")) -# type_colors.put("Poison", mog.Color("#7D5AFC")) -# type_colors.put("Water", mog.Color("#00E2C7")) +# type_colors.put("Bug", mog.Color(0xD7FF87)) +# type_colors.put("Electric", mog.Color(0xFDFF90)) +# type_colors.put("Fire", mog.Color(0xFF7698)) +# type_colors.put("Flying", mog.Color(0xFF87D7)) +# type_colors.put("Grass", mog.Color(0x75FBAB)) +# type_colors.put("Ground", mog.Color(0xFF875F)) +# type_colors.put("Normal", mog.Color(0x929292)) +# type_colors.put("Poison", mog.Color(0x7D5AFC)) +# type_colors.put("Water", mog.Color(0x00E2C7)) # return type_colors @@ -24,15 +24,15 @@ # fn build_dim_color_mapping() -> Dict[mog.Color]: # var dim_type_colors = Dict[mog.Color]() -# dim_type_colors.put("Bug", mog.Color("#97AD64")) -# dim_type_colors.put("Electric", mog.Color("#FCFF5F")) -# dim_type_colors.put("Fire", mog.Color("#BA5F75")) -# dim_type_colors.put("Flying", mog.Color("#C97AB2")) -# dim_type_colors.put("Grass", mog.Color("#59B980")) -# dim_type_colors.put("Ground", mog.Color("#C77252")) -# dim_type_colors.put("Normal", mog.Color("#727272")) -# dim_type_colors.put("Poison", mog.Color("#634BD0")) -# dim_type_colors.put("Water", mog.Color("#439F8E")) +# dim_type_colors.put("Bug", mog.Color(0x97AD64)) +# dim_type_colors.put("Electric", mog.Color(0xFCFF5F)) +# dim_type_colors.put("Fire", mog.Color(0xBA5F75)) +# dim_type_colors.put("Flying", mog.Color(0xC97AB2)) +# dim_type_colors.put("Grass", mog.Color(0x59B980)) +# dim_type_colors.put("Ground", mog.Color(0xC77252)) +# dim_type_colors.put("Normal", mog.Color(0x727272)) +# dim_type_colors.put("Poison", mog.Color(0x634BD0)) +# dim_type_colors.put("Water", mog.Color(0x439F8E)) # return dim_type_colors @@ -77,15 +77,15 @@ # # if is_even: # # colors = DIM_TYPE_COLORS -# # var color = colors.get(data[row - 1][col], mog.Color("#FFFFFF")) +# # var color = colors.get(data[row - 1][col], mog.Color(0xFFFFFF)) # # var copy_style = style.foreground(color) # # return copy_style # # if is_even: -# # var copy_style = style.foreground(mog.Color("245")) +# # var copy_style = style.foreground(mog.Color("245)) # # return copy_style -# # var copy_style = style.foreground(mog.Color("252")) +# # var copy_style = style.foreground(mog.Color("252)) # # return copy_style # # return style_func @@ -98,12 +98,12 @@ # # .padding_left(1) # # var header_style = style \ -# # .foreground(mog.Color("252")) \ +# # .foreground(mog.Color("252)) \ # # .bold() # # var selected_style = style \ -# # .foreground(mog.Color("#01BE85")) \ -# # .background(mog.Color("#00432F")) +# # .foreground(mog.Color(0x01BE85)) \ +# # .background(mog.Color(0x00432F)) # @always_inline # fn capitalize_headers(data: List[String]) -> List[String]: @@ -122,12 +122,12 @@ # .padding_left(1) # var header_style = style \ -# .foreground(mog.Color("252")) \ +# .foreground(mog.Color("252)) \ # .bold() # var selected_style = style \ -# .foreground(mog.Color("#01BE85")) \ -# .background(mog.Color("#00432F")) +# .foreground(mog.Color(0x01BE85)) \ +# .background(mog.Color(0x00432F)) # if row == 0: # return header_style @@ -141,19 +141,19 @@ # if is_even: # colors = DIM_TYPE_COLORS -# var color = colors.get(data[row - 1][col], mog.Color("#FFFFFF")) +# var color = colors.get(data[row - 1][col], mog.Color(0xFFFFFF)) # var copy_style = style.foreground(color) # return copy_style # if is_even: -# var copy_style = style.foreground(mog.Color("245")) +# var copy_style = style.foreground(mog.Color("245)) # return copy_style -# var copy_style = style.foreground(mog.Color("252")) +# var copy_style = style.foreground(mog.Color("252)) # return copy_style # var border_style = mog.Style() -# border_style = border_style.foreground(mog.Color("238")) +# border_style = border_style.foreground(mog.Color("238)) # var table = mog.new_table() # table.rows(data) # table.width = 100 diff --git a/external/mist/ansi_colors.mojo b/external/mist/ansi_colors.mojo index 3bb781d..421c727 100644 --- a/external/mist/ansi_colors.mojo +++ b/external/mist/ansi_colors.mojo @@ -1,260 +1,259 @@ -# RGB values of ANSI colors (0-255). -alias ANSI_HEX_CODES = List[String]( - "#000000", - "#000000", - "#800000", - "#008000", - "#808000", - "#000080", - "#800080", - "#008080", - "#c0c0c0", - "#808080", - "#ff0000", - "#00ff00", - "#ffff00", - "#0000ff", - "#ff00ff", - "#00ffff", - "#ffffff", - "#000000", - "#00005f", - "#000087", - "#0000af", - "#0000d7", - "#0000ff", - "#005f00", - "#005f5f", - "#005f87", - "#005faf", - "#005fd7", - "#005fff", - "#008700", - "#00875f", - "#008787", - "#0087af", - "#0087d7", - "#0087ff", - "#00af00", - "#00af5f", - "#00af87", - "#00afaf", - "#00afd7", - "#00afff", - "#00d700", - "#00d75f", - "#00d787", - "#00d7af", - "#00d7d7", - "#00d7ff", - "#00ff00", - "#00ff5f", - "#00ff87", - "#00ffaf", - "#00ffd7", - "#00ffff", - "#5f0000", - "#5f005f", - "#5f0087", - "#5f00af", - "#5f00d7", - "#5f00ff", - "#5f5f00", - "#5f5f5f", - "#5f5f87", - "#5f5faf", - "#5f5fd7", - "#5f5fff", - "#5f8700", - "#5f875f", - "#5f8787", - "#5f87af", - "#5f87d7", - "#5f87ff", - "#5faf00", - "#5faf5f", - "#5faf87", - "#5fafaf", - "#5fafd7", - "#5fafff", - "#5fd700", - "#5fd75f", - "#5fd787", - "#5fd7af", - "#5fd7d7", - "#5fd7ff", - "#5fff00", - "#5fff5f", - "#5fff87", - "#5fffaf", - "#5fffd7", - "#5fffff", - "#870000", - "#87005f", - "#870087", - "#8700af", - "#8700d7", - "#8700ff", - "#875f00", - "#875f5f", - "#875f87", - "#875faf", - "#875fd7", - "#875fff", - "#878700", - "#87875f", - "#878787", - "#8787af", - "#8787d7", - "#8787ff", - "#87af00", - "#87af5f", - "#87af87", - "#87afaf", - "#87afd7", - "#87afff", - "#87d700", - "#87d75f", - "#87d787", - "#87d7af", - "#87d7d7", - "#87d7ff", - "#87ff00", - "#87ff5f", - "#87ff87", - "#87ffaf", - "#87ffd7", - "#87ffff", - "#af0000", - "#af005f", - "#af0087", - "#af00af", - "#af00d7", - "#af00ff", - "#af5f00", - "#af5f5f", - "#af5f87", - "#af5faf", - "#af5fd7", - "#af5fff", - "#af8700", - "#af875f", - "#af8787", - "#af87af", - "#af87d7", - "#af87ff", - "#afaf00", - "#afaf5f", - "#afaf87", - "#afafaf", - "#afafd7", - "#afafff", - "#afd700", - "#afd75f", - "#afd787", - "#afd7af", - "#afd7d7", - "#afd7ff", - "#afff00", - "#afff5f", - "#afff87", - "#afffaf", - "#afffd7", - "#afffff", - "#d70000", - "#d7005f", - "#d70087", - "#d700af", - "#d700d7", - "#d700ff", - "#d75f00", - "#d75f5f", - "#d75f87", - "#d75faf", - "#d75fd7", - "#d75fff", - "#d78700", - "#d7875f", - "#d78787", - "#d787af", - "#d787d7", - "#d787ff", - "#d7af00", - "#d7af5f", - "#d7af87", - "#d7afaf", - "#d7afd7", - "#d7afff", - "#d7d700", - "#d7d75f", - "#d7d787", - "#d7d7af", - "#d7d7d7", - "#d7d7ff", - "#d7ff00", - "#d7ff5f", - "#d7ff87", - "#d7ffaf", - "#d7ffd7", - "#d7ffff", - "#ff0000", - "#ff005f", - "#ff0087", - "#ff00af", - "#ff00d7", - "#ff00ff", - "#ff5f00", - "#ff5f5f", - "#ff5f87", - "#ff5faf", - "#ff5fd7", - "#ff5fff", - "#ff8700", - "#ff875f", - "#ff8787", - "#ff87af", - "#ff87d7", - "#ff87ff", - "#ffaf00", - "#ffaf5f", - "#ffaf87", - "#ffafaf", - "#ffafd7", - "#ffafff", - "#ffd700", - "#ffd75f", - "#ffd787", - "#ffd7af", - "#ffd7d7", - "#ffd7ff", - "#ffff00", - "#ffff5f", - "#ffff87", - "#ffffaf", - "#ffffd7", - "#ffffff", - "#080808", - "#121212", - "#1c1c1c", - "#262626", - "#303030", - "#3a3a3a", - "#444444", - "#4e4e4e", - "#585858", - "#626262", - "#6c6c6c", - "#767676", - "#808080", - "#8a8a8a", - "#949494", - "#9e9e9e", - "#a8a8a8", - "#b2b2b2", - "#bcbcbc", - "#c6c6c6", - "#d0d0d0", - "#dadada", - "#e4e4e4", - "#eeeeee", +alias ANSI_HEX_CODES = List[UInt32]( + 0x000000, + 0x800000, + 0x008000, + 0x808000, + 0x000080, + 0x800080, + 0x008080, + 0xC0C0C0, + 0x808080, + 0xFF0000, + 0x00FF00, + 0xFFFF00, + 0x0000FF, + 0xFF00FF, + 0x00FFFF, + 0xFFFFFF, + 0x000000, + 0x00005F, + 0x000087, + 0x0000AF, + 0x0000D7, + 0x0000FF, + 0x005F00, + 0x005F5F, + 0x005F87, + 0x005FAF, + 0x005FD7, + 0x005FFF, + 0x008700, + 0x00875F, + 0x008787, + 0x0087AF, + 0x0087D7, + 0x0087FF, + 0x00AF00, + 0x00AF5F, + 0x00AF87, + 0x00AFAF, + 0x00AFD7, + 0x00AFFF, + 0x00D700, + 0x00D75F, + 0x00D787, + 0x00D7AF, + 0x00D7D7, + 0x00D7FF, + 0x00FF00, + 0x00FF5F, + 0x00FF87, + 0x00FFAF, + 0x00FFD7, + 0x00FFFF, + 0x5F0000, + 0x5F005F, + 0x5F0087, + 0x5F00AF, + 0x5F00D7, + 0x5F00FF, + 0x5F5F00, + 0x5F5F5F, + 0x5F5F87, + 0x5F5FAF, + 0x5F5FD7, + 0x5F5FFF, + 0x5F8700, + 0x5F875F, + 0x5F8787, + 0x5F87AF, + 0x5F87D7, + 0x5F87FF, + 0x5FAF00, + 0x5FAF5F, + 0x5FAF87, + 0x5FAFAF, + 0x5FAFD7, + 0x5FAFFF, + 0x5FD700, + 0x5FD75F, + 0x5FD787, + 0x5FD7AF, + 0x5FD7D7, + 0x5FD7FF, + 0x5FFF00, + 0x5FFF5F, + 0x5FFF87, + 0x5FFFAF, + 0x5FFFD7, + 0x5FFFFF, + 0x870000, + 0x87005F, + 0x870087, + 0x8700AF, + 0x8700D7, + 0x8700FF, + 0x875F00, + 0x875F5F, + 0x875F87, + 0x875FAF, + 0x875FD7, + 0x875FFF, + 0x878700, + 0x87875F, + 0x878787, + 0x8787AF, + 0x8787D7, + 0x8787FF, + 0x87AF00, + 0x87AF5F, + 0x87AF87, + 0x87AFAF, + 0x87AFD7, + 0x87AFFF, + 0x87D700, + 0x87D75F, + 0x87D787, + 0x87D7AF, + 0x87D7D7, + 0x87D7FF, + 0x87FF00, + 0x87FF5F, + 0x87FF87, + 0x87FFAF, + 0x87FFD7, + 0x87FFFF, + 0xAF0000, + 0xAF005F, + 0xAF0087, + 0xAF00AF, + 0xAF00D7, + 0xAF00FF, + 0xAF5F00, + 0xAF5F5F, + 0xAF5F87, + 0xAF5FAF, + 0xAF5FD7, + 0xAF5FFF, + 0xAF8700, + 0xAF875F, + 0xAF8787, + 0xAF87AF, + 0xAF87D7, + 0xAF87FF, + 0xAFAF00, + 0xAFAF5F, + 0xAFAF87, + 0xAFAFAF, + 0xAFAFD7, + 0xAFAFFF, + 0xAFD700, + 0xAFD75F, + 0xAFD787, + 0xAFD7AF, + 0xAFD7D7, + 0xAFD7FF, + 0xAFFF00, + 0xAFFF5F, + 0xAFFF87, + 0xAFFFAF, + 0xAFFFD7, + 0xAFFFFF, + 0xD70000, + 0xD7005F, + 0xD70087, + 0xD700AF, + 0xD700D7, + 0xD700FF, + 0xD75F00, + 0xD75F5F, + 0xD75F87, + 0xD75FAF, + 0xD75FD7, + 0xD75FFF, + 0xD78700, + 0xD7875F, + 0xD78787, + 0xD787AF, + 0xD787D7, + 0xD787FF, + 0xD7AF00, + 0xD7AF5F, + 0xD7AF87, + 0xD7AFAF, + 0xD7AFD7, + 0xD7AFFF, + 0xD7D700, + 0xD7D75F, + 0xD7D787, + 0xD7D7AF, + 0xD7D7D7, + 0xD7D7FF, + 0xD7FF00, + 0xD7FF5F, + 0xD7FF87, + 0xD7FFAF, + 0xD7FFD7, + 0xD7FFFF, + 0xFF0000, + 0xFF005F, + 0xFF0087, + 0xFF00AF, + 0xFF00D7, + 0xFF00FF, + 0xFF5F00, + 0xFF5F5F, + 0xFF5F87, + 0xFF5FAF, + 0xFF5FD7, + 0xFF5FFF, + 0xFF8700, + 0xFF875F, + 0xFF8787, + 0xFF87AF, + 0xFF87D7, + 0xFF87FF, + 0xFFAF00, + 0xFFAF5F, + 0xFFAF87, + 0xFFAFAF, + 0xFFAFD7, + 0xFFAFFF, + 0xFFD700, + 0xFFD75F, + 0xFFD787, + 0xFFD7AF, + 0xFFD7D7, + 0xFFD7FF, + 0xFFFF00, + 0xFFFF5F, + 0xFFFF87, + 0xFFFFAF, + 0xFFFFD7, + 0xFFFFFF, + 0x080808, + 0x121212, + 0x1C1C1C, + 0x262626, + 0x303030, + 0x3A3A3A, + 0x444444, + 0x4E4E4E, + 0x585858, + 0x626262, + 0x6C6C6C, + 0x767676, + 0x808080, + 0x8A8A8A, + 0x949494, + 0x9E9E9E, + 0xA8A8A8, + 0xB2B2B2, + 0xBCBCBC, + 0xC6C6C6, + 0xD0D0D0, + 0xDADADA, + 0xE4E4E4, + 0xEEEEEE, ) +"""RGB values of ANSI colors (0-255).""" diff --git a/external/mist/color.mojo b/external/mist/color.mojo index 805d91a..fce0ecb 100644 --- a/external/mist/color.mojo +++ b/external/mist/color.mojo @@ -44,8 +44,11 @@ trait Color(EqualityComparable, CollectionElement): ... -@value +@register_passable("trivial") struct NoColor(Color, Stringable): + fn __init__(inout self): + pass + fn __eq__(self, other: NoColor) -> Bool: return True @@ -60,21 +63,21 @@ struct NoColor(Color, Stringable): return "" -@value +@register_passable("trivial") struct ANSIColor(Color, Stringable): """ANSIColor is a color (0-15) as defined by the ANSI Standard.""" - var value: UInt8 + var value: UInt32 + + fn __init__(inout self, value: UInt32): + self.value = value - @always_inline fn __eq__(self, other: ANSIColor) -> Bool: return self.value == other.value - @always_inline fn __ne__(self, other: ANSIColor) -> Bool: return self.value != other.value - @always_inline fn sequence(self, is_background: Bool) -> String: """Returns the ANSI Sequence for the color and the text. @@ -89,34 +92,26 @@ struct ANSIColor(Color, Stringable): return int_to_str(modifier + int(self.value) + 30) return int_to_str(modifier + int(self.value) - 8 + 90) - @always_inline fn __str__(self) -> String: """String returns the ANSI Sequence for the color and the text.""" return ANSI_HEX_CODES[int(self.value)] - @always_inline - fn convert_to_rgb(self) -> hue.Color: - """Converts an ANSI color to hue.Color by looking up the hex value and converting it.""" - var hex: String = ANSI_HEX_CODES[int(self.value)] - - return hex_to_rgb(hex) - -@value +@register_passable("trivial") struct ANSI256Color(Color, Stringable): """ANSI256Color is a color (16-255) as defined by the ANSI Standard.""" - var value: UInt8 + var value: UInt32 + + fn __init__(inout self, value: UInt32): + self.value = value - @always_inline fn __eq__(self, other: ANSI256Color) -> Bool: return self.value == other.value - @always_inline fn __ne__(self, other: ANSI256Color) -> Bool: return self.value != other.value - @always_inline fn sequence(self, is_background: Bool) -> String: """Returns the ANSI Sequence for the color and the text. @@ -129,85 +124,108 @@ struct ANSI256Color(Color, Stringable): return prefix + ";5;" + int_to_str(int(self.value)) - @always_inline fn __str__(self) -> String: """String returns the ANSI Sequence for the color and the text.""" return ANSI_HEX_CODES[int(self.value)] - @always_inline - fn convert_to_rgb(self) -> hue.Color: - """Converts an ANSI color to hue.Color by looking up the hex value and converting it.""" - var hex: String = ANSI_HEX_CODES[int(self.value)] - - return hex_to_rgb(hex) - - -fn convert_base16_to_base10(value: String) -> Int: - """Converts a base 16 number to base 10. - https://www.catalyst2.com/knowledgebase/dictionary/hexadecimal-base-16-numbers/#:~:text=To%20convert%20the%20hex%20number,16%20%2B%200%20%3D%2016). - - Args: - value: Hexadecimal number. - - Returns: - Base 10 number. - """ - alias mapping = List[String]("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f") - - # We assume mapping.find always returns a value considering the value passed in is a valid hex value - # and the mapping has all the values. - var length = len(value) - var total: Int = 0 - for i in range(length - 1, -1, -1): - var exponent = length - 1 - i - - @parameter - for j in range(16): - if mapping[j] == value[i]: - total += j * (16**exponent) - break - return total +# // ansiToRGB converts an ANSI color to a 24-bit RGB color. +# // +# // r, g, b := ansiToRGB(57) +# func ansiToRGB(ansi uint32) (uint32, uint32, uint32) { +# // For out-of-range values return black. +# if ansi > 255 { +# return 0, 0, 0 +# } + +# // Low ANSI. +# if ansi < 16 { +# h, ok := lowANSI[ansi] +# if !ok { +# return 0, 0, 0 +# } +# r, g, b := hexToRGB(h) +# return r, g, b +# } + +# // Grays. +# if ansi > 231 { +# s := (ansi-232)*10 + 8 +# return s, s, s +# } + +# // ANSI256. +# n := ansi - 16 +# b := n % 6 +# g := (n - b) / 6 % 6 +# r := (n - b - g*6) / 36 % 6 +# for _, v := range []*uint32{&r, &g, &b} { +# if *v > 0 { +# c := *v*40 + 55 +# *v = c +# } +# } + +# return r, g, b +# } + + +fn ansi_to_rgb(ansi: UInt32) -> (UInt32, UInt32, UInt32): + """Converts an ANSI color to a 24-bit RGB color.""" + # For out-of-range values return black. + if ansi > 255: + return UInt32(0), UInt32(0), UInt32(0) + + # Low ANSI. + if ansi < 16: + var h = ANSI_HEX_CODES[int(ansi)] + return hex_to_rgb(h) + + # Grays. + if ansi > 231: + var s = (ansi - 232) * 10 + 8 + return s, s, s + + # ANSI256. + var n = ansi - 16 + var b = n % 6 + var g = (n - b) / 6 % 6 + var r = (n - b - g * 6) / 36 % 6 + var rgb = List[UInt32](r, g, b) + var v = rgb[0] + var i = 0 + while i < 3: + if v > 0: + var c = v * 40 + 55 + v = c + i += 1 + return r, g, b -fn hex_to_rgb(value: String) -> hue.Color: - """Converts a hex color to hue.Color. - Args: - value: Hex color value. +fn hex_to_rgb(hex: UInt32) -> (UInt32, UInt32, UInt32): + """Converts a number in hexadecimal format to red, green, and blue values. - Returns: - Color. + `r, g, b = hex_to_rgb(0x0000FF)`. """ - var hex = value[1:] - var results = List[Int]() - - @parameter - for i in range(3): - results.append(convert_base16_to_base10(hex[(i * 2) : (i * 2) + 2])) - - return hue.Color(results[0], results[1], results[2]) + return hex >> 16, hex >> 8 & 0xFF, hex & 0xFF -@value +@register_passable("trivial") struct RGBColor(Color): """RGBColor is a hex-encoded color, e.g. '#abcdef'.""" - var value: String + var value: UInt32 - @always_inline - fn __init__(inout self, value: String): - self.value = value.lower() + fn __init__(inout self, value: UInt32): + self.value = value - @always_inline fn __eq__(self, other: RGBColor) -> Bool: return self.value == other.value - @always_inline fn __ne__(self, other: RGBColor) -> Bool: return self.value != other.value - @always_inline fn sequence(self, is_background: Bool) -> String: """Returns the ANSI Sequence for the color and the text. @@ -223,20 +241,15 @@ struct RGBColor(Color): return ( prefix + String(";2;") - + int_to_str(int(rgb.R)) + + int_to_str(int(rgb[0])) + ";" - + int_to_str(int(rgb.G)) + + int_to_str(int(rgb[1])) + ";" - + int_to_str(int(rgb.B)) + + int_to_str(int(rgb[2])) ) - @always_inline - fn convert_to_rgb(self) -> hue.Color: - """Converts the Hex code value to hue.Color.""" - return hex_to_rgb(self.value) - -fn ansi256_to_ansi(value: UInt8) -> ANSIColor: +fn ansi256_to_ansi(value: UInt32) -> ANSIColor: """Converts an ANSI256 color to an ANSI color. Args: @@ -249,8 +262,12 @@ fn ansi256_to_ansi(value: UInt8) -> ANSIColor: var i: Int = 0 while i <= 15: - var hb = hex_to_rgb(ANSI_HEX_CODES[i]) - var d = h.distance_HSLuv(hb) + var hb = hex_to_rgb(ANSI_HEX_CODES[int(i)]) + var d = hue.Color( + h[0].cast[DType.float64](), h[1].cast[DType.float64](), h[2].cast[DType.float64]() + ).distance_HSLuv( + hue.Color(hb[0].cast[DType.float64](), hb[1].cast[DType.float64](), hb[2].cast[DType.float64]()) + ) if d < md: md = d @@ -266,8 +283,7 @@ fn v2ci(value: Float64) -> Int: return 0 elif value < 115: return 1 - else: - return int((value - 35) / 40) + return int((value - 35) / 40) fn hex_to_ansi256(color: hue.Color) -> ANSI256Color: diff --git a/external/mist/profile.mojo b/external/mist/profile.mojo index b37e949..b2abc24 100644 --- a/external/mist/profile.mojo +++ b/external/mist/profile.mojo @@ -1,4 +1,5 @@ import os +import external.hue from .color import ( NoColor, ANSIColor, @@ -8,6 +9,7 @@ from .color import ( hex_to_ansi256, ansi256_to_ansi, hex_to_rgb, + ansi_to_rgb, int_to_str, ) @@ -72,7 +74,6 @@ struct Profile: alias valid = InlineArray[Int, 4](TRUE_COLOR, ANSI256, ANSI, ASCII) var value: Int - @always_inline fn __init__(inout self, value: Int): """ Initialize a new profile with the given profile type. @@ -86,14 +87,12 @@ struct Profile: self.value = value - @always_inline fn __init__(inout self): """ Initialize a new profile with the given profile type. """ self.value = get_color_profile() - @always_inline fn __copyinit__(inout self, other: Profile): self.value = other.value @@ -119,7 +118,9 @@ struct Profile: var h = hex_to_rgb(color[RGBColor].value) if self.value != TRUE_COLOR: - var ansi256 = hex_to_ansi256(h) + var ansi256 = hex_to_ansi256( + hue.Color(h[0].cast[DType.float64](), h[1].cast[DType.float64](), h[2].cast[DType.float64]()) + ) if self.value == ANSI: return ansi256_to_ansi(ansi256.value) @@ -130,33 +131,19 @@ struct Profile: # If it somehow gets here, just return No Color until I can figure out how to just return whatever color was passed in. return color[NoColor] - @always_inline - fn color(self, value: String) -> AnyColor: + fn color(self, value: UInt32) -> AnyColor: """Color creates a Color from a string. Valid inputs are hex colors, as well as ANSI color codes (0-15, 16-255). If an invalid input is passed in, NoColor() is returned which will not apply any coloring. Args: value: The string to convert to a color. """ - if len(value) == 0: - return NoColor() - if self.value == ASCII: return NoColor() - if value[0] == "#": - return self.convert(RGBColor(value)) - - return NoColor() - - @always_inline - fn color(self, value: UInt8) -> AnyColor: - """Color creates a Color from a string. Valid inputs are hex colors, as well as - ANSI color codes (0-15, 16-255). If an invalid input is passed in, NoColor() is returned which will not apply any coloring. - - Args: - value: The int to convert to a color. - """ if value < 16: return self.convert(ANSIColor(value)) - return self.convert(ANSI256Color(value)) + elif value < 256: + return self.convert(ANSI256Color(value)) + + return self.convert(RGBColor(value)) diff --git a/external/mist/renderers.mojo b/external/mist/renderers.mojo index 4ef9c3f..15c2b02 100644 --- a/external/mist/renderers.mojo +++ b/external/mist/renderers.mojo @@ -2,19 +2,19 @@ from .style import Style, new_style from .profile import Profile -alias RED = "#E88388" -alias GREEN = "#A8CC8C" -alias YELLOW = "#DBAB79" -alias BLUE = "#71BEF2" -alias MAGENTA = "#D290E4" -alias CYAN = "#66C2CD" -alias GRAY = "#B9BFCA" +alias RED = 0xE88388 +alias GREEN = 0xA8CC8C +alias YELLOW = 0xDBAB79 +alias BLUE = 0x71BEF2 +alias MAGENTA = 0xD290E4 +alias CYAN = 0x66C2CD +alias GRAY = 0xB9BFCA # Convenience functions for quick style application -fn render_as_color(text: String, color: String) -> String: +fn render_as_color(text: String, color: UInt32) -> String: var profile = Profile() - return new_style(profile).foreground(profile.color(color)).render(text) + return new_style(profile.value).foreground(color=profile.color(color)).render(text) fn red(text: String) -> String: @@ -52,9 +52,9 @@ fn gray(text: String) -> String: return render_as_color(text, GRAY) -fn render_with_background_color(text: String, color: String) -> String: +fn render_with_background_color(text: String, color: UInt32) -> String: var profile = Profile() - return new_style(profile).background(profile.color(color)).render(text) + return new_style().background(color=profile.color(color)).render(text) fn red_background(text: String) -> String: diff --git a/external/mist/style.mojo b/external/mist/style.mojo index 407fda3..5eb06a3 100644 --- a/external/mist/style.mojo +++ b/external/mist/style.mojo @@ -11,7 +11,6 @@ from .color import ( ansi256_to_ansi, ) from .profile import get_color_profile, ASCII -import time # Text formatting sequences alias reset = "0" @@ -41,20 +40,19 @@ struct Style: In reality, these styles are turning visual terminal features on and off around the text it's styling. This struct should be considered immutable and each style added returns a new instance of itself rather than modifying the struct in place. - It's recommended to use the `new` static method to create a new instance of Style so that you can chain style methods together. + It's recommended to use `new_style()` function to create a new instance of Style so that you can chain style methods together. Example: - ``` - import mist + ```mojo + import mist - var style = mist.new_style().foreground("#E88388") - print(style.render("Hello World")) - ``` + var style = mist.new_style().foreground(0xE88388) + print(style.render("Hello World")) + ``` """ var styles: List[String] var profile: Profile - @always_inline fn __init__(inout self, profile: Profile, *, styles: List[String] = List[String]()): """Constructs a Style. Use new_style() instead of __init__ to chain function calls. @@ -65,7 +63,6 @@ struct Style: self.styles = styles self.profile = profile - @always_inline fn __init__(inout self, *, styles: List[String] = List[String]()): """Constructs a Style. Use new_style() instead of __init__ to chain function calls. @@ -75,64 +72,53 @@ struct Style: self.styles = styles self.profile = Profile() - @always_inline fn _add_style(self, style: String) -> Self: """Creates a deepcopy of Self, adds a style to it's list of styles, and returns that. Immutability instead of mutating the object. Args: style: The ANSI style to add to the list of styles. """ - var new_styles = self.get_styles() - new_styles.append(style) - return Self(self.profile, styles=new_styles) + var new = self + new.styles.append(style) + return new - @always_inline fn get_styles(self) -> List[String]: """Return a deepcopy of the styles list.""" return List[String](self.styles) - @always_inline fn bold(self) -> Self: """Makes the text bold when rendered.""" return self._add_style(bold) - @always_inline fn faint(self) -> Self: """Makes the text faint when rendered.""" return self._add_style(faint) - @always_inline fn italic(self) -> Self: """Makes the text italic when rendered.""" return self._add_style(italic) - @always_inline fn underline(self) -> Self: """Makes the text underlined when rendered.""" return self._add_style(underline) - @always_inline fn blink(self) -> Self: """Makes the text blink when rendered.""" return self._add_style(blink) - @always_inline fn reverse(self) -> Self: """Makes the text have reversed background and foreground colors when rendered.""" return self._add_style(reverse) - @always_inline fn crossout(self) -> Self: """Makes the text crossed out when rendered.""" return self._add_style(crossout) - @always_inline fn overline(self) -> Self: """Makes the text overlined when rendered.""" return self._add_style(overline) - @always_inline - fn background(self, color: AnyColor) -> Self: + fn background(self, *, color: AnyColor) -> Self: """Set the background color of the text when it's rendered. Args: @@ -156,8 +142,7 @@ struct Style: sequence = c.sequence(True) return self._add_style(sequence) - @always_inline - fn background(self, color_value: String) -> Self: + fn background(self, color_value: UInt32) -> Self: """Shorthand for using the style profile to set the background color of the text. Args: @@ -166,34 +151,9 @@ struct Style: Returns: A new Style with the background color set. """ - return self.background(self.profile.color(color_value)) + return self.background(color=self.profile.color(color_value)) - @always_inline - fn background(self, color_value: StringLiteral) -> Self: - """Shorthand for using the style profile to set the background color of the text. - - Args: - color_value: The color value to set the background to. This can be a hex value, an ANSI color, or an RGB color. - - Returns: - A new Style with the background color set. - """ - return self.background(self.profile.color(color_value)) - - @always_inline - fn background(self, color_value: UInt8) -> Self: - """Shorthand for using the style profile to set the background color of the text. - - Args: - color_value: The color value to set the background to. This can be a hex value, an ANSI color, or an RGB color. - - Returns: - A new Style with the background color set. - """ - return self.background(self.profile.color(color_value)) - - @always_inline - fn foreground(self, color: AnyColor) -> Self: + fn foreground(self, *, color: AnyColor) -> Self: """Set the foreground color of the text. Args: @@ -214,8 +174,7 @@ struct Style: sequence = color[RGBColor].sequence(False) return self._add_style(sequence) - @always_inline - fn foreground(self, color_value: String) -> Self: + fn foreground(self, color_value: UInt32) -> Self: """Shorthand for using the style profile to set the foreground color of the text. Args: @@ -224,33 +183,8 @@ struct Style: Returns: A new Style with the foreground color set. """ - return self.foreground(self.profile.color(color_value)) - - @always_inline - fn foreground(self, color_value: StringLiteral) -> Self: - """Shorthand for using the style profile to set the foreground color of the text. - - Args: - color_value: The color value to set the foreground to. This can be a hex value, an ANSI color, or an RGB color. + return self.foreground(color=self.profile.color(color_value)) - Returns: - A new Style with the foreground color set. - """ - return self.foreground(self.profile.color(color_value)) - - @always_inline - fn foreground(self, color_value: Int) -> Self: - """Shorthand for using the style profile to set the foreground color of the text. - - Args: - color_value: The color value to set the foreground to. This can be a hex value, an ANSI color, or an RGB color. - - Returns: - A new Style with the foreground color set. - """ - return self.foreground(self.profile.color(color_value)) - - @always_inline fn render(self, text: String) -> String: """Renders text with the styles applied to it. @@ -280,16 +214,7 @@ struct Style: return builder.render() -fn new_style() -> Style: - """Creates a new Style with no styles applied. - - Returns: - A new Style with the given color profile. - """ - return Style() - - -fn new_style(profile: Profile) -> Style: +fn new_style(profile: Optional[Int] = None) -> Style: """Creates a new Style with no styles applied. Args: @@ -298,4 +223,6 @@ fn new_style(profile: Profile) -> Style: Returns: A new Style with the given color profile. """ - return Style(profile) + if profile: + return Style(profile.value()[]) + return Style() diff --git a/mog/color.mojo b/mog/color.mojo index 6672f24..c7b68eb 100644 --- a/mog/color.mojo +++ b/mog/color.mojo @@ -47,22 +47,15 @@ struct Color(TerminalColor): Example usage: ```mojo var ansi_color = mog.Color(21) - var hex_color = mog.Color("#0000ff") + var hex_color = mog.Color(0x0000ff) ``` . """ - var value: Variant[UInt8, String] - - fn __init__(inout self, value: StringLiteral): - self.value = str(value) + var value: UInt32 fn color(self, renderer: Renderer) -> mist.AnyColor: - if self.value.isa[UInt8](): - return renderer.color_profile.color(self.value[UInt8]) - elif self.value.isa[String](): - return renderer.color_profile.color(self.value[String]) - return mist.NoColor() + return renderer.color_profile.color(self.value) @value @@ -83,7 +76,7 @@ struct ANSIColor(TerminalColor): ``` """ - var value: UInt8 + var value: UInt32 fn color(self, renderer: Renderer) -> mist.AnyColor: return Color(self.value).color(renderer) @@ -101,16 +94,12 @@ struct AdaptiveColor(TerminalColor): Example usage: ```mojo - var color = mog.AdaptiveColor(light="#0000ff", dark="#000099") + var color = mog.AdaptiveColor(light=0x0000ff, dark=0x000099) ``` """ - var light: Variant[UInt8, String] - var dark: Variant[UInt8, String] - - fn __init__(inout self, light: StringLiteral = "", dark: StringLiteral = ""): - self.light = str(light) - self.dark = str(dark) + var light: UInt32 + var dark: UInt32 fn color(self, renderer: Renderer) -> mist.AnyColor: if renderer.has_dark_background(): @@ -131,19 +120,14 @@ struct CompleteColor(TerminalColor): Example usage: ```mojo - var color = mog.CompleteColor(true_color="#0000ff", ansi256=21, ansi=4) + var color = mog.CompleteColor(true_color=0x0000ff, ansi256=21, ansi=4) ``` . """ - var true_color: String - var ansi256: Variant[UInt8, String] - var ansi: Variant[UInt8, String] - - fn __init__(inout self, true_color: StringLiteral = "", light: StringLiteral = "", dark: StringLiteral = ""): - self.true_color = str(true_color) - self.ansi256 = str(light) - self.ansi = str(dark) + var true_color: UInt32 + var ansi256: UInt32 + var ansi: UInt32 fn color(self, renderer: Renderer) -> mist.AnyColor: var p = renderer.color_profile @@ -170,8 +154,8 @@ struct CompleteAdaptiveColor(TerminalColor): Example usage: ```mojo var color = mog.CompleteAdaptiveColor( - light=mog.CompleteColor(true_color="#0000ff", ansi256=21, ansi=4), - dark=mog.CompleteColor(true_color="#000099", ansi256=22, ansi=5), + light=mog.CompleteColor(true_color=0x0000ff, ansi256=21, ansi=4), + dark=mog.CompleteColor(true_color=0x000099, ansi256=22, ansi=5), ) ``` . diff --git a/mog/style.mojo b/mog/style.mojo index fe361b0..0221ace 100644 --- a/mog/style.mojo +++ b/mog/style.mojo @@ -218,8 +218,8 @@ struct Style: var style = ( mog.new_style() .bold(True) - .foreground(mog.Color("#FAFAFA")) - .background(mog.Color("#7D56F4")) + .foreground(mog.Color(0xFAFAFA)) + .background(mog.Color(0x7D56F4)) .padding_top(2) .padding_left(4) .width(22) @@ -1896,26 +1896,26 @@ struct Style: # Sooooo verbose compared to just passing the string value. But this is closer to the lipgloss API. # It's more verbose because we can't pass around args with trait as the arg type. if fg.isa[Color](): - styler = styler.foreground(fg[Color].color(self.renderer)) + styler = styler.foreground(color=fg[Color].color(self.renderer)) elif fg.isa[ANSIColor](): - styler = styler.foreground(fg[ANSIColor].color(self.renderer)) + styler = styler.foreground(color=fg[ANSIColor].color(self.renderer)) elif fg.isa[AdaptiveColor](): - styler = styler.foreground(fg[AdaptiveColor].color(self.renderer)) + styler = styler.foreground(color=fg[AdaptiveColor].color(self.renderer)) elif fg.isa[CompleteColor](): - styler = styler.foreground(fg[CompleteColor].color(self.renderer)) + styler = styler.foreground(color=fg[CompleteColor].color(self.renderer)) elif fg.isa[CompleteAdaptiveColor](): - styler = styler.foreground(fg[CompleteAdaptiveColor].color(self.renderer)) + styler = styler.foreground(color=fg[CompleteAdaptiveColor].color(self.renderer)) if bg.isa[Color](): - styler = styler.background(bg[Color].color(self.renderer)) + styler = styler.background(color=bg[Color].color(self.renderer)) elif bg.isa[ANSIColor](): - styler = styler.background(bg[ANSIColor].color(self.renderer)) + styler = styler.background(color=bg[ANSIColor].color(self.renderer)) elif bg.isa[AdaptiveColor](): - styler = styler.background(bg[AdaptiveColor].color(self.renderer)) + styler = styler.background(color=bg[AdaptiveColor].color(self.renderer)) elif bg.isa[CompleteColor](): - styler = styler.background(bg[CompleteColor].color(self.renderer)) + styler = styler.background(color=bg[CompleteColor].color(self.renderer)) elif bg.isa[CompleteAdaptiveColor](): - styler = styler.background(bg[CompleteAdaptiveColor].color(self.renderer)) + styler = styler.background(color=bg[CompleteAdaptiveColor].color(self.renderer)) return styler.render(border) @@ -2073,21 +2073,21 @@ struct Style: var bottom_margin = self.get_as_int(MARGIN_BOTTOM_KEY) var left_margin = self.get_as_int(MARGIN_LEFT_KEY) - var styler = mist.new_style(self.renderer.color_profile) + var styler = mist.new_style(self.renderer.color_profile.value) var bgc = self.get_as_color(MARGIN_BACKGROUND_KEY) # TODO: Dealing with variants is verbose :( if bgc.isa[Color](): - styler = styler.background(bgc[Color].color(self.renderer)) + styler = styler.background(color=bgc[Color].color(self.renderer)) elif bgc.isa[ANSIColor](): - styler = styler.background(bgc[ANSIColor].color(self.renderer)) + styler = styler.background(color=bgc[ANSIColor].color(self.renderer)) elif bgc.isa[AdaptiveColor](): - styler = styler.background(bgc[AdaptiveColor].color(self.renderer)) + styler = styler.background(color=bgc[AdaptiveColor].color(self.renderer)) elif bgc.isa[CompleteColor](): - styler = styler.background(bgc[CompleteColor].color(self.renderer)) + styler = styler.background(color=bgc[CompleteColor].color(self.renderer)) elif bgc.isa[CompleteAdaptiveColor](): - styler = styler.background(bgc[CompleteAdaptiveColor].color(self.renderer)) + styler = styler.background(color=bgc[CompleteAdaptiveColor].color(self.renderer)) # Add left and right margin padded_text = pad_left(padded_text, left_margin, styler) @@ -2127,7 +2127,7 @@ struct Style: if i != len(texts) - 1: input_text += " " - var term_style = mist.new_style(self.renderer.color_profile) + var term_style = mist.new_style(self.renderer.color_profile.value) var term_style_space = term_style var term_style_whitespace = term_style @@ -2190,75 +2190,75 @@ struct Style: # TODO: Again super verbose and repetitive bc of Variant if fg.isa[Color](): var terminal_color = fg[Color].color(self.renderer) - term_style = term_style.foreground(terminal_color) + term_style = term_style.foreground(color=terminal_color) if use_space_styler: - term_style_space = term_style_space.foreground(terminal_color) + term_style_space = term_style_space.foreground(color=terminal_color) if use_whitespace_styler: - term_style_whitespace = term_style_whitespace.foreground(terminal_color) + term_style_whitespace = term_style_whitespace.foreground(color=terminal_color) elif fg.isa[ANSIColor](): var terminal_color = fg[ANSIColor].color(self.renderer) - term_style = term_style.foreground(terminal_color) + term_style = term_style.foreground(color=terminal_color) if use_space_styler: - term_style_space = term_style_space.foreground(terminal_color) + term_style_space = term_style_space.foreground(color=terminal_color) if use_whitespace_styler: - term_style_whitespace = term_style_whitespace.foreground(terminal_color) + term_style_whitespace = term_style_whitespace.foreground(color=terminal_color) elif fg.isa[AdaptiveColor](): var terminal_color = fg[AdaptiveColor].color(self.renderer) - term_style = term_style.foreground(terminal_color) + term_style = term_style.foreground(color=terminal_color) if use_space_styler: - term_style_space = term_style_space.foreground(terminal_color) + term_style_space = term_style_space.foreground(color=terminal_color) if use_whitespace_styler: - term_style_whitespace = term_style_whitespace.foreground(terminal_color) + term_style_whitespace = term_style_whitespace.foreground(color=terminal_color) elif fg.isa[CompleteColor](): var terminal_color = fg[CompleteColor].color(self.renderer) - term_style = term_style.foreground(terminal_color) + term_style = term_style.foreground(color=terminal_color) if use_space_styler: - term_style_space = term_style_space.foreground(terminal_color) + term_style_space = term_style_space.foreground(color=terminal_color) if use_whitespace_styler: - term_style_whitespace = term_style_whitespace.foreground(terminal_color) + term_style_whitespace = term_style_whitespace.foreground(color=terminal_color) elif fg.isa[CompleteAdaptiveColor](): var terminal_color = fg[CompleteAdaptiveColor].color(self.renderer) - term_style = term_style.foreground(terminal_color) + term_style = term_style.foreground(color=terminal_color) if use_space_styler: - term_style_space = term_style_space.foreground(terminal_color) + term_style_space = term_style_space.foreground(color=terminal_color) if use_whitespace_styler: - term_style_whitespace = term_style_whitespace.foreground(terminal_color) + term_style_whitespace = term_style_whitespace.foreground(color=terminal_color) if bg.isa[Color](): var terminal_color = bg[Color].color(self.renderer) - term_style = term_style.background(terminal_color) + term_style = term_style.background(color=terminal_color) if use_space_styler: - term_style_space = term_style_space.background(terminal_color) + term_style_space = term_style_space.background(color=terminal_color) if color_whitespace: - term_style_whitespace = term_style_whitespace.background(terminal_color) + term_style_whitespace = term_style_whitespace.background(color=terminal_color) elif bg.isa[ANSIColor](): var terminal_color = bg[ANSIColor].color(self.renderer) - term_style = term_style.background(terminal_color) + term_style = term_style.background(color=terminal_color) if use_space_styler: - term_style_space = term_style_space.background(terminal_color) + term_style_space = term_style_space.background(color=terminal_color) if color_whitespace: - term_style_whitespace = term_style_whitespace.background(terminal_color) + term_style_whitespace = term_style_whitespace.background(color=terminal_color) elif bg.isa[AdaptiveColor](): var terminal_color = bg[AdaptiveColor].color(self.renderer) - term_style = term_style.background(terminal_color) + term_style = term_style.background(color=terminal_color) if use_space_styler: - term_style_space = term_style_space.background(terminal_color) + term_style_space = term_style_space.background(color=terminal_color) if color_whitespace: - term_style_whitespace = term_style_whitespace.background(terminal_color) + term_style_whitespace = term_style_whitespace.background(color=terminal_color) elif bg.isa[CompleteColor](): var terminal_color = bg[CompleteColor].color(self.renderer) - term_style = term_style.background(terminal_color) + term_style = term_style.background(color=terminal_color) if use_space_styler: - term_style_space = term_style_space.background(terminal_color) + term_style_space = term_style_space.background(color=terminal_color) if color_whitespace: - term_style_whitespace = term_style_whitespace.background(terminal_color) + term_style_whitespace = term_style_whitespace.background(color=terminal_color) elif bg.isa[CompleteAdaptiveColor](): var terminal_color = bg[CompleteAdaptiveColor].color(self.renderer) - term_style = term_style.background(terminal_color) + term_style = term_style.background(color=terminal_color) if use_space_styler: - term_style_space = term_style_space.background(terminal_color) + term_style_space = term_style_space.background(color=terminal_color) if color_whitespace: - term_style_whitespace = term_style_whitespace.background(terminal_color) + term_style_whitespace = term_style_whitespace.background(color=terminal_color) if underline_spaces: term_style = term_style_space.underline() @@ -2298,13 +2298,13 @@ struct Style: # Padding if not inline: if left_padding > 0: - var style = mist.new_style(self.renderer.color_profile) + var style = mist.new_style(self.renderer.color_profile.value) if color_whitespace or use_whitespace_styler: style = term_style_whitespace styled_text = pad_left(styled_text, left_padding, style) if right_padding > 0: - var style = mist.new_style(self.renderer.color_profile) + var style = mist.new_style(self.renderer.color_profile.value) if color_whitespace or use_whitespace_styler: style = term_style_whitespace styled_text = pad_right(styled_text, right_padding, style) @@ -2345,7 +2345,7 @@ struct Style: var number_of_lines = len(lines) if not (number_of_lines == 0 and width == 0): - var style = mist.new_style(self.renderer.color_profile) + var style = mist.new_style(self.renderer.color_profile.value) if color_whitespace or use_whitespace_styler: style = term_style_whitespace styled_text = align_text_horizontal(styled_text, horizontal_align, width, style) diff --git a/mog/whitespace.mojo b/mog/whitespace.mojo index b91b8ef..60891c7 100644 --- a/mog/whitespace.mojo +++ b/mog/whitespace.mojo @@ -96,7 +96,7 @@ fn new_whitespace(renderer: Renderer, *opts: WhitespaceOption) -> WhiteSpace: """Creates a new whitespace renderer. The order of the options matters, if you're using WithWhitespaceRenderer, make sure it comes first as other options might depend on it.""" - var w = WhiteSpace(renderer=renderer, style=mist.new_style(renderer.color_profile)) + var w = WhiteSpace(renderer=renderer, style=mist.new_style(renderer.color_profile.value)) for opt in opts: opt(w) @@ -109,7 +109,7 @@ fn new_whitespace(renderer: Renderer, opts: List[WhitespaceOption]) -> WhiteSpac """Creates a new whitespace renderer. The order of the options matters, if you're using WithWhitespaceRenderer, make sure it comes first as other options might depend on it.""" - var w = WhiteSpace(renderer=renderer, style=mist.new_style(renderer.color_profile)) + var w = WhiteSpace(renderer=renderer, style=mist.new_style(renderer.color_profile.value)) for opt in opts: opt[](w) @@ -137,7 +137,7 @@ fn with_whitespace_foreground[terminal_color: AnyTerminalColor]() -> WhitespaceO elif terminal_color.isa[CompleteAdaptiveColor](): color = terminal_color[CompleteAdaptiveColor].color(w.renderer) - w.style = w.style.foreground(color) + w.style = w.style.foreground(color=color) return style_foreground @@ -161,7 +161,7 @@ fn with_whitespace_background[terminal_color: AnyTerminalColor]() -> WhitespaceO elif terminal_color.isa[CompleteAdaptiveColor](): color = terminal_color[CompleteAdaptiveColor].color(w.renderer) - w.style = w.style.background(color) + w.style = w.style.background(color=color) return style_background diff --git a/tests/integration/test_mog.mojo b/tests/integration/test_mog.mojo index 310f207..d0cc351 100644 --- a/tests/integration/test_mog.mojo +++ b/tests/integration/test_mog.mojo @@ -18,10 +18,10 @@ from time import now fn dummy_style_func(row: Int, col: Int) -> Style: var style = mog.new_style().horizontal_alignment(position.center).vertical_alignment(position.center).padding(0, 1) if row == 0: - style = style.foreground(mog.Color("#c9a0dc")) + style = style.foreground(mog.Color(0xC9A0DC)) return style elif row % 2 == 0: - style = style.foreground(mog.Color("#e58006")) + style = style.foreground(mog.Color(0xE58006)) return style else: return style @@ -29,7 +29,7 @@ fn dummy_style_func(row: Int, col: Int) -> Style: fn test_table() raises: var test = MojoTest("Testing table creation with and without headers") - var border_style = mog.new_style().foreground(mog.Color("#39E506")) + var border_style = mog.new_style().foreground(mog.Color(0x39E506)) var table = Table( style_function=default_styles, @@ -74,7 +74,7 @@ fn test_horizontal_joined_paragraphs() raises: var style_build_start = now() var style = mog.new_style().bold().width(50).padding(1, 1, 1, 1).horizontal_alignment(position.center).border( rounded_border() - ).foreground(mog.Color("#c9a0dc")).border_foreground(mog.Color("#39E506")) + ).foreground(mog.Color(0xC9A0DC)).border_foreground(mog.Color(0x39E506)) var style_build_duration = now() - style_build_start print("Style build duration: ", style_build_duration, style_build_duration / 1e9) var start_time = now() @@ -132,7 +132,7 @@ fn test_horizontal_joined_paragraphs() raises: fn test_borderless_paragraph() raises: var borderless_style = mog.new_style().width(50).padding(1, 2).horizontal_alignment(position.center).border( hidden_border() - ).background(mog.Color("#c9a0dc")) + ).background(mog.Color(0xC9A0DC)) print( join_horizontal(