Cowrie (cowrie v0.3.2)

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).

Link to this section 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.

Link to this section Functions

Link to this function

alert(text, opts \\ [])

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

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

Examples

iex> br

Outputs available ANSI colors, useful when you want to choose a color visually.

examples

Examples

iex> colors
Link to this function

comment(text, opts \\ [])

Prints a comment to STDOUT, i.e. supplemental text regarded as non-essential.

Formatting and transforms options: :comment

examples

Examples

iex> comment("Objects are closer than they appear")
Objects are closer than they appear
Link to this function

dd(text, opts \\ [])

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

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 ...
Link to this function

dt(text, opts \\ [])

Prints a dictionary term (dt) to STDOUT

Formatting and transforms options: :dt

examples

Examples

iex(5)> dt("Thonking", dt: ">>> " <> IO.ANSI.bright())
>>> Thonking
Link to this function

error(text, opts \\ [])

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.

Link to this function

h1(text, opts \\ [])

Prints a primary heading (h1) to STDOUT.

Formatting and transforms options: :h1

examples

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!
Link to this function

h2(text, opts \\ [])

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

Examples

iex> hr(hr_char: "~", hr_padding: 0)
              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Link to this function

info(text, opts \\ [])

Prints informational text to STDOUT.

Formatting and transforms options: :info

Link to this function

li(text, opts \\ [])

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

Examples

iex> li("Bullet me", li_bullet: "~>")
~> Bullet me
Link to this function

line(text, opts \\ [])

Prints a line of "unformatted" text to STDOUT.

Formatting and transforms options: :line

Link to this function

ol(items, opts \\ [])

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

Examples

iex> ol(["do", "re", "mi"])
1. do
2. re
3. mi
Link to this function

prompt(text, opts \\ [])

@spec prompt(text :: binary(), opts :: keyword()) :: binary()

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

Link to this function

secret(text, opts \\ [])

@spec secret(text :: binary(), opts :: keyword()) :: binary()

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

Link to this function

spinner(callback, opts \\ [])

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

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!"}
Link to this function

table(rows, headers \\ [], opts \\ [])

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

Examples:

iex(12)> table([["Max", "Mao", "Brutus"], ["Fido", "Whiskers", "Wilbur"]], ["Dogs", "Cats", "Pigs"])
+------+----------+--------+
| Dogs | Cats     | Pigs   |
+------+----------+--------+
| Max  | Mao      | Brutus |
| Fido | Whiskers | Wilbur |
+------+----------+--------+
Link to this function

ul(items, opts \\ [])

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

Examples

iex(13)> ul(["Sunlight", "Water", "Soil"])
- Sunlight
- Water
- Soil

iex(14)> ul(["Sunlight", "Water", "Soil"], li_bullet: ">")
> Sunlight
> Water
> Soil
Link to this function

warning(text, opts \\ [])

Prints a warning to STDOUT.

Formatting and transforms options: :warning

Link to this function

yes?(text, opts \\ [])

@spec yes?(text :: binary(), opts :: keyword()) :: boolean()

Prompts the user to continue with a simple Yes or No.