hledger-lib-1.20.4: A reusable library providing the core functionality of hledger

Hledger.Utils.String

Description

String formatting helpers, starting to get a bit out of control.

Synopsis

# Documentation

takeEnd :: Int -> [a] -> [a] Source #

Take elements from the end of a list.

# misc

Double-quote this string if it contains whitespace, single quotes or double-quotes, escaping the quotes as needed.

Single-quote this string if it contains whitespace or double-quotes. No good for strings containing single quotes.

words' :: String -> [String] Source #

Quote-aware version of words - don't split on spaces which are inside quotes. NB correctly handles "a'b" but not "'a'". Can raise an error if parsing fails.

unwords' :: [String] -> String Source #

Quote-aware version of unwords - single-quote strings which contain whitespace

Strip ANSI escape sequences from a string.

>>> stripAnsi "\ESC[31m-1\ESC[m"
"-1"


# single-line layout

Remove trailing whitespace.

Remove trailing newlines/carriage returns.

Remove consecutive line breaks, replacing them with single space

Clip and pad a string to a minimum & maximum width, andor leftright justify it. Works on multi-line strings too (but will rewrite non-unix line endings).

# multi-line layout

Join several multi-line strings as side-by-side rectangular strings of the same height, top-padded. Treats wide characters as double width.

Join several multi-line strings as side-by-side rectangular strings of the same height, bottom-padded. Treats wide characters as double width.

concatOneLine :: [String] -> String Source #

Join multi-line strings horizontally, after compressing each of them to a single line with a comma and space between each original line.

Join strings vertically, left-aligned and right-padded.

Join strings vertically, right-aligned and left-padded.

Convert a multi-line string to a rectangular string top-padded to the specified height.

Convert a multi-line string to a rectangular string bottom-padded to the specified height.

Convert a multi-line string to a rectangular string left-padded to the specified width. Treats wide characters as double width.

Convert a multi-line string to a rectangular string right-padded to the specified width. Treats wide characters as double width.

Clip a multi-line string to the specified width and height from the top left.

fitto :: Int -> Int -> String -> String Source #

Clip and pad a multi-line string to fill the specified width and height.

# wide-character-aware layout

Get the designated render width of a character: 0 for a combining character, 1 for a regular character, 2 for a wide character. (Wide characters are rendered as exactly double width in apps and fonts that support it.) (From Pandoc.)

Calculate the render width of a string, considering wide characters (counted as double width)

Like strWidth, but also strips ANSI escape sequences before calculating the width.

This is no longer used in code, as widths are calculated before adding ANSI escape sequences, but is being kept around for now.

Double-width-character-aware string truncation. Take as many characters as possible from a string without exceeding the specified width. Eg takeWidth 3 "りんご" = "り".

General-purpose wide-char-aware single-line string layout function. It can left- or right-pad a short string to a minimum width. It can left- or right-clip a long string to a maximum width, optionally inserting an ellipsis (the third argument). It clips and pads on the right when the fourth argument is true, otherwise on the left. It treats wide characters as double width.

A version of fitString that works on multi-line strings, separate for now to avoid breakage. This will rewrite any line endings to unix newlines.

Left-pad a string to the specified width. Treats wide characters as double width. Works on multi-line strings too (but will rewrite non-unix line endings).

Right-pad a string to the specified width. Treats wide characters as double width. Works on multi-line strings too (but will rewrite non-unix line endings).