diff --git a/CHANGELOG.md b/CHANGELOG.md index 54bd52e..5b2fa9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.9.3] +### Added + - Allow for border-less tables + ## [0.9.2] - 2023-07-31 ### Added - a `short` option for arguments diff --git a/lib/prompt.ex b/lib/prompt.ex index 2266919..0538682 100644 --- a/lib/prompt.ex +++ b/lib/prompt.ex @@ -371,7 +371,15 @@ defmodule Prompt do doc: "Use the first element as the header for the table." ], border: [ - type: {:in, [:normal, :markdown]} + type: {:in, [:normal, :markdown, :none]}, + default: :normal, + doc: + "Determine how the border is displayed, one of :normal (default), :markdown, or :none" + ], + color: [ + type: {:in, @colors}, + doc: + "The text color. One of `#{Kernel.inspect(@colors)}`. Defaults to the terminal default." ] ) @@ -382,8 +390,6 @@ defmodule Prompt do Supported options: #{NimbleOptions.docs(@table_options)} - * border: :normal (default) | :markdown --- determine how the border is displayed - ## Examples iex> Prompt.table([["Hello", "from", "the", "terminal!"],["this", "is", "another", "row"]]) @@ -403,7 +409,7 @@ defmodule Prompt do | this | is | another | row | +-------+------+---------+----------+ " - + iex> Prompt.table([["One", "Two", "Three", "Four"], ["Hello", "from", "the", "terminal!"],["this", "is", "another", "row"]], header: true, border: :markdown) " | One | Two | Three | Four | @@ -412,13 +418,28 @@ defmodule Prompt do | this | is | another | row | " + iex> Prompt.table([["Hello", "from", "the", "terminal!"],["this", "is", "another", "row"]], border: :none) + " + Hello from the terminal + this is another row + " + """ @spec table(list(list()), keyword()) :: :ok def table(matrix, opts \\ []) when is_list(matrix) do case NimbleOptions.validate(opts, @table_options) do {:ok, options} -> - matrix - |> build_table(options) + table = matrix |> build_table(options) + color = Keyword.get(options, :color, IO.ANSI.default_color()) + + [ + :reset, + IO.ANSI.default_background(), + color, + table, + :reset + ] + |> IO.ANSI.format() |> write() {:error, err} -> @@ -432,7 +453,7 @@ defmodule Prompt do other mediums like markdown files. """ @spec table_data(list(list()), keyword()) :: [<<>> | [any()], ...] - def table_data(matrix, opts \\ []) when is_list(matrix) do + def table_data(matrix, opts \\ [border: :normal]) when is_list(matrix) do matrix |> build_table(opts) end @@ -442,7 +463,7 @@ defmodule Prompt do row_delimiter = Prompt.Table.row_delimiter(tbl) first = - if Keyword.get(opts, :border) != :markdown do + if Keyword.get(opts, :border) == :normal do row_delimiter else "" @@ -463,13 +484,17 @@ defmodule Prompt do end last = - if Keyword.get(opts, :border) != :markdown do + if Keyword.get(opts, :border) == :normal do row_delimiter else "" end - [first, next, rest, last] + if Keyword.get(opts, :color) do + [first, next, rest, last] + else + [first, next, rest, last] + end end defp run(opts, validation, io) do diff --git a/lib/prompt/table.ex b/lib/prompt/table.ex index 76ecb48..997b81d 100644 --- a/lib/prompt/table.ex +++ b/lib/prompt/table.ex @@ -37,10 +37,17 @@ defmodule Prompt.Table do row_str = for {column, idx} <- Enum.with_index(row) do column_string = column_str(column, Map.get(table.columns_length, idx)) - "| #{column_string} " + + if Keyword.get(table.opts, :border) == :none, + do: " #{column_string} ", + else: "| #{column_string} " end - "#{row_str}|\n" + if Keyword.get(table.opts, :border) == :none do + "#{row_str}\n" + else + "#{row_str}|\n" + end end @doc "Generate the row delimiter" @@ -49,18 +56,23 @@ defmodule Prompt.Table do delimiter = case Keyword.get(table.opts, :border, :normal) do :markdown -> "|" - _ -> "+" + :normal -> "+" + :none -> "" end - row = - for column_number <- 0..(table.column_count - 1) do - # should get us the length of the largest cell in this column - length = Map.get(table.columns_length, column_number) - r = row_str(length) - "#{delimiter}-#{r}-" - end - - [row, delimiter, "\n"] + if delimiter == "" do + "\n" + else + row = + for column_number <- 0..(table.column_count - 1) do + # should get us the length of the largest cell in this column + length = Map.get(table.columns_length, column_number) + r = row_str(length) + "#{delimiter}-#{r}-" + end + + [row, delimiter, "\n"] + end end defp row_str(total_length), do: Enum.map(1..total_length, fn _ -> "-" end) diff --git a/mix.exs b/mix.exs index aefca62..792d580 100644 --- a/mix.exs +++ b/mix.exs @@ -5,7 +5,7 @@ defmodule Prompt.MixProject do [ app: :prompt, description: "A terminal toolkit and a set of helpers for building console applications.", - version: "0.9.2", + version: "0.9.3", elixir: "~> 1.10", package: package(), source_url: "https://github.com/silbermm/prompt", diff --git a/test/prompt_module_test.exs b/test/prompt_module_test.exs index 90e33e1..defa0b1 100644 --- a/test/prompt_module_test.exs +++ b/test/prompt_module_test.exs @@ -30,11 +30,11 @@ defmodule Example do use Prompt.Router, otp_app: :prompt command :test, ExampleCommand do - arg :help, :boolean + arg(:help, :boolean) end command "", FallbackCommand do - arg :help, :boolean + arg(:help, :boolean) end @impl true @@ -65,7 +65,6 @@ defmodule PromptModuleTest do end) =~ "Unrecognized option - whatever" end - test "fallback" do assert capture_io(fn -> Example.main([]) diff --git a/test/prompt_test.exs b/test/prompt_test.exs index fa89996..5f6eccd 100644 --- a/test/prompt_test.exs +++ b/test/prompt_test.exs @@ -187,8 +187,8 @@ defmodule PromptTest do ["Hello", "from", "the", "terminal!"], ["this", "is", "another", "row"] ]) - end) == - "+-------+------+---------+-----------+\n| Hello | from | the | terminal! |\n| this | is | another | row |\n+-------+------+---------+-----------+\n" + end) =~ + "\e[39m+-------+------+---------+-----------+\n| Hello | from | the | terminal! |\n| this | is | another | row |\n+-------+------+---------+-----------+\n" end test "display simple table with headers" do @@ -200,8 +200,8 @@ defmodule PromptTest do ], header: true ) - end) == - "+-------+------+---------+-----------+\n| Hello | from | the | terminal! |\n+-------+------+---------+-----------+\n| this | is | another | row |\n+-------+------+---------+-----------+\n" + end) =~ + "\e[39m+-------+------+---------+-----------+\n| Hello | from | the | terminal! |\n+-------+------+---------+-----------+\n| this | is | another | row |\n+-------+------+---------+-----------+\n" end test "return table data" do