diff --git a/ext/lipgloss/table.c b/ext/lipgloss/table.c index ef0c590..bd1d83d 100644 --- a/ext/lipgloss/table.c +++ b/ext/lipgloss/table.c @@ -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); diff --git a/sig/lipgloss/lipgloss.rbs b/sig/lipgloss/lipgloss.rbs index 680f643..6122439 100644 --- a/sig/lipgloss/lipgloss.rbs +++ b/sig/lipgloss/lipgloss.rbs @@ -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 diff --git a/test/table_test.rb b/test/table_test.rb index 297d9fd..6f5c544 100644 --- a/test/table_test.rb +++ b/test/table_test.rb @@ -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