Skip to content

Commit

Permalink
fix zero length cell bug
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Dec 3, 2024
1 parent e5b1f33 commit a298625
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 171 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ repos:
language: system
files: '\.(mojo|🔥)$'
stages: [commit]
- id: mojo-docs
name: mojo-docs
entry: python3 ./scripts/check-docstrings.py
language: system
pass_filenames: false
stages: [commit]
2 changes: 1 addition & 1 deletion examples/layout.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ fn build_status_bar() -> String:

fn main():
# The page style
var builder = String()
var builder = String(capacity=4096)
var doc_style = mog.Style().padding(1, 2, 1, 2)

# Tabs.
Expand Down
5 changes: 2 additions & 3 deletions src/mog/align.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ fn align_text_horizontal(text: String, pos: position.Position, width: Int, style
Returns:
The aligned text.
"""
# TODO: Seems to be a bug with padding empty cells to the length needed.
lines, widest_line = get_lines(text)

var aligned_text = String(capacity=len(text))
for i in range(len(lines)):
var line = lines[i]
var line_width = printable_rune_width(lines[i])

var line_width = printable_rune_width(line)
var short_amount = widest_line - line_width # difference from the widest line
short_amount += max(0, width - (short_amount + line_width)) # difference from the total width, if set
if short_amount > 0:
Expand Down
3 changes: 3 additions & 0 deletions src/mog/extensions.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ fn get_lines(text: String) -> Tuple[List[String], Int]:
Returns:
A tuple containing the lines and the width of the widest line.
"""
if text == "":
return List[String](""), 0

var lines = text.splitlines()
var widest_line = 0
for line in lines:
Expand Down
2 changes: 1 addition & 1 deletion src/mog/join.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn join_horizontal(pos: Position, strs: List[String]) -> String:

# Max line widths for the above text blocks
var max_widths = List[Int](capacity=len(strs))
var max_height: Int = 0
var max_height = 0

# Break text blocks into lines and get max widths for each text block
for s in strs:
Expand Down
2 changes: 1 addition & 1 deletion src/mog/size.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn get_width(text: String) -> Int:
The width of the string in cells.
"""
var width = 0
for line in text.splitlines():
for line in text.as_string_slice().splitlines():
var w = ansi.printable_rune_width(line[])
if w > width:
width = w
Expand Down
9 changes: 3 additions & 6 deletions src/mog/style.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn pad(text: String, n: Int, style: mist.Style) -> String:

var spaces = style.render(WHITESPACE * abs(n))
var result = String(capacity=int(len(text) * 1.5))
var lines = text.splitlines()
var lines = text.as_string_slice().splitlines()
for i in range(len(lines)):
if n > 0:
result.write(lines[i], spaces)
Expand Down Expand Up @@ -2320,7 +2320,6 @@ struct Style:
if use_space_styler:
# Look for spaces and apply a different styler
for char in lines[i]:
# for j in range(printable_rune_width(lines[i])):
if char == " ":
result.write(term_style_space.render(char))
else:
Expand Down Expand Up @@ -2373,16 +2372,14 @@ struct Style:
# if transform:
# return transform(result)

# Apply border at the end
lines = result.splitlines()

var number_of_lines = len(lines)
if not (number_of_lines == 0 and width == 0):
if width != 0:
var style = mist.Style(self.renderer.color_profile.value)
if color_whitespace or use_whitespace_styler:
style = term_style_whitespace
result = align_text_horizontal(result, horizontal_align, width, style)

# Apply border at the end
if not inline:
result = self.apply_border(result)
result = self.apply_margins(result, inline)
Expand Down
18 changes: 10 additions & 8 deletions src/mog/table/table.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ struct Table:
return new

fn rows(self, rows: List[List[String]]) -> Table:
"""Returns the style for a cell based on it's position (row, column).
"""Appends the data from `rows` to the table.
Args:
rows: The rows to add to the table.
Expand All @@ -227,8 +227,8 @@ struct Table:
The updated table.
"""
var new = self
for i in range(len(rows)):
new.data.append(rows[i])
for row in rows:
new.data.append(row[])
return new

fn row(self, *row: String) -> Table:
Expand Down Expand Up @@ -602,12 +602,14 @@ struct Table:
if self.border_left:
cells.append(left)

var c: Int = 0
var c = 0
while c < self.data.columns():
var cell = self.data.at(index, c)
var style = self.style(index + 1, c).height(height).max_height(height).width(self.widths[c]).max_width(
self.widths[c]
)
var style = self.style(index + 1, c).
height(height).
max_height(height).
width(self.widths[c]).
max_width(self.widths[c])

cells.append(style.render(truncate(cell, self.widths[c] * height, "")))

Expand All @@ -628,7 +630,7 @@ struct Table:

if self.border_row and index < self.data.rows() - 1:
result.write(self.border_style.render(self.border.middle_left))
var i: Int = 0
var i = 0
while i < len(self.widths):
result.write(self.border_style.render(self.border.middle * self.widths[i]))
if i < len(self.widths) - 1 and self.border_column:
Expand Down
8 changes: 4 additions & 4 deletions test/test_join.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import mog.position


def test_horizontal_join():
var a = "Hello World!\nThis is an example."
var b = "I could be more creative.\nBut, I'm out of ideas."
alias a = "Hello World!\nThis is an example."
alias b = "I could be more creative.\nBut, I'm out of ideas."

# Test horizontally joining three paragraphs along their bottom edges
var bottom_aligned = join_horizontal(position.bottom, a, b)
Expand Down Expand Up @@ -34,8 +34,8 @@ def test_horizontal_join():


def test_vertical_join():
var a = "Hello World!\nThis is an example."
var b = "I could be more creative.\nBut, I'm out of ideas."
alias a = "Hello World!\nThis is an example."
alias b = "I could be more creative.\nBut, I'm out of ideas."

# Test vertically joining two paragraphs along their right border
var right_aligned = join_vertical(position.right, a, b)
Expand Down
147 changes: 0 additions & 147 deletions test/test_mog.mojo

This file was deleted.

0 comments on commit a298625

Please sign in to comment.