Skip to content

Commit

Permalink
Fix bugs stemming from splitlines, fixed removesuffix bug, and added …
Browse files Browse the repository at this point in the history
…a bunch of tests
  • Loading branch information
thatstoasty committed Dec 21, 2024
1 parent 65c9342 commit 214357a
Show file tree
Hide file tree
Showing 12 changed files with 541 additions and 268 deletions.
5 changes: 2 additions & 3 deletions examples/ansi.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import mog
fn main():
var s = mog.Style().foreground(mog.Color(240))

var table = mog.new_table()
var table = mog.Table.new()
table.width = 50
table = (
table.row("Bubble Tea", s.render("Milky"))
.row("Milk Tea", s.render("Also milky"))
.row("Actual milk", s.render("Milky as well"))
)
print(repr(table.render()))
print(table.render())
print(table)
2 changes: 1 addition & 1 deletion examples/pokemon.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@ fn main():
style_function=style_func,
).rows(data)

print(table.render())
print(table)
2 changes: 1 addition & 1 deletion src/mog/__init__.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ from .border import (
HIDDEN_BORDER,
NO_BORDER,
)
from .table import Table, default_styles, StringData, new_table, Data, Filter
from .table import Table, default_styles, StringData, Data, Filter
from .size import get_height, get_width, get_dimensions
from .color import (
NoColor,
Expand Down
9 changes: 4 additions & 5 deletions src/mog/join.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn join_horizontal(pos: Position, strs: List[String]) -> String:
If you just want to align to the left, right or center you may as well just
use the helper constants Top, Center, and Bottom.
Examples:
#### Examples:
```mojo
import mog
Expand Down Expand Up @@ -152,7 +152,7 @@ fn join_horizontal(pos: Position, strs: List[String]) -> String:
if len(blocks[i]) >= max_height:
continue

var extra_lines = List[String]()
var extra_lines = List[String](capacity=max_height - len(blocks[i]))
extra_lines.resize(max_height - len(blocks[i]), "")

if pos == top:
Expand All @@ -176,11 +176,10 @@ fn join_horizontal(pos: Position, strs: List[String]) -> String:
# remember, all blocks have the same number of members now
for i in range(len(blocks[0])):
for j in range(len(blocks)):
var block = blocks[j]
result.write(block[i])
result.write(blocks[j][i])

# Also make lines the same length by padding with whitespace
var spaces = WHITESPACE * (max_widths[j] - printable_rune_width(block[i]))
var spaces = WHITESPACE * (max_widths[j] - printable_rune_width(blocks[j][i]))
result.write(spaces)

if i < len(blocks[0]) - 1:
Expand Down
55 changes: 40 additions & 15 deletions src/mog/style.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ struct Style:
A new Style with the border rule unset.
"""
var new = self
new._unset_attribute(PropKey.TAB_WIDTH)
new._unset_attribute(PropKey.BORDER_RIGHT)
return new

fn border_foreground(self, *colors: AnyTerminalColor) -> Style:
Expand Down Expand Up @@ -1447,6 +1447,8 @@ struct Style:
A new Style with the padding rule set.
#### Notes:
Padding is applied inside the text area, inside of the border if there is one.
Margin is applied outside the text area, outside of the border if there is one.
* With one argument, the value is applied to all sides.
* With two arguments, the value is applied to the vertical and horizontal
sides, in that order.
Expand Down Expand Up @@ -1499,6 +1501,9 @@ struct Style:
Returns:
A new Style with the padding top rule set.
#### Notes:
Padding is applied inside the text area, inside of the border if there is one.
"""
var new = self
new._set_attribute(PropKey.PADDING_TOP, width)
Expand All @@ -1522,6 +1527,9 @@ struct Style:
Returns:
A new Style with the padding right rule set.
#### Notes:
Padding is applied inside the text area, inside of the border if there is one.
"""
var new = self
new._set_attribute(PropKey.PADDING_RIGHT, width)
Expand All @@ -1545,6 +1553,9 @@ struct Style:
Returns:
A new Style with the padding bottom rule set.
#### Notes:
Padding is applied inside the text area, inside of the border if there is one.
"""
var new = self
new._set_attribute(PropKey.PADDING_BOTTOM, width)
Expand All @@ -1568,6 +1579,9 @@ struct Style:
Returns:
A new Style with the padding left rule set.
#### Notes:
Padding is applied inside the text area, inside of the border if there is one.
"""
var new = self
new._set_attribute(PropKey.PADDING_LEFT, width)
Expand All @@ -1584,26 +1598,25 @@ struct Style:
return new

fn margin(self, *widths: Int) -> Style:
"""Shorthand method for setting padding on all sides at once.
With one argument, the value is applied to all sides.
With two arguments, the value is applied to the vertical and horizontal
sides, in that order.
With three arguments, the value is applied to the top side, the horizontal
sides, and the bottom side, in that order.
With four arguments, the value is applied clockwise starting from the top
side, followed by the right side, then the bottom, and finally the left.
With more than four arguments no padding will be added.
"""Shorthand method for setting margin on all sides at once.
Args:
widths: The padding widths to apply.
Returns:
A new Style with the margin rule set.
#### Notes:
Padding is applied inside the text area, inside of the border if there is one.
Margin is applied outside the text area, outside of the border if there is one.
* With one argument, the value is applied to all sides.
* With two arguments, the value is applied to the vertical and horizontal
sides, in that order.
* With three arguments, the value is applied to the top side, the horizontal
sides, and the bottom side, in that order.
* With four arguments, the value is applied clockwise starting from the top
side, followed by the right side, then the bottom, and finally the left.
* With more than four arguments no margin will be added.
"""
var top = 0
var bottom = 0
Expand Down Expand Up @@ -1648,6 +1661,9 @@ struct Style:
Returns:
A new Style with the margin top rule set.
#### Notes:
Margin is applied uotside the text area, outside of the border if there is one.
"""
var new = self
new._set_attribute(PropKey.MARGIN_TOP, width)
Expand All @@ -1671,6 +1687,9 @@ struct Style:
Returns:
A new Style with the margin right rule set.
#### Notes:
Margin is applied uotside the text area, outside of the border if there is one.
"""
var new = self
new._set_attribute(PropKey.MARGIN_RIGHT, width)
Expand All @@ -1694,6 +1713,9 @@ struct Style:
Returns:
A new Style with the margin bottom rule set.
#### Notes:
Margin is applied uotside the text area, outside of the border if there is one.
"""
var new = self
new._set_attribute(PropKey.MARGIN_BOTTOM, width)
Expand All @@ -1717,6 +1739,9 @@ struct Style:
Returns:
A new Style with the margin left rule set.
#### Notes:
Margin is applied uotside the text area, outside of the border if there is one.
"""
var new = self
new._set_attribute(PropKey.MARGIN_LEFT, width)
Expand Down
2 changes: 1 addition & 1 deletion src/mog/table/__init__.mojo
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .table import Table, default_styles, StyleFunction, new_table
from .table import Table, default_styles, StyleFunction
from .rows import StringData, Data, Filter
67 changes: 44 additions & 23 deletions src/mog/table/rows.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,28 @@ struct StringData(Data):
var _columns: Int
"""The number of columns in the table."""

fn __init__(out self, rows: List[List[String]] = List[List[String]](), columns: Int = 0):
fn __init__(out self, rows: List[List[String]] = List[List[String]]()):
"""Initializes a new StringData instance.
Args:
rows: The rows of the table.
columns: The number of columns in the table.
"""
self._rows = rows
self._columns = columns
self._columns = len(rows)

fn __init__(out self, *rows: List[String]):
"""Initializes a new StringData instance.
Args:
rows: The rows of the table.
"""
var widest = 0
var r = List[List[String]](capacity=len(rows))
for row in rows:
widest = max(widest, len(row[]))
r.append(row[])
self._rows = r
self._columns = widest

# TODO: Can't return ref String because it depends on the origin of a struct attribute
# and Traits do not support variables yet.
Expand Down Expand Up @@ -106,27 +119,46 @@ struct StringData(Data):
"""
self._columns = max(self._columns, len(row))
self._rows.append(row)

fn item(mut self, rows: List[String]) -> Self:
fn append(mut self, *elements: String):
"""Appends the given row to the table.
Args:
rows: The row to append.
elements: The row to append.
"""
self._columns = max(self._columns, len(elements))
var row = List[String](capacity=len(elements))
for element in elements:
row.append(element[])
self._rows.append(row)

fn __add__(self, other: Self) -> Self:
"""Concatenates two StringData instances.
Args:
other: The other StringData instance to concatenate.
Returns:
The updated table.
The concatenated StringData instance.
"""
self._columns = max(self._columns, len(rows))
self._rows.append(rows)
return self
return StringData(self._rows + other._rows, max(self.columns(), other.columns()))

fn __iadd__(mut self, other: Self):
"""Concatenates two StringData instances in place.
Args:
other: The other StringData instance to concatenate.
"""
self._rows.extend(other._rows)
self._columns = max(self.columns(), other.columns())


alias FilterFunction = fn (row: Int) -> Bool
"""Function type that filters rows based on a condition."""


@value
struct Filter[DataType: Data](Data):
struct Filter[DataType: Data, //](Data):
"""Applies a filter function on some data.
Parameters:
Expand All @@ -135,20 +167,9 @@ struct Filter[DataType: Data](Data):

var data: DataType
"""The data of the table."""
var filter_function: FilterFunction
var filter: FilterFunction
"""The filter function to apply."""

fn filter(self, data: Int) -> Bool:
"""Applies the given filter function to the data.
Args:
data: The data to filter.
Returns:
The filtered data.
"""
return self.filter_function(data)

fn __getitem__(self, row: Int, column: Int) -> String:
"""Returns the contents of the cell at the given index.
Expand Down
Loading

0 comments on commit 214357a

Please sign in to comment.