Skip to content

Commit

Permalink
Merge pull request #15 from silbermm/feature/borderless-tables
Browse files Browse the repository at this point in the history
Add option for borderless tables
  • Loading branch information
silbermm authored Aug 9, 2023
2 parents ecd31d7 + a621058 commit eb327ae
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 35 additions & 10 deletions lib/prompt.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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."
]
)

Expand All @@ -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"]])
Expand All @@ -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 |
Expand All @@ -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} ->
Expand All @@ -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
Expand All @@ -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
""
Expand All @@ -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
Expand Down
36 changes: 24 additions & 12 deletions lib/prompt/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions test/prompt_module_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -65,7 +65,6 @@ defmodule PromptModuleTest do
end) =~ "Unrecognized option - whatever"
end


test "fallback" do
assert capture_io(fn ->
Example.main([])
Expand Down
8 changes: 4 additions & 4 deletions test/prompt_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit eb327ae

Please sign in to comment.