This repository has been archived by the owner on Aug 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace TableRex dependency with a hard copy instead
- Loading branch information
Showing
15 changed files
with
3,538 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule TableRex do | ||
@moduledoc """ | ||
TableRex generates configurable, text-based tables for display | ||
""" | ||
alias TableRex.Renderer | ||
alias TableRex.Table | ||
|
||
@doc """ | ||
A shortcut function to render with a one-liner. | ||
Sacrifices all customisation for those that just want sane defaults. | ||
Returns `{:ok, rendered_string}` on success and `{:error, reason}` on failure. | ||
""" | ||
@spec quick_render(list, list, String.t() | nil) :: Renderer.render_return() | ||
def quick_render(rows, header \\ [], title \\ nil) when is_list(rows) and is_list(header) do | ||
Table.new(rows, header, title) | ||
|> Table.render() | ||
end | ||
|
||
@doc """ | ||
A shortcut function to render with a one-liner. | ||
Sacrifices all customisation for those that just want sane defaults. | ||
Returns the `rendered_string` on success and raises `RuntimeError` on failure. | ||
""" | ||
@spec quick_render!(list, list, String.t() | nil) :: String.t() | no_return | ||
def quick_render!(rows, header \\ [], title \\ nil) when is_list(rows) and is_list(header) do | ||
case quick_render(rows, header, title) do | ||
{:ok, rendered} -> rendered | ||
{:error, reason} -> raise TableRex.Error, message: reason | ||
end | ||
end | ||
end | ||
|
||
defmodule TableRex.Error do | ||
defexception [:message] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2015 Darian Moody | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
This directory contains a copy of [djm/table_rex](https://github.com/djm/table_rex). | ||
|
||
A hard copy is used instead of a dependency so that `mix archive.install ...`, | ||
which does not recognize the archive's defined dependencies, is supported. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
defmodule TableRex.Cell do | ||
@moduledoc """ | ||
Defines a struct that represents a single table cell, and helper functions. | ||
A cell stores both the original data _and_ the string-rendered version, | ||
this decision was taken as a tradeoff: this way uses more memory to store | ||
the table structure but the renderers gain the ability to get direct access | ||
to the string-coerced data rather than having to risk repeated coercion or | ||
handle their own storage of the computer values. | ||
Fields: | ||
* `raw_value`: The un-coerced original value | ||
* `rendered_value`: The stringified value for rendering | ||
* `align`: | ||
* `:left`: left align text in the cell. | ||
* `:center`: center text in the cell. | ||
* `:right`: right align text in the cell. | ||
* `nil`: align text in cell according to column alignment. | ||
* `color`: the ANSI color of the cell. | ||
If creating a Cell manually: raw_value is the only required key to | ||
enable that Cell to work well with the rest of TableRex. It should | ||
be set to a piece of data that can be rendered to string. | ||
""" | ||
alias TableRex.Cell | ||
|
||
defstruct raw_value: nil, rendered_value: "", align: nil, color: nil | ||
|
||
@type t :: %__MODULE__{} | ||
|
||
@doc """ | ||
Converts the passed value to be a normalised %Cell{} struct. | ||
If a non %Cell{} value is passed, this function returns a new | ||
%Cell{} struct with: | ||
* the `rendered_value` key set to the stringified binary of the | ||
value passed in. | ||
* the `raw_value` key set to original data passed in. | ||
* any other options passed are applied over the normal struct | ||
defaults, which allows overriding alignment & color. | ||
If a %Cell{} is passed in with no `rendered_value` key, then the | ||
`raw_value` key's value is rendered and saved against it, otherwise | ||
the Cell is passed through untouched. This is so that advanced use | ||
cases which require direct Cell creation and manipulation are not | ||
hindered. | ||
""" | ||
@spec to_cell(Cell.t()) :: Cell.t() | ||
def to_cell(%Cell{rendered_value: rendered_value} = cell) when rendered_value != "", do: cell | ||
|
||
def to_cell(%Cell{raw_value: raw_value} = cell) do | ||
%Cell{cell | rendered_value: to_string(raw_value)} | ||
end | ||
|
||
@spec to_cell(any, list) :: Cell.t() | ||
def to_cell(value, opts \\ []) do | ||
opts = Enum.into(opts, %{}) | ||
|
||
%Cell{rendered_value: to_string(value), raw_value: value} | ||
|> Map.merge(opts) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule TableRex.Column do | ||
@moduledoc """ | ||
Defines a struct that represents a column's metadata | ||
The align field can be one of :left, :center or :right. | ||
""" | ||
|
||
defstruct align: :left, padding: 1, color: nil | ||
|
||
@type t :: %__MODULE__{} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule TableRex.Renderer do | ||
@moduledoc """ | ||
An Elixir behaviour that defines the API Renderers should conform to, allowing | ||
for display output in a variety of formats. | ||
""" | ||
|
||
@typedoc "Return value of the render function." | ||
@type render_return :: {:ok, String.t()} | {:error, String.t()} | ||
|
||
@doc "Returns a Map of the options and their default values required by the renderer." | ||
@callback default_options() :: map | ||
|
||
@doc "Renders a passed %TableRex.Table{} struct into a string." | ||
@callback render(table :: %TableRex.Table{}, opts :: list) :: render_return | ||
end |
Oops, something went wrong.