Cowrie (cowrie v0.5.0)
Cowrie
helps you print beautiful and consistent Terminal output to the Shell
using familiar functions inspired by HTML.
The output of each function can be styled via configuration (see the page on configuration) or by overriding any configured styles by passing relevant options as the second argument. This is analogous to cascading style sheets (CSS).
Summary
Functions
Prints an alert box of the specified type to STDOUT (think Bootstrap alert boxes).
Prints a line break (an empty line) to STDOUT.
Outputs available ANSI colors, useful when you want to choose a color visually.
Prints a comment to STDOUT, i.e. supplemental text regarded as non-essential.
Prints a dictionary definition (dd) to STDOUT
Prints a demonstration of all output functions for visual inspection, useful when you are tweaking your styling.
Prints a dictionary term (dt) to STDOUT
Prints an error message to STDERR.
Prints a primary heading (h1) to STDOUT.
Prints a secondary heading (h2) to STDOUT.
Prints a horizontal rule (hr) to STDOUT.
Prints informational text to STDOUT.
Prints a list item (unordered) to STDOUT.
Prints a line of "unformatted" text to STDOUT.
Prints an ordered list of items (ul) to STDOUT.
Prompts the user for input. Input will be consumed until Enter is pressed.
Careful: the returned value will include a newline (\n
)!
The user's input will not be visible to them as they type in the console. This method is useful when asking for sensitive information such as a password.
Displays a spinner animation which offers visual feedback appropriate for long- running tasks. Displaying a spinner animation is most appropriate for tasks which do not provide their own console messaging or when you wish to silence the task's own messaging.
Prints a table with the given rows to STDOUT (headers are optional).
Prints an unordered list of items (ul) to STDOUT.
Prints a warning to STDOUT.
Prompts the user to continue with a simple Yes
or No
.
Functions
Prints an alert box of the specified type to STDOUT (think Bootstrap alert boxes).
Options:
:type
indicates the type of alert, one of:danger
,:info
,:success
,:warning
. default::warning
Formatting and transform options correspond to the given alert type
:
:alert_danger
for type:danger
:alert_info
for type:info
:alert_success
for type:success
:alert_warning
for type:warning
Examples
iex(2)> alert("The answer is 42", type: :info)
╔═══════════════════════════╗
║ ℹ️ Info:️ The answer is 42 ║
╚═══════════════════════════╝
Prints a line break (an empty line) to STDOUT.
Formatting and transforms options: :br
Examples
iex> br
Outputs available ANSI colors, useful when you want to choose a color visually.
Examples
iex> colors
Prints a comment to STDOUT, i.e. supplemental text regarded as non-essential.
Formatting and transforms options: :comment
Examples
iex> comment("Objects are closer than they appear")
Objects are closer than they appear
Prints a dictionary definition (dd) to STDOUT
Formatting and transforms options: :dd
Prints a demonstration of all output functions for visual inspection, useful when you are tweaking your styling.
Examples
iex> demo
Cowrie Formatting h1/2
Common Outputs h2/2)
This is a line of text line/2
This is an informational message info/2
This is a warning/2
Here comes an hr/2:
This is an error/2
────────────────────────────────────────────────────────────────────────
Alerts h2/2
╔════════════════════════════════╗
║ ℹ️ Info:️ This is an info alert ║
╚════════════════════════════════╝
# ... etc ...
Prints a dictionary term (dt) to STDOUT
Formatting and transforms options: :dt
Examples
iex(5)> dt("Thonking", dt: ">>> " <> IO.ANSI.bright())
>>> Thonking
Prints an error message to STDERR.
Formatting and transforms options: :error
Your terminal may already provide formatting for messages sent to STDERR; be careful about formatting these messages because you are more likely to have tests that assert for specific error messages being logged, and adding ANSI formatting codes to the output can make it a bit more difficult to verify the exact text.
Prints a primary heading (h1) to STDOUT.
Formatting and transforms options: :h1
Examples
Normally, you would put your pre- and post-transformation functions in your config, but you can pass them as arguments if you really want to:
iex> h1("This is awesome!", pre_transforms: %{h1: &Cowrie.Transforms.upcase/2})
THIS IS AWESOME!
Prints a secondary heading (h2) to STDOUT.
Formatting and transforms options: :h2
Prints a horizontal rule (hr) to STDOUT.
Formatting options:
:hr
for the line as a whole:hr_char
specifies the character(s) to be repeated to form the line.:hr_padding
number of whitespace columns
Transform key: :hr
Examples
iex> hr(hr_char: "~", hr_padding: 0)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Prints informational text to STDOUT.
Formatting and transforms options: :info
Prints a list item (unordered) to STDOUT.
This function is useful if don't have a list to pass to ul/2
.
Formatting option: :li
; transform options: :ul_li
.
:li_bullet
changes the character(s) used for the list bullet.
Examples
iex> li("Bullet me", li_bullet: "~>")
~> Bullet me
Prints a line of "unformatted" text to STDOUT.
Formatting and transforms options: :line
Prints an ordered list of items (ul) to STDOUT.
:ol_start
the integer used to start the lists. Default: 1:li
for formatting the individual list items.
Pre- and post-transform key: :ol_li
Examples
iex> ol(["do", "re", "mi"])
1. do
2. re
3. mi
Prompts the user for input. Input will be consumed until Enter is pressed.
Careful: the returned value will include a newline (\n
)!
Formatting and transforms options: :prompt
The user's input will not be visible to them as they type in the console. This method is useful when asking for sensitive information such as a password.
Formatting and transforms options: :secret
Displays a spinner animation which offers visual feedback appropriate for long- running tasks. Displaying a spinner animation is most appropriate for tasks which do not provide their own console messaging or when you wish to silence the task's own messaging.
The callback function must be a zero-arity anonymous function. The return value of the spinner function will the return value of this function.
Note: the task being executed should avoid any use of IO.puts
(or other functions
that write to STDOUT) and instead rely on Mix.shell().info
; all console messages
logged by the callback will be silenced by temporarily setting the Mix.Shell
to
Mix.Shell.Quiet
for the duration of the executed function.
Examples
Using the function capture notation &
:
iex> spinner(&Heavy.work/0)
Using an anonymous function:
iex> spinner(fn -> Heavy.work() end)
As used in the demo:
iex> spinner(fn -> Process.sleep(:timer.seconds(5)) end)
Demonstrating a return value:
iex> spinner(fn ->
...> Process.sleep(:timer.seconds(5))
...> {:ok, "Task complete!"}
...> end)
{:ok, "Task complete!"}
Specifying a custom spinner module:
iex> spinner(fn ->
...> Process.sleep(:timer.seconds(5))
...> {:ok, "Task complete!"}
...> end, spinner_module: Tacocat.Cat)
{:ok, "Task complete!"}
Prints a table with the given rows to STDOUT (headers are optional).
The first argument should be a list of lists, where each internal list represents a row of data. The second argument should be a list of headers -- the length of this list should match the length of the lists passed as rows.
Formatting options:
:th
for header cells:td
for data cells
Transforms:
:th
for header cells:td
for data cells
Examples:
iex(12)> table([["Max", "Mao", "Brutus"], ["Fido", "Whiskers", "Wilbur"]], ["Dogs", "Cats", "Pigs"])
+------+----------+--------+
| Dogs | Cats | Pigs |
+------+----------+--------+
| Max | Mao | Brutus |
| Fido | Whiskers | Wilbur |
+------+----------+--------+
Prints an unordered list of items (ul) to STDOUT.
:li_bullet
the character used to denote each list item. Default:-
:li
for formatting the individual list items.
Transform key for each list item: :ul_li
Examples
iex(13)> ul(["Sunlight", "Water", "Soil"])
- Sunlight
- Water
- Soil
iex(14)> ul(["Sunlight", "Water", "Soil"], li_bullet: ">")
> Sunlight
> Water
> Soil
Prints a warning to STDOUT.
Formatting and transforms options: :warning
Prompts the user to continue with a simple Yes
or No
.