View Source Boxen (boxen v0.2.0)

Port of boxen library for elixir.

usage

Usage

# Most simple usage
Boxen.boxify("Hello, world")
# => {:ok, string} | {:error, string}

# with title
Boxen.boxify("Hello, world", title: "Message")

# with option
Boxen.boxify("Hello, world")

helper

Helper

The Boxen.boxify function returns a tuple {:ok, string} or {:error, string}. To actually the box, you have to print the string(IO.puts).

This library has a helper function Boxen.Helpers.print_output/1 that prints the boxified text.

Boxen.boxify("Hello, world") |> Boxen.Helpers.print_output
# => Boxified output | {:error, string}

options

Options

As a keyword list.

title

title

Adds title for the box. If width is not provided & title's width is greater than text's width, then box width will be equal to title's width.

Type: string | nil. Default: nil

Example:

Boxen.boxify("hello world", title: "Something")

padding

padding

Adds padding inside the box.

Type: map | integer. Default: 0

For map, the map should contain top, bottom, left and right value as atom.

Example

# Integer
Boxen.boxify("hello world", padding: 1)

# Map
Boxen.boxify("hello world", padding: %{top: 1, bottom: 1, left: 2, right: 2})

It is not necessary to pass all the options inside the map. You can just pass %{top: 1, bottom: 1}, and the rest will have the default value of 0.

margin

margin

Adds margin outside the box. Default: 0

Type: map | integer

For map, the map should contain top, bottom, left and right value as atom.

Example:

# Integer
Boxen.boxify("hello world", margin: 1)

# Map
Boxen.boxify("hello world", margin: %{top: 1, bottom: 1, left: 2, right: 2})

It is not necessary to pass all the options inside the map. You can just pass %{top: 1, bottom: 1}, and the rest will have the default value of 0.

text_alignment

text_alignment

Alignment of text inside the box.

Type: atom

Value: :left(default) | :right | :center

Example:

Boxen.boxify("hello 
world 
elixir is awesome", text_alignment: :center)

title_alignment

title_alignment

Alignment of title on the top of the box.

Type: atom

Value: :left(default) | :right | :center

Example:

Boxen.boxify("hello world", title: "Message", title_alignment: :center)

box

box

The box type to show the text content in. Default is :single.

Type: atom | map

Value(taken directly from Boxen library):

  • :fallback: Just blank space
  • :single

foo
  • :double

foo
  • :round

foo
  • :bold

foo
  • :single_double

foo
  • :double_single

foo
  • :classic
+---+
|foo|
+---+
  • :arrow

foo

Example:

Boxen.boxify("Hello world", box: :double)

You can also add your own box through this option. Pass a map containing the following data:

top_left,
top,
top_right,
right,
bottom_right,
bottom,
bottom_left,
left

It is not necessary to pass all the options inside the map. You can just pass %{top: 1, bottom: 1}, and the rest will have the default to blank spaces.

Example:

Boxen.boxify("Hello world", box: %{top_left: "->", ....})

width

width

Set width for the box.

Type: integer

Example:

Boxen.boxify("hello world", width: 4)

If width is less than given text, then the text is hard wrapped to the given width.

border_color

border_color

Sets the color of the border.

Type: string

The value should be an ANSI escape sequence for color.

Example(using IO.ANSI module to generate color):

red = IO.ANSI.red() #=> ""
Boxen.boxify("hello world", border_color: red)

text_color

text_color

Sets the color for the whole text.

Type: string

The value should be an ANSI escape sequence for color.

Example(using IO.ANSI module to generate color):

blue = IO.ANSI.blue() #=> ""
Boxen.boxify("hello world", text_color: blue)

custom-coloring

Custom coloring

If you want more granular control over color, you can provide your own text with ANSI escape sequence embedded in it. Same applies for title.

Example:

text = IO.ANSI.format([:blue, "hello, ", :cyan, "elixir"]) |> IO.chardata_to_string #=> "hello, elixir"

Boxen.boxify(text)

acknowledgments

Acknowledgments

Thanks to Sindre Sorhus and the contributors of boxen library. This library is inspired by boxen, and as such, almost all functions are re-written in elixir(with some minor changes here and there).

Link to this section Summary

Functions

Function to boxify a given text.

Link to this section Types

@type opts() :: [
  box: atom() | map(),
  title: String.t(),
  title_alignment: :left | :right | :center,
  text_alignment: :left | :right | :center,
  padding: integer(),
  margin: integer(),
  width: non_neg_integer(),
  border_color: String.t(),
  text_color: String.t()
]

Link to this section Functions

Link to this function

boxify(input_text, opts \\ [])

View Source
@spec boxify(input_text :: String.t(), opts :: opts()) ::
  {:ok, String.t()} | {:error, String.t()}

Function to boxify a given text.

Options:

  • :box: Type of box
  • :title: Title
  • :title_alignment: Alignment of title. Possible values: :left | :right | :center

  • :text_alignment: Alignment of text inside the box. Possible values: :left | :right | :center

  • :padding: Padding inside the box.
  • :margin: Margin outside the box.
  • :width: Width of the box.
  • :border_color: Color of borders.
  • :text_color: Text color.