Skip to content

Commit

Permalink
Feature/try speedup (#12)
Browse files Browse the repository at this point in the history
* set sb capacity

* update style

* update mog

* switch to using new_style

* switch to using new_style

* cleanup imports

* cleanup imports
  • Loading branch information
thatstoasty authored Jul 2, 2024
1 parent 884b3d4 commit 8b85673
Show file tree
Hide file tree
Showing 43 changed files with 2,654 additions and 1,570 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Run Tests

on: ["push"]
on:
pull_request:

jobs:
test:
Expand Down
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ Users familiar with CSS will feel at home with Mog.

```mojo
from mog import Style
import mog
var style = Style.new() \
var style = mog.new_style() \
.bold(True) \
.foreground(mog.Color("#FAFAFA")) \
.background(mog.Color("#7D56F4")) \
Expand Down Expand Up @@ -110,7 +110,7 @@ mog.CompleteAdaptiveColor(
Mog supports the usual ANSI text formatting options:

```mojo
var style = Style.new() \
var style = mog.new_style() \
.bold(True) \
.italic(True) \
.faint(True) \
Expand All @@ -126,14 +126,14 @@ Mog also supports rules for block-level formatting:

```mojo
# Padding
var style = Style.new() \
var style = mog.new_style() \
.padding_top(2) \
.padding_right(4) \
.padding_bottom(2) \
.padding_left(4)
# Margins
var style = Style.new() \
var style = mog.new_style() \
.margin_top(2) \
.margin_right(4) \
.margin_bottom(2) \
Expand All @@ -145,25 +145,25 @@ format as CSS:

```mojo
# 2 cells on all sides
Style.new().padding(2)
mog.new_style().padding(2)
# 2 cells on the top and bottom, 4 cells on the left and right
Style.new().margin(2, 4)
mog.new_style().margin(2, 4)
# 1 cell on the top, 4 cells on the sides, 2 cells on the bottom
Style.new().padding(1, 4, 2)
mog.new_style().padding(1, 4, 2)
# Clockwise, starting from the top: 2 cells on the top, 4 on the right, 3 on
# the bottom, and 1 on the left
Style.new().margin(2, 4, 3, 1)
mog.new_style().margin(2, 4, 3, 1)
```

## Aligning Text

You can align paragraphs of text to the left, right, or center.

```mojo
var style = Style.new() \
var style = mog.new_style() \
.width(24) \
.align(position.left) \ # align it left
.align(position.right) \ # no wait, align it right
Expand All @@ -175,7 +175,7 @@ var style = Style.new() \
Setting a minimum width and height is simple and straightforward.

```mojo
var style = Style.new() \
var style = mog.new_style() \
.set_string("What’s for lunch?") \
.width(24) \
.height(32) \
Expand All @@ -188,12 +188,12 @@ Adding borders is easy:

```mojo
# Add a purple, rectangular border
var style = Style.new() \
var style = mog.new_style() \
.border(normal_border()) \
.border_foreground(mog.Color("63"))
# Set a rounded, yellow-on-purple border to the top and left
var another_style = Style.new() \
var another_style = mog.new_style() \
.border(rounded_border()) \
.border_foreground(mog.Color("228")) \
.border_background(mog.Color("63")) \
Expand All @@ -218,19 +218,19 @@ pattern to the margin and padding shorthand functions.

```mojo
# Add a thick border to the top and bottom
Style.new().border(thick_border(), True, False)
mog.new_style().border(thick_border(), True, False)
# Add a double border to the top and left sides. Rules are set clockwise
# from top.
Style.new().border(double_border(), True, False, False, True)
mog.new_style().border(double_border(), True, False, False, True)
```

## Copying Styles

Just use `copy()`:

```mojo
var style = Style.new().foreground(mog.Color("219"))
var style = mog.new_style().foreground(mog.Color("219"))
var wild_style = style.copy().blink(True)
```
Expand All @@ -244,7 +244,7 @@ styles.
All rules can be unset:

```mojo
var style = Style.new() \
var style = mog.new_style() \
.bold(True) \ # make it bold
.unset_bold() \ # jk don't make it bold
.background(mog.Color("227")) \ # yellow background
Expand Down Expand Up @@ -278,7 +278,7 @@ tabs to 4 spaces at render time. This behavior can be changed on a per-style
basis, however:

```mojo
style = Style.new() # tabs will render as 4 spaces, the default
style = mog.new_style() # tabs will render as 4 spaces, the default
style = style.tab_width(2) # render tabs as 2 spaces
style = style.tab_width(0) # remove tabs entirely
style = style.tab_width(mog.NO_TAB_CONVERSION) # leave tabs intact
Expand All @@ -289,7 +289,7 @@ style = style.tab_width(mog.NO_TAB_CONVERSION) # leave tabs intact
Generally, you just call the `render(string)` method on a `mog.Style`:

```mojo
style = Style.new().bold(True).set_string("Hello,")
style = mog.new_style().bold(True).set_string("Hello,")
print(style.render("kitty.")) # Hello, kitty.
print(style.render("puppy.")) # Hello, puppy.
print(style.render("my", "puppy.")) # Hello, my puppy.
Expand All @@ -298,7 +298,7 @@ print(style.render("my", "puppy.")) # Hello, my puppy.
But you could also use the Stringer interface:

```mojo
var style = Style.new().set_string("你好,猫咪。").bold(True)
var style = mog.new_style().set_string("你好,猫咪。").bold(True)
print(style) # 你好,猫咪。
```

Expand Down Expand Up @@ -350,7 +350,7 @@ your layouts.

```mojo
# render a block of text.
var style = Style.new() \
var style = mog.new_style() \
.width(40) \
.padding(2)
var block string = style.render(some_long_string)
Expand Down Expand Up @@ -407,7 +407,7 @@ Use the table package to style and render the table.
```mojo
t = table.new_table().
.border(normal_border()) \
.border_style(Style.new().foreground(mog.Color("99"))) \
.border_style(mog.new_style().foreground(mog.Color("99"))) \
.headers("LANGUAGE", "FORMAL", "INFORMAL") \
.rows(rows)
Expand Down
16 changes: 16 additions & 0 deletions benchmarks/basic_styling.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ fn basic_styling():
keep(output)


var file_style = (
mog.new_style()
.bold(True)
.foreground(mog.Color("#FAFAFA"))
.background(mog.Color("#7D56F4"))
.padding_top(2)
.padding_left(4)
.width(22)
)


fn basic_comptime_styling():
var output = file_style.render("Hello, kitty")
keep(output)


fn basic_styling_big_file():
var content: String = ""
try:
Expand Down
39 changes: 21 additions & 18 deletions benchmarks/layout.mojo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from benchmark.compiler import keep
from external.gojo.strings import StringBuilder
from external.weave.ansi import printable_rune_width
import mog
from mog.join import join_vertical, join_horizontal
from mog.border import hidden_border, normal_border, rounded_border, Border
from mog.style import Style
Expand Down Expand Up @@ -44,7 +45,7 @@ fn build_tabs() -> String:
bottom_right="",
)

var tab_style = Style.new().border(tab_border).border_foreground(highlight).padding(0, 1)
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)

Expand All @@ -63,11 +64,11 @@ fn build_tabs() -> String:


fn build_description() -> String:
var divider = Style.new().padding(0, 1).foreground(subtle).render("")
var divider = mog.new_style().padding(0, 1).foreground(subtle).render("")

var url = Style.new().foreground(special)
var desc_style = Style.new().margin_top(1)
var info_style = Style.new().border(normal_border(), True, False, False, False).border_foreground(subtle)
var url = mog.new_style().foreground(special)
var desc_style = mog.new_style().margin_top(1)
var info_style = mog.new_style().border(normal_border(), True, False, False, False).border_foreground(subtle)

return join_vertical(
position.left,
Expand All @@ -77,11 +78,11 @@ fn build_description() -> String:


fn build_dialog_box() -> String:
var dialog_box_style = Style.new().alignment(position.center).border(rounded_border()).border_foreground(
var dialog_box_style = mog.new_style().alignment(position.center).border(rounded_border()).border_foreground(
mog.Color("#874BFD")
).padding(1, 0)

var button_style = Style.new().foreground(mog.Color("#FFF7DB")).background(mog.Color("#888B7E")).padding(
var button_style = mog.new_style().foreground(mog.Color("#FFF7DB")).background(mog.Color("#888B7E")).padding(
0, 3
).margin_top(1)

Expand All @@ -92,7 +93,9 @@ fn build_dialog_box() -> String:
var ok_button = active_button_style.render("Yes")
var cancel_button = button_style.render("Maybe")

var question = Style.new().width(50).alignment(position.center).render("Are you sure you want to eat marmalade?")
var question = mog.new_style().width(50).alignment(position.center).render(
"Are you sure you want to eat marmalade?"
)

var buttons = join_horizontal(position.top, ok_button, cancel_button)
var ui = join_vertical(position.center, question, buttons)
Expand All @@ -110,19 +113,19 @@ fn build_dialog_box() -> String:


fn build_lists() -> String:
var list_style = Style.new().border(normal_border(), False, True, False, False).border_foreground(
var list_style = mog.new_style().border(normal_border(), False, True, False, False).border_foreground(
subtle
).margin_right(2).height(8).width(column_width + 1)

var list_header = Style.new().border(normal_border(), False, False, True, False).border_foreground(
var list_header = mog.new_style().border(normal_border(), False, False, True, False).border_foreground(
subtle
).margin_right(2)

var list_item = Style.new().padding_left(2)
var list_item = mog.new_style().padding_left(2)

var check_mark = Style.new().foreground(special).padding_right(1).render("")
var check_mark = mog.new_style().foreground(special).padding_right(1).render("")

var list_done = Style.new().crossout().foreground(mog.AdaptiveColor(light="#969B86", dark="#696969"))
var list_done = mog.new_style().crossout().foreground(mog.AdaptiveColor(light="#969B86", dark="#696969"))

var lists = join_horizontal(
position.top,
Expand Down Expand Up @@ -169,7 +172,7 @@ fn build_lists() -> String:


fn build_history() -> String:
var history_style = Style.new().height(20).width(column_width).padding(1, 2).margin(1, 3, 0, 0).alignment(
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)

Expand All @@ -186,11 +189,11 @@ fn build_history() -> String:


fn build_status_bar() -> String:
var status_nugget_style = Style.new().foreground(mog.Color("#FFFDF5")).padding(0, 1)
var status_nugget_style = mog.new_style().foreground(mog.Color("#FFFDF5")).padding(0, 1)

var status_bar_style = Style.new().foreground(mog.Color("#C1C6B2")).background(mog.Color("#353533"))
var status_bar_style = mog.new_style().foreground(mog.Color("#C1C6B2")).background(mog.Color("#353533"))

var status_style = Style.new().foreground(mog.Color("#FFFDF5")).background(mog.Color("#FF5F87")).padding(0, 1)
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(
Expand Down Expand Up @@ -221,7 +224,7 @@ fn build_status_bar() -> String:
fn render_layout():
# The page style
var builder = StringBuilder()
var doc_style = Style.new().padding(1, 2, 1, 2).border(rounded_border()).border_foreground(subtle)
var doc_style = mog.new_style().padding(1, 2, 1, 2).border(rounded_border()).border_foreground(subtle)

# Tabs.
_ = builder.write_string(build_tabs())
Expand Down
47 changes: 35 additions & 12 deletions benchmarks/run.mojo
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import benchmark
from benchmarks.layout import render_layout
from benchmarks.basic_styling import basic_styling, basic_styling_big_file
from benchmarks.basic_styling import (
basic_styling,
basic_styling_big_file,
basic_comptime_styling,
)
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"))


fn table_styling(row: Int, col: Int) -> mog.Style:
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"))
if row == 0:
return header_style
else:
Expand All @@ -16,7 +22,14 @@ fn table_styling(row: Int, col: Int) -> mog.Style:
fn main():
var results = mog.new_table()
results.style_function = table_styling
results.set_headers("Name", "Mean (ms)", "Total (ms)", "Iterations", "Warmup Total", "Warmup Iterations")
results.set_headers(
"Name",
"Mean (ms)",
"Total (ms)",
"Iterations",
"Warmup Total",
"Warmup Iterations",
)

var report = benchmark.run[render_layout](max_iters=10)
results.row(
Expand All @@ -28,7 +41,7 @@ fn main():
str(report.warmup_iters),
)

var bs_report = benchmark.run[basic_styling](max_iters=10)
var bs_report = benchmark.run[basic_styling](max_iters=50)
results.row(
"Basic styling",
str(bs_report.mean(benchmark.Unit.ms)),
Expand All @@ -38,14 +51,24 @@ fn main():
str(bs_report.warmup_iters),
)

var bs_big_report = benchmark.run[basic_styling_big_file](max_iters=10)
var bcs_report = benchmark.run[basic_comptime_styling](max_iters=50)
results.row(
"Large file test",
str(bs_big_report.mean(benchmark.Unit.ms)),
str(bs_big_report.duration(benchmark.Unit.ms)),
str(bs_big_report.iters()),
str(bs_big_report.warmup_duration / 1e6),
str(bs_big_report.warmup_iters),
"Basic comptime styling",
str(bcs_report.mean(benchmark.Unit.ms)),
str(bcs_report.duration(benchmark.Unit.ms)),
str(bcs_report.iters()),
str(bcs_report.warmup_duration / 1e6),
str(bcs_report.warmup_iters),
)

# var bs_big_report = benchmark.run[basic_styling_big_file](max_iters=10)
# results.row(
# "Large file test",
# str(bs_big_report.mean(benchmark.Unit.ms)),
# str(bs_big_report.duration(benchmark.Unit.ms)),
# str(bs_big_report.iters()),
# str(bs_big_report.warmup_duration / 1e6),
# str(bs_big_report.warmup_iters),
# )

print(results.render())
Loading

0 comments on commit 8b85673

Please sign in to comment.