From d0cb48c11ae09109c81a43a01b9ba2dfdabeed60 Mon Sep 17 00:00:00 2001 From: Mikhail Tavarez Date: Tue, 2 Jul 2024 15:20:37 -0500 Subject: [PATCH] rm copy --- .devcontainer/Dockerfile | 76 +++++++++++++++++++++++++++++++++ .devcontainer/devcontainer.json | 18 ++++++++ README.md | 2 +- benchmarks/layout.mojo | 30 ++++++------- benchmarks/run.mojo | 2 +- examples/readme/basic.mojo | 2 +- examples/readme/layout.mojo | 30 ++++++------- examples/table/pokemon.mojo | 20 ++++----- mog/color.mojo | 2 +- mog/style.mojo | 54 +++++------------------ 10 files changed, 142 insertions(+), 94 deletions(-) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/devcontainer.json diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..99c9d97 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,76 @@ +# ===----------------------------------------------------------------------=== # +# Copyright (c) 2023, Modular Inc. All rights reserved. +# +# Licensed under the Apache License v2.0 with LLVM Exceptions: +# https://llvm.org/LICENSE.txt +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# The Dockerfile is provided as reference. Please review the redistribution +# terms of the Mojo license in Section 1 (https://www.modular.com/legal/mojo) +# prior to distributing pre-built container images. +# ===----------------------------------------------------------------------=== # + +# Example command line: +# Use no-cache to force docker to rebuild layers of the image by downloading the SDK from the repos +# ./build-image.sh --auth-key +# + +FROM ubuntu:22.04 + +ARG DEFAULT_TZ=America/Los_Angeles +ENV DEFAULT_TZ=$DEFAULT_TZ + +RUN apt-get update \ + && DEBIAN_FRONTEND=noninteractive TZ=$DEFAULT_TZ apt-get install -y \ + tzdata \ + vim \ + nano \ + sudo \ + curl \ + wget \ + python3-pip \ + git && \ + rm -rf /var/lib/apt/lists/* + + +# Download the latest version of minicoda py3.8 for linux x86/x64. +# RUN curl -fsSL https://repo.anaconda.com/miniconda/$( wget -O - https://repo.anaconda.com/miniconda/ 2>/dev/null | grep -o 'Miniconda3-py38_[^"]*-Linux-x86_64.sh' | head -n 1) > /tmp/miniconda.sh \ +# && chmod +x /tmp/miniconda.sh \ +# && /tmp/miniconda.sh -b -p /opt/conda + +# RUN mkdir -p ~/miniconda3 \ +# && wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh \ +# && bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3 \ +# && rm -rf ~/miniconda3/miniconda.sh + +# ENV PATH=~/miniconda3/bin/conda:$PATH +# RUN conda init + +RUN pip install \ + pytest \ + git+https://github.com/guidorice/mojo-pytest.git + +# RUN pip install \ +# jupyterlab \ +# ipykernel \ +# matplotlib \ +# ipywidgets + +RUN curl https://get.modular.com | sh - +# RUN modular auth examples && modular install nightly/mojo +RUN modular auth examples && modular install mojo + +ARG MODULAR_HOME="/root/.modular" +ENV MODULAR_HOME=$MODULAR_HOME +# ENV PATH="$PATH:$MODULAR_HOME/pkg/packages.modular.com_nightly_mojo/bin" +ENV PATH="$PATH:$MODULAR_HOME/pkg/packages.modular.com_mojo/bin" + +# Change permissions to allow for Apptainer/Singularity containers +RUN chmod -R a+rwX /root + +# CMD ["jupyter", "lab", "--ip='*'", "--NotebookApp.token=''", "--NotebookApp.password=''","--allow-root"] diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..4ef9288 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,18 @@ +{ + "name": "Mojo", + "build": { + "dockerfile": "Dockerfile" + }, + "customizations": { + // Configure properties specific to VS Code. + "vscode": { + // Set *default* container specific settings.json values on container create. + "settings": {}, + "extensions": [ + "modular-mojotools.vscode-mojo", + // "ms-toolsai.jupyter" + // "modular-mojotools.vscode-mojo-nightly" + ] + } + } +} diff --git a/README.md b/README.md index 7dbc532..83244ed 100644 --- a/README.md +++ b/README.md @@ -232,7 +232,7 @@ Just use `copy()`: ```mojo var style = mog.new_style().foreground(mog.Color("219")) -var wild_style = style.copy().blink(True) +var wild_style = style.blink(True) ``` `copy()` performs a copy on the underlying data structure ensuring that you get diff --git a/benchmarks/layout.mojo b/benchmarks/layout.mojo index 0623664..14c090f 100644 --- a/benchmarks/layout.mojo +++ b/benchmarks/layout.mojo @@ -47,9 +47,9 @@ fn build_tabs() -> String: var tab_style = mog.new_style().border(tab_border).border_foreground(highlight).padding(0, 1) - var active_tab = tab_style.copy().border(active_tab_border, True) + var active_tab = tab_style.border(active_tab_border, True) - var tab_gap = tab_style.copy().border_top(False).border_left(False).border_right(False) + var tab_gap = tab_style.border_top(False).border_left(False).border_right(False) var row = join_horizontal( position.top, @@ -86,7 +86,7 @@ fn build_dialog_box() -> String: 0, 3 ).margin_top(1) - var active_button_style = button_style.copy().foreground(mog.Color("#FFF7DB")).background( + var active_button_style = button_style.foreground(mog.Color("#FFF7DB")).background( mog.Color("#F25D94") ).margin_right(2).underline() @@ -140,9 +140,7 @@ fn build_lists() -> String: list_item.render("Pomelo"), ), ), - list_style.copy() - .width(column_width) - .render( + list_style.width(column_width).render( join_vertical( position.left, list_header.render("Actual Lip Gloss Vendors"), @@ -153,9 +151,7 @@ fn build_lists() -> String: check_mark + list_done.render("Milk"), ), ), - list_style.copy() - .width(column_width - 1) - .render( + list_style.width(column_width - 1).render( join_vertical( position.left, list_header.render("Programming Languages"), @@ -182,9 +178,9 @@ fn build_history() -> String: return join_horizontal( position.top, - history_style.copy().alignment(position.right).render(history_a), - history_style.copy().alignment(position.center).render(history_b), - history_style.copy().margin_right(0).render(history_c), + history_style.alignment(position.right).render(history_a), + history_style.alignment(position.center).render(history_b), + history_style.margin_right(0).render(history_c), ) @@ -196,17 +192,15 @@ fn build_status_bar() -> String: var status_style = mog.new_style().foreground(mog.Color("#FFFDF5")).background(mog.Color("#FF5F87")).padding(0, 1) # .margin_right(1) - var encoding_style = status_nugget_style.copy().background(mog.Color("#A550DF")).horizontal_alignment( - position.right - ) + var encoding_style = status_nugget_style.background(mog.Color("#A550DF")).horizontal_alignment(position.right) - var status_text_style = status_bar_style.copy().padding_left(1) - var fish_cake_style = status_nugget_style.copy().background(mog.Color("#6124DF")) + var status_text_style = status_bar_style.padding_left(1) + var fish_cake_style = status_nugget_style.background(mog.Color("#6124DF")) var status_key = status_style.render("STATUS") var encoding = encoding_style.render("UTF-8") var fish_cake = fish_cake_style.render("Fish Cake") - var status_val = status_text_style.copy().width( + var status_val = status_text_style.width( width - get_width(status_key) - get_width(encoding) - get_width(fish_cake) ).render("Ravishing") diff --git a/benchmarks/run.mojo b/benchmarks/run.mojo index 6cde81d..ed196e0 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.copy().foreground(mog.Color("#39E506")) +var header_style = style.foreground(mog.Color("#39E506")) fn table_styling(row: Int, col: Int) -> mog.Style: diff --git a/examples/readme/basic.mojo b/examples/readme/basic.mojo index 084808c..35d8772 100644 --- a/examples/readme/basic.mojo +++ b/examples/readme/basic.mojo @@ -2,7 +2,7 @@ from mog import Style import mog -fn main() raises: +fn main(): var style = ( mog.new_style() .bold(True) diff --git a/examples/readme/layout.mojo b/examples/readme/layout.mojo index 2ae3926..dfd3d8e 100644 --- a/examples/readme/layout.mojo +++ b/examples/readme/layout.mojo @@ -45,9 +45,9 @@ fn build_tabs() -> String: var tab_style = mog.new_style().border(tab_border).border_foreground(highlight).padding(0, 1) - var active_tab = tab_style.copy().border(active_tab_border, True) + var active_tab = tab_style.border(active_tab_border, True) - var tab_gap = tab_style.copy().border_top(False).border_left(False).border_right(False) + var tab_gap = tab_style.border_top(False).border_left(False).border_right(False) var row = join_horizontal( position.top, @@ -84,7 +84,7 @@ fn build_dialog_box() -> String: 0, 3 ).margin_top(1) - var active_button_style = button_style.copy().foreground(mog.Color("#FFF7DB")).background( + var active_button_style = button_style.foreground(mog.Color("#FFF7DB")).background( mog.Color("#F25D94") ).margin_right(2).underline() @@ -138,9 +138,7 @@ fn build_lists() -> String: list_item.render("Pomelo"), ), ), - list_style.copy() - .width(column_width) - .render( + list_style.width(column_width).render( join_vertical( position.left, list_header.render("Actual Lip Gloss Vendors"), @@ -151,9 +149,7 @@ fn build_lists() -> String: check_mark + list_done.render("Milk"), ), ), - list_style.copy() - .width(column_width - 1) - .render( + list_style.width(column_width - 1).render( join_vertical( position.left, list_header.render("Programming Languages"), @@ -180,9 +176,9 @@ fn build_history() -> String: return join_horizontal( position.top, - history_style.copy().alignment(position.right).render(history_a), - history_style.copy().alignment(position.center).render(history_b), - history_style.copy().margin_right(0).render(history_c), + history_style.alignment(position.right).render(history_a), + history_style.alignment(position.center).render(history_b), + history_style.margin_right(0).render(history_c), ) @@ -194,17 +190,15 @@ fn build_status_bar() -> String: var status_style = mog.new_style().foreground(mog.Color("#FFFDF5")).background(mog.Color("#FF5F87")).padding(0, 1) # .margin_right(1) - var encoding_style = status_nugget_style.copy().background(mog.Color("#A550DF")).horizontal_alignment( - position.right - ) + var encoding_style = status_nugget_style.background(mog.Color("#A550DF")).horizontal_alignment(position.right) - var status_text_style = status_bar_style.copy().padding_left(1) - var fish_cake_style = status_nugget_style.copy().background(mog.Color("#6124DF")) + var status_text_style = status_bar_style.padding_left(1) + var fish_cake_style = status_nugget_style.background(mog.Color("#6124DF")) var status_key = status_style.render("STATUS") var encoding = encoding_style.render("UTF-8") var fish_cake = fish_cake_style.render("Fish Cake") - var status_val = status_text_style.copy().width( + var status_val = status_text_style.width( width - get_width(status_key) - get_width(encoding) - get_width(fish_cake) ).render("Ravishing") diff --git a/examples/table/pokemon.mojo b/examples/table/pokemon.mojo index a9b28ee..f80e2ba 100644 --- a/examples/table/pokemon.mojo +++ b/examples/table/pokemon.mojo @@ -78,14 +78,14 @@ # # colors = DIM_TYPE_COLORS # # var color = colors.get(data[row - 1][col], mog.Color("#FFFFFF")) -# # var copy_style = style.copy().foreground(color) +# # var copy_style = style.foreground(color) # # return copy_style # # if is_even: -# # var copy_style = style.copy().foreground(mog.Color("245")) +# # var copy_style = style.foreground(mog.Color("245")) # # return copy_style -# # var copy_style = style.copy().foreground(mog.Color("252")) +# # var copy_style = style.foreground(mog.Color("252")) # # return copy_style # # return style_func @@ -97,11 +97,11 @@ # # .padding_bottom(1) \ # # .padding_left(1) -# # var header_style = style.copy() \ +# # var header_style = style \ # # .foreground(mog.Color("252")) \ # # .bold() -# # var selected_style = style.copy() \ +# # var selected_style = style \ # # .foreground(mog.Color("#01BE85")) \ # # .background(mog.Color("#00432F")) @@ -121,11 +121,11 @@ # .padding_bottom(1) \ # .padding_left(1) -# var header_style = style.copy() \ +# var header_style = style \ # .foreground(mog.Color("252")) \ # .bold() -# var selected_style = style.copy() \ +# var selected_style = style \ # .foreground(mog.Color("#01BE85")) \ # .background(mog.Color("#00432F")) @@ -142,14 +142,14 @@ # colors = DIM_TYPE_COLORS # var color = colors.get(data[row - 1][col], mog.Color("#FFFFFF")) -# var copy_style = style.copy().foreground(color) +# var copy_style = style.foreground(color) # return copy_style # if is_even: -# var copy_style = style.copy().foreground(mog.Color("245")) +# var copy_style = style.foreground(mog.Color("245")) # return copy_style -# var copy_style = style.copy().foreground(mog.Color("252")) +# var copy_style = style.foreground(mog.Color("252")) # return copy_style # var border_style = mog.Style() diff --git a/mog/color.mojo b/mog/color.mojo index 89bbb0d..2baa643 100644 --- a/mog/color.mojo +++ b/mog/color.mojo @@ -28,7 +28,7 @@ struct NoColor(TerminalColor): Example usage: - var style = some_style.copy().background(NoColor()) + var style = some_style.background(NoColor()) """ fn color(self, renderer: Renderer) -> mist.AnyColor: diff --git a/mog/style.mojo b/mog/style.mojo index 06a4b39..32e95a4 100644 --- a/mog/style.mojo +++ b/mog/style.mojo @@ -119,33 +119,6 @@ fn get_lines(text: String) -> Tuple[List[String], Int]: return lines, widest_line -alias TRUTHY_VALUES = List[String]("True", "true", "TRUE", "1") - - -@always_inline -fn to_bool(s: String) -> Bool: - return s in TRUTHY_VALUES - - -fn str_to_float(s: String) raises -> Float64: - try: - var dot_pos = s.find(".") - var int_str = s[0:dot_pos] - var num_str = s[dot_pos + 1 : len(s)] - var numerator = atol(num_str) - var denom_str = String("") - for _ in range(len(num_str)): - denom_str += "0" - var denominator = atol("1" + denom_str) - var frac = numerator / denominator - - # return the number as a Float64 - var result: Float64 = atol(int_str) + frac - return result - except: - raise Error("Failed to convert " + s + " to a float.") - - # Apply left padding. fn pad_left(text: String, n: Int, style: mist.Style) -> String: if n == 0: @@ -317,7 +290,7 @@ struct Style: Returns: A new Style object with the renderer set. """ - var new_style = self.copy() + var new_style = self new_style.renderer = renderer return new_style @@ -330,7 +303,7 @@ struct Style: Returns: A new Style object with the string value set. """ - var new_style = self.copy() + var new_style = self new_style.value = value return new_style @@ -344,7 +317,7 @@ struct Style: Returns: A new Style object with the rule set. """ - var new_style = self.copy() + var new_style = self new_style.rules.put(str(key), value) return new_style @@ -357,7 +330,7 @@ struct Style: Returns: A new Style object with the rule unset. """ - var new_style = self.copy() + var new_style = self new_style.rules.delete(str(key)) return new_style @@ -745,7 +718,7 @@ struct Style: Returns: A new Style object with the alignment rules set. """ - var new_style = self.copy() + var new_style = self if len(align) > 0: new_style.rules.put(str(HORIZONTAL_ALIGNMENT_KEY), align[0]) @@ -811,7 +784,7 @@ struct Style: Returns: A new Style object with the border rule set. """ - var new_style = self.copy() + var new_style = self new_style.rules.put(str(BORDER_STYLE_KEY), border) if top: @@ -914,7 +887,7 @@ struct Style: var bottom: AnyTerminalColor = NoColor() var left: AnyTerminalColor = NoColor() var right: AnyTerminalColor = NoColor() - var new_style = self.copy() + var new_style = self var widths_specified = len(colors) if widths_specified == 1: top = colors[0] @@ -1035,7 +1008,7 @@ struct Style: var bottom: AnyTerminalColor = NoColor() var left: AnyTerminalColor = NoColor() var right: AnyTerminalColor = NoColor() - var new_style = self.copy() + var new_style = self var widths_specified = len(colors) if widths_specified == 1: top = colors[0] @@ -1166,7 +1139,7 @@ struct Style: var bottom = 0 var left = 0 var right = 0 - var new_style = self.copy() + var new_style = self var widths_specified = len(widths) if widths_specified == 1: top = widths[0] @@ -1296,7 +1269,7 @@ struct Style: var bottom = 0 var left = 0 var right = 0 - var new_style = self.copy() + var new_style = self var widths_specified = len(widths) if widths_specified == 1: top = widths[0] @@ -1919,10 +1892,3 @@ struct Style: styled_text = self.apply_margins(styled_text, inline) return styled_text - - fn copy(self) -> Self: - return Self( - renderer=self.renderer, - rules=self.rules, - value=self.value, - )