Superls.StrFmt (superls v1.2.3)

View Source
iex> Superls.StrFmt.to_string [{12000, :sizeb, [:bright]}]
"  11.7K"

or composable form :

iex> [ [str_fmt1, str_fmt2], 
       str_fmt3
     ] |> Superls.StrFmt.to_string()
str_fmt_unitoutput for a 8 columns terminal
"abc""abc"
[{"abc", :str, [:blue]}]"abc"
[{123, :str, [:blue]}]"123"
[{12000, :sizeb, []}]" 11.7K" # could be B, K, M and G.
[{1696153262, :date, [:blue]}]"2023-10-01"
[{1696153262, :datetime, []}]"23-10-01 09:41:02"
[{"9char-str", {:scr,50}, []}]"9..r" # 50% of 9 chars screen
[{"9char-str", :scr, []}]"9ch..str" # equiv. of {:scr, 100}
[{"foo_", :padr, []}]"foo_____"
[{"_", :padl, [:red]}]"_____"

Summary

Functions

Returns the terminal number of columns.

puts(fmt) convenience for to_string(fmt) |> IO.puts()

Format the str_fmt into a string.

Types

str_fmt_unit()

@type str_fmt_unit() ::
  String.t()
  | {String.t() | :atom | charlist() | number(), :str, IO.ANSI.ansidata()}
  | {integer(), :sizeb, IO.ANSI.ansidata()}
  | {posix_time :: integer(), :date, IO.ANSI.ansidata()}
  | {posix_time :: integer(), :datetime, IO.ANSI.ansidata()}
  | {String.t(), :scr, IO.ANSI.ansidata()}
  | {String.t(), {:scr, number()}, IO.ANSI.ansidata()}
  | {String.t(), :padl, IO.ANSI.ansidata()}
  | {String.t(), :padr, IO.ANSI.ansidata()}
  | [str_fmt_unit()]

t()

@type t() :: [str_fmt_unit()]

Functions

ncols()

@spec ncols() :: integer()

Returns the terminal number of columns.

puts(str_fmt)

@spec puts(str_fmt :: t()) :: :ok

puts(fmt) convenience for to_string(fmt) |> IO.puts()

text_length(txt)

to_string(str_fmt)

@spec to_string(str_fmt :: t()) :: formatted_str :: String.t()

Format the str_fmt into a string.

iex> StrFmt.to_string [{12000, :sizeb, [:bright]}]
"  11.7K"

The transformations are :

  • colors

    see IO.ANSI.

  • padding to a ncols terminal

    padr padl

  • human readable bytes B, K, M, G

    sizeb

  • shorten txt, may add ellipsis if txt length > ncols

    scr

  • shorten percent txt, may add ellipsis if txt length > 44% of ncols

    {scr, 44}

  • human readable date, datetime (posix)

    date datetime

  • basic types interpolation

    str

  • newline

    " "