Skip to content

Commit

Permalink
switch to using new_style
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Jul 2, 2024
1 parent e002c5e commit 602672f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 50 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
4 changes: 2 additions & 2 deletions examples/readme/basic.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import mog


var style = (
Style.new()
mog.new_style()
.bold(True)
.foreground(mog.Color("#FAFAFA"))
.background(mog.Color("#7D56F4"))
Expand All @@ -15,7 +15,7 @@ var style = (

fn main() raises:
# var style = (
# Style.new()
# mog.new_style()
# .bold(True)
# .foreground(mog.Color("#FAFAFA"))
# .background(mog.Color("#7D56F4"))
Expand Down
38 changes: 20 additions & 18 deletions examples/readme/layout.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,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 @@ -62,11 +62,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 @@ -76,11 +76,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 @@ -91,7 +91,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 @@ -109,19 +111,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 @@ -168,7 +170,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 @@ -185,11 +187,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 @@ -220,7 +222,7 @@ fn build_status_bar() -> String:
fn main():
# 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
2 changes: 1 addition & 1 deletion examples/table/ansi.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import mog


fn main() raises:
var s = Style.new().foreground(mog.Color("240"))
var s = mog.new_style().foreground(mog.Color("240"))

var t = new_table()
t.width = 50
Expand Down
4 changes: 2 additions & 2 deletions mog/style.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ struct Style:
Example:
var: String = "..."
var user_style = Style.new().inline(True)
var user_style = mog.new_style().inline(True)
print(user_style.render(user_input))
Args:
Expand Down Expand Up @@ -655,7 +655,7 @@ struct Style:
Example:
var: String = "..."
var user_style = Style.new().max_width(16)
var user_style = mog.new_style().max_width(16)
print(user_style.render(user_input))
Args:
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/test_mog.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ from time import now


fn dummy_style_func(row: Int, col: Int) -> Style:
var style = Style.new().horizontal_alignment(position.center).vertical_alignment(position.center).padding(0, 1)
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"))
return style
Expand All @@ -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 = Style.new().foreground(mog.Color("#39E506"))
var border_style = mog.new_style().foreground(mog.Color("#39E506"))

var table = Table(
style_function=default_styles,
Expand Down Expand Up @@ -72,7 +72,7 @@ fn test_table() raises:
fn test_horizontal_joined_paragraphs() raises:
var test = MojoTest("Testing Style rendering")
var style_build_start = now()
var style = Style.new().bold().width(50).padding(1, 1, 1, 1).horizontal_alignment(position.center).border(
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"))
var style_build_duration = now() - style_build_start
Expand Down Expand Up @@ -130,7 +130,7 @@ fn test_horizontal_joined_paragraphs() raises:


fn test_borderless_paragraph() raises:
var borderless_style = Style.new().width(50).padding(1, 2).horizontal_alignment(position.center).border(
var borderless_style = mog.new_style().width(50).padding(1, 2).horizontal_alignment(position.center).border(
hidden_border()
).background(mog.Color("#c9a0dc"))

Expand Down

0 comments on commit 602672f

Please sign in to comment.