hledger-lib-1.41: A library providing the core functionality of hledger

Safe Haskell | None
Language | Haskell2010

Hledger.Utils.String

Contents

- misc
- single-line layout
- wide-character-aware layout

Description

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

Synopsis

- takeEnd :: Int -> [a] -> [a]
- capitalise :: String -> String
- lowercase :: String -> String
- uppercase :: String -> String
- underline :: String -> String
- stripbrackets :: String -> String
- quoteIfNeeded :: String -> String
- singleQuoteIfNeeded :: String -> String
- quoteForCommandLine :: String -> String
- words' :: String -> [String]
- unwords' :: [String] -> String
- stripAnsi :: String -> String
- strip :: String -> String
- lstrip :: String -> String
- rstrip :: String -> String
- strip1Char :: Char -> Char -> String -> String
- stripBy :: (Char -> Bool) -> String -> String
- strip1By :: (Char -> Bool) -> String -> String
- chomp :: String -> String
- chomp1 :: String -> String
- singleline :: String -> String
- elideLeft :: Int -> String -> String
- elideRight :: Int -> String -> String
- formatString :: Bool -> Maybe Int -> Maybe Int -> String -> String
- charWidth :: Char -> Int
- strWidth :: String -> Int
- strWidthAnsi :: String -> Int
- takeWidth :: Int -> String -> String

# Documentation

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

Take elements from the end of a list.

# misc

capitalise :: String -> String #

lowercase :: String -> String #

uppercase :: String -> String #

underline :: String -> String #

stripbrackets :: String -> String #

quoteIfNeeded :: String -> String #

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

singleQuoteIfNeeded :: String -> String #

Single-quote this string if it contains whitespace or double-quotes. Does not work for strings containing single quotes.

quoteForCommandLine :: String -> String #

Try to single- and backslash-quote a string as needed to make it usable as an argument on a (sh/bash) shell command line. At least, well enough to handle common currency symbols, like $. Probably broken in many ways.

    >>> quoteForCommandLine "a"
    "a"
    >>> quoteForCommandLine "\""
    "'\"'"
    >>> quoteForCommandLine "$"
    "'\\$'"

words' :: String -> [String] #

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 #

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

stripAnsi :: String -> String #

Strip ANSI escape sequences from a string.

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

# single-line layout

strip :: String -> String #

Remove leading and trailing whitespace.

lstrip :: String -> String #

Remove leading whitespace.

rstrip :: String -> String #

Remove trailing whitespace.

strip1Char :: Char -> Char -> String -> String #

Strip the given starting and ending character from the start and end of a string if both are present.

stripBy :: (Char -> Bool) -> String -> String #

Strip a run of zero or more characters matching the predicate from the start and end of a string.

strip1By :: (Char -> Bool) -> String -> String #

Strip a single balanced enclosing pair of a character matching the predicate from the start and end of a string.

chomp :: String -> String #

Remove all trailing newlines/carriage returns.

chomp1 :: String -> String #

Remove all trailing newline/carriage returns, leaving just one trailing newline.

singleline :: String -> String #

Remove consecutive line breaks, replacing them with single space

elideLeft :: Int -> String -> String #

elideRight :: Int -> String -> String #

formatString :: Bool -> Maybe Int -> Maybe Int -> String -> String #

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

# wide-character-aware layout

charWidth :: Char -> Int #

Returns width of a character in a monospace font: 0 for a combining character, 1 for a regular character, 2 for an East Asian wide character. Ambiguous characters are treated as width 1.

strWidth :: String -> Int #

Alias for `realLength`.

strWidthAnsi :: String -> Int #

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.

takeWidth :: Int -> String -> String #

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