hledger-lib
Safe HaskellNone
LanguageGHC2021

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

capitalise :: String -> String Source #

lowercase :: String -> String Source #

uppercase :: String -> String Source #

underline :: String -> String Source #

stripbrackets :: String -> String Source #

quoteIfNeeded :: String -> String Source #

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

singleQuoteIfNeeded :: String -> String Source #

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

shellQuoteIfNeeded :: String -> String Source #

Quote a string if needed for use as one argument in a shell command line on the current platform. Uses double-quote escaping on Windows (cmd.exe does not recognise single quotes as a quoting character); elsewhere uses single quotes, which are literal in POSIX shells. Use this whenever interpolating a path or other string into a command that will be passed to runCommand, callCommand, or shell.

quoteForCommandLine :: String -> String Source #

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 "$"
"'$'"
>>> quoteForCommandLine "it's"
"'it'\\''s'"

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

Quote-aware version of words, splitting a string into words like the shell does: spaces inside single or double quotes don't split, quotes enclosing a word are removed, and a word can mix unquoted and quoted parts (so date:'1 to 15' is the single word date:1 to 15). Can raise an error if parsing fails (eg if there's an unclosed quote); wordsmay and wordsEither are total versions.

>>> words' "a b"
["a","b"]
>>> words' "'a b' c"
["a b","c"]
>>> words' "date:'1 to 15' x"
["date:1 to 15","x"]
>>> words' "\"it's\""
["it's"]
>>> words' "a '' b"
["a","","b"]
>>> wordsmay "an unclosed 'quote"
Nothing

wordsmay :: String -> Maybe [String] Source #

Like words', but return Nothing if parsing fails (eg because of an unclosed quote), rather than raising an error.

wordsEither :: String -> Either String [String] Source #

Like words', but on failure (eg because of an unclosed quote) return a pretty error message, showing the position and the problem, instead of raising an error.

stripQuotes :: String -> String Source #

Strip one matching pair of single or double quotes on the ends of a string.

isSingleQuoted :: String -> Bool Source #

isDoubleQuoted :: String -> Bool Source #

stripAnsi :: String -> String Source #

Strip ANSI escape sequences from a string.

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

single-line layout

strip :: String -> String Source #

Remove leading and trailing whitespace.

lstrip :: String -> String Source #

Remove leading whitespace.

rstrip :: String -> String Source #

Remove trailing whitespace.

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

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

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

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

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

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

chomp :: String -> String Source #

Remove all trailing newlines/carriage returns.

chomp1 :: String -> String Source #

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

singleline :: String -> String Source #

Remove consecutive line breaks, replacing them with single space

elideLeft :: Int -> String -> String Source #

elideRight :: Int -> String -> String Source #

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

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 #

strWidth :: String -> Int Source #

Alias for realLength.

strWidthAnsi :: String -> Int Source #

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 Source #

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