Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions ext/lipgloss/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,49 @@ static VALUE table_initialize(VALUE self) {
return self;
}

static VALUE table_cells_to_s(VALUE cells) {
Check_Type(cells, T_ARRAY);

long length = RARRAY_LEN(cells);
VALUE result = rb_ary_new_capa(length);

for (long index = 0; index < length; index++) {
rb_ary_push(result, rb_obj_as_string(rb_ary_entry(cells, index)));
}

return result;
}

static VALUE table_rows_to_s(VALUE rows) {
Check_Type(rows, T_ARRAY);

long length = RARRAY_LEN(rows);
VALUE result = rb_ary_new_capa(length);

for (long index = 0; index < length; index++) {
rb_ary_push(result, table_cells_to_s(rb_ary_entry(rows, index)));
}

return result;
}

static VALUE table_headers(VALUE self, VALUE headers) {
GET_TABLE(self, table);
Check_Type(headers, T_ARRAY);

VALUE json_str = rb_funcall(headers, rb_intern("to_json"), 0);
VALUE json_str = rb_funcall(table_cells_to_s(headers), rb_intern("to_json"), 0);
unsigned long long new_handle = lipgloss_table_headers(table->handle, StringValueCStr(json_str));
return table_wrap(rb_class_of(self), new_handle);
}

static VALUE table_row(VALUE self, VALUE row) {
GET_TABLE(self, table);
Check_Type(row, T_ARRAY);

VALUE json_str = rb_funcall(row, rb_intern("to_json"), 0);
VALUE json_str = rb_funcall(table_cells_to_s(row), rb_intern("to_json"), 0);
unsigned long long new_handle = lipgloss_table_row(table->handle, StringValueCStr(json_str));
return table_wrap(rb_class_of(self), new_handle);
}

static VALUE table_rows(VALUE self, VALUE rows) {
GET_TABLE(self, table);
Check_Type(rows, T_ARRAY);

VALUE json_str = rb_funcall(rows, rb_intern("to_json"), 0);
VALUE json_str = rb_funcall(table_rows_to_s(rows), rb_intern("to_json"), 0);
unsigned long long new_handle = lipgloss_table_rows(table->handle, StringValueCStr(json_str));

return table_wrap(rb_class_of(self), new_handle);
Expand Down
6 changes: 3 additions & 3 deletions sig/lipgloss/lipgloss.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ module Lipgloss

class Table
def initialize: () -> void
def headers: (Array[String] headers) -> Table
def row: (Array[String] row) -> Table
def rows: (Array[Array[String]] rows) -> Table
def headers: (Array[_ToS] headers) -> Table
def row: (Array[_ToS] row) -> Table
def rows: (Array[Array[_ToS]] rows) -> Table
def border: (Symbol border_type) -> Table
def border_style: (Style style) -> Table
def border_top: (bool value) -> Table
Expand Down
12 changes: 12 additions & 0 deletions test/table_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ class TableTest < Minitest::Spec
assert_equal expected, result
end

it "renders cells that aren't strings with to_s" do
table = Lipgloss::Table.new
.headers(["a", :b, 3])
.rows([["1", 2, 3.5], [true, nil, :sym]])
.row([4, "5", false])

result = strip_ansi(table.render)
expected = "╭────┬─┬─────╮\n│a │b│3 │\n├────┼─┼─────┤\n│1 │2│3.5 │\n│true│ │sym │\n│4 │5│false│\n╰────┴─┴─────╯"

assert_equal expected, result
end

it "has HEADER_ROW constant" do
assert_equal(-1, Lipgloss::Table::HEADER_ROW)
end
Expand Down
Loading