Current section

Files

Jump to
tobble src tobble.erl
Raw

src/tobble.erl

-module(tobble).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/tobble.gleam").
-export([builder/0, add_row/2, set_title/2, to_list/1, title/1, build/1, build_with_internal/1, render_with_options/2, render/1, render_iter/2]).
-export_type([table/0, builder/0, builder_error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Tobble is a table library for Gleam, which makes it as easy as\n"
" possible to render tables from simple output. It does provide some\n"
" customization options, but they are not very expansive, as Tobble does not\n"
" aim to be a full layout library. Rather, it aims to make it simple to make\n"
" beautiful output for your programs.\n"
"\n"
" ```gleam\n"
" import gleam/io\n"
" import tobble\n"
"\n"
" pub fn main() {\n"
" let assert Ok(table) =\n"
" tobble.builder()\n"
" |> tobble.add_row([\"\", \"Output\"])\n"
" |> tobble.add_row([\"Stage 1\", \"Wibble\"])\n"
" |> tobble.add_row([\"Stage 2\", \"Wobble\"])\n"
" |> tobble.add_row([\"Stage 3\", \"WibbleWobble\"])\n"
" |> tobble.build()\n"
"\n"
" io.println(tobble.render(table))\n"
" }\n"
" ```\n"
"\n"
" ```text\n"
" +---------+--------------+\n"
" | | Output |\n"
" +---------+--------------+\n"
" | Stage 1 | Wibble |\n"
" | Stage 2 | Wobble |\n"
" | Stage 3 | WibbleWobble |\n"
" +---------+--------------+\n"
" ```\n"
).
-opaque table() :: {table,
tobble@internal@rows:rows(binary()),
gleam@option:option(binary())}.
-opaque builder() :: {builder, tobble@internal@builder:builder()}.
-type builder_error() :: {inconsistent_column_count_error, integer(), integer()} |
empty_table_error |
empty_title_error.
-file("src/tobble.gleam", 79).
?DOC(
" Create a new `Builder` for table generation. Once you have completed\n"
" adding your rows to this with `add_row`, you should call `build` to\n"
" generate a `Table`.\n"
).
-spec builder() -> builder().
builder() ->
{builder, tobble@internal@builder:new()}.
-file("src/tobble.gleam", 86).
?DOC(
" Add a row to a table that is being built. Every call to `add_row` for a given\n"
" `Builder` must have the same number of columns, or an\n"
" `InconsistentColumnCountError` will be returned when `build` is called.\n"
).
-spec add_row(builder(), list(binary())) -> builder().
add_row(Builder, Columns) ->
_pipe = erlang:element(2, Builder),
_pipe@1 = tobble@internal@builder:add_row(_pipe, Columns),
{builder, _pipe@1}.
-file("src/tobble.gleam", 120).
?DOC(
" Set a title for a table that is being built. The title must be a non-empty\n"
" string or a `TitleEmptyError` will be returned when `build` is called.\n"
"\n"
" # Example\n"
"\n"
" ```gleam\n"
" let assert Ok(table) =\n"
" tobble.builder()\n"
" |> tobble.set_title(\"Setup\")\n"
" |> tobble.add_row([\"\", \"Output\"])\n"
" |> tobble.add_row([\"Stage 1\", \"Wibble\"])\n"
" |> tobble.add_row([\"Stage 2\", \"Wobble\"])\n"
" |> tobble.add_row([\"Stage 3\", \"WibbleWobble\"])\n"
" |> tobble.build()\n"
"\n"
" io.println(tobble.render(table))\n"
" ```\n"
"\n"
" ```gleam\n"
" Setup\n"
" +---------+--------------+\n"
" | | Output |\n"
" +---------+--------------+\n"
" | Stage 1 | Wibble |\n"
" | Stage 2 | Wobble |\n"
" | Stage 3 | WibbleWobble |\n"
" +---------+--------------+\n"
" ```\n"
).
-spec set_title(builder(), binary()) -> builder().
set_title(Builder, Title) ->
_pipe = erlang:element(2, Builder),
_pipe@1 = tobble@internal@builder:set_title(_pipe, Title),
{builder, _pipe@1}.
-file("src/tobble.gleam", 284).
?DOC(
" Convert an existing table to a list of its rows.\n"
"\n"
" # Example\n"
"\n"
" ```gleam\n"
" let assert Ok(table) =\n"
" tobble.builder()\n"
" |> tobble.add_row([\"\", \"Output\"])\n"
" |> tobble.add_row([\"Stage 1\", \"Wibble\"])\n"
" |> tobble.add_row([\"Stage 2\", \"Wobble\"])\n"
" |> tobble.add_row([\"Stage 3\", \"WibbleWobble\"])\n"
" |> tobble.build()\n"
"\n"
" io.debug(tobble.to_list(table))\n"
" ```\n"
"\n"
" ```gleam\n"
" [\n"
" [\"\", \"Output\"],\n"
" [\"Stage 1\", \"Wibble\"],\n"
" [\"Stage 2\", \"Wobble\"],\n"
" [\"Stage 3\", \"WibbleWobble\"]\n"
" ]\n"
" ```\n"
).
-spec to_list(table()) -> list(list(binary())).
to_list(Table) ->
tobble@internal@rows:to_lists(erlang:element(2, Table)).
-file("src/tobble.gleam", 308).
?DOC(
" Get the title of a table, if there is one.\n"
"\n"
" # Example\n"
"\n"
" ```gleam\n"
" let assert Ok(table) =\n"
" tobble.builder()\n"
" |> tobble.set_title(\"Setup\")\n"
" |> tobble.add_row([\"\", \"Output\"])\n"
" |> tobble.add_row([\"Stage 1\", \"Wibble\"])\n"
" |> tobble.add_row([\"Stage 2\", \"Wobble\"])\n"
" |> tobble.add_row([\"Stage 3\", \"WibbleWobble\"])\n"
" |> tobble.build()\n"
"\n"
" io.debug(tobble.title(table))\n"
" ```\n"
"\n"
" ```gleam\n"
" Some(\"Setup\")\n"
" ```\n"
).
-spec title(table()) -> gleam@option:option(binary()).
title(Table) ->
erlang:element(3, Table).
-file("src/tobble.gleam", 312).
-spec builder_error_from_internal(tobble@internal@builder:builder_error()) -> builder_error().
builder_error_from_internal(Error) ->
case Error of
{inconsistent_column_count_error, Expected, Got} ->
{inconsistent_column_count_error, Expected, Got};
empty_table_error ->
empty_table_error;
empty_title_error ->
empty_title_error
end.
-file("src/tobble.gleam", 128).
?DOC(
" Build a `Table` from the given `Builder`. If an invalid operation was\n"
" performed when constructing the `Builder`, an error will be returned.\n"
).
-spec build(builder()) -> {ok, table()} | {error, builder_error()}.
build(Builder) ->
_pipe = erlang:element(2, Builder),
_pipe@1 = tobble@internal@builder:to_result(_pipe),
_pipe@2 = gleam@result:map(
_pipe@1,
fun(Built) ->
{table, erlang:element(2, Built), erlang:element(3, Built)}
end
),
gleam@result:map_error(_pipe@2, fun builder_error_from_internal/1).
-file("src/tobble.gleam", 136).
?DOC(false).
-spec build_with_internal(tobble@internal@builder:builder()) -> {ok, table()} |
{error, builder_error()}.
build_with_internal(Builder) ->
build({builder, Builder}).
-file("src/tobble.gleam", 247).
?DOC(
" Render the given table to a `String`, with extra options (found in\n"
" `tobble/table_render_opts`). Note that options are applied in order, so if\n"
" duplicate or conflicting options are given, the last one will win.\n"
"\n"
"\n"
" ```gleam\n"
" let assert Ok(table) =\n"
" tobble.builder()\n"
" |> tobble.add_row([\"\", \"Output\"])\n"
" |> tobble.add_row([\"Stage 1\", \"Wibble\"])\n"
" |> tobble.add_row([\"Stage 2\", \"Wobble\"])\n"
" |> tobble.add_row([\"Stage 3\", \"WibbleWobble\"])\n"
" |> tobble.build()\n"
"\n"
" io.println(\n"
" tobble.render_with_options(table, options: [\n"
" table_render_opts.column_width(6),\n"
" table_render_opts.horizontal_rules_after_every_row(),\n"
" ]),\n"
")\n"
" ```\n"
"\n"
" ```text\n"
" +--------+--------+\n"
" | | Output |\n"
" +--------+--------+\n"
" | Stage | Wibble |\n"
" | 1 | |\n"
" +--------+--------+\n"
" | Stage | Wobble |\n"
" | 2 | |\n"
" +--------+--------+\n"
" | Stage | Wibble |\n"
" | 3 | Wobble |\n"
" +--------+--------+\n"
).
-spec render_with_options(table(), list(tobble@table_render_opts:option())) -> binary().
render_with_options(Table, Options) ->
_pipe = tobble@internal@render:default_render_context(
erlang:element(2, Table)
),
_pipe@1 = tobble@table_render_opts:apply_options(_pipe, Options),
_pipe@2 = tobble@internal@render:to_yielder(
_pipe@1,
erlang:element(2, Table),
erlang:element(3, Table)
),
_pipe@3 = gleam@yielder:map(_pipe@2, fun gleam_stdlib:identity/1),
_pipe@4 = gleam@yielder:to_list(_pipe@3),
_pipe@5 = gleam@string_tree:join(_pipe@4, <<"\n"/utf8>>),
unicode:characters_to_binary(_pipe@5).
-file("src/tobble.gleam", 168).
?DOC(
" Render the given table to a `String`, with the default options.\n"
" The output can be customized by using `render_with_options`.\n"
"\n"
" # Example\n"
"\n"
" ```gleam\n"
" let assert Ok(table) =\n"
" tobble.builder()\n"
" |> tobble.add_row([\"\", \"Output\"])\n"
" |> tobble.add_row([\"Stage 1\", \"Wibble\"])\n"
" |> tobble.add_row([\"Stage 2\", \"Wobble\"])\n"
" |> tobble.add_row([\"Stage 3\", \"WibbleWobble\"])\n"
" |> tobble.build()\n"
"\n"
" io.println(tobble.render(table))\n"
" ```\n"
"\n"
" ```text\n"
" +---------+--------------+\n"
" | | Output |\n"
" +---------+--------------+\n"
" | Stage 1 | Wibble |\n"
" | Stage 2 | Wobble |\n"
" | Stage 3 | WibbleWobble |\n"
" +---------+--------------+\n"
" ```\n"
).
-spec render(table()) -> binary().
render(Table) ->
render_with_options(Table, []).
-file("src/tobble.gleam", 203).
?DOC(
" Render the given table to a `Yielder`. Each element of the `Yielder` will\n"
" produce a single line of output, without a trailing newline. Note that\n"
" options are applied in order, so if duplicate or conflicting options\n"
" are given, the last one will win.\n"
"\n"
" # Example\n"
"\n"
" ```gleam\n"
" let assert Ok(table) =\n"
" tobble.builder()\n"
" |> tobble.add_row([\"\", \"Output\"])\n"
" |> tobble.add_row([\"Stage 1\", \"Wibble\"])\n"
" |> tobble.add_row([\"Stage 2\", \"Wobble\"])\n"
" |> tobble.add_row([\"Stage 3\", \"WibbleWobble\"])\n"
" |> tobble.build()\n"
"\n"
"\n"
" table\n"
" |> tobble.render_iter(options: [])\n"
" |> yielder.each(io.println)\n"
" ```\n"
"\n"
" ```text\n"
" +---------+--------------+\n"
" | | Output |\n"
" +---------+--------------+\n"
" | Stage 1 | Wibble |\n"
" | Stage 2 | Wobble |\n"
" | Stage 3 | WibbleWobble |\n"
" +---------+--------------+\n"
" ```\n"
).
-spec render_iter(table(), list(tobble@table_render_opts:option())) -> gleam@yielder:yielder(binary()).
render_iter(Table, Options) ->
_pipe = tobble@internal@render:default_render_context(
erlang:element(2, Table)
),
_pipe@1 = tobble@table_render_opts:apply_options(_pipe, Options),
tobble@internal@render:to_yielder(
_pipe@1,
erlang:element(2, Table),
erlang:element(3, Table)
).