Hledger.Utils.Text

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

Safe Haskell | None
Language | Haskell2010

Hledger.Utils.Text

Contents

- misc
- single-line layout
- multi-line layout
- wide-character-aware layout
- Reading
- tests

Description

Text formatting helpers, ported from String as needed. There may be better alternatives out there.

Synopsis

- textCapitalise :: Text -> Text
- textUnbracket :: Text -> Text
- wrap :: Text -> Text -> Text -> Text
- textChomp :: Text -> Text
- quoteIfSpaced :: Text -> Text
- textQuoteIfNeeded :: Text -> Text
- escapeDoubleQuotes :: Text -> Text
- escapeBackslash :: Text -> Text
- stripquotes :: Text -> Text
- textElideRight :: Int -> Text -> Text
- formatText :: Bool -> Maybe Int -> Maybe Int -> Text -> Text
- textConcatTopPadded :: [Text] -> Text
- textConcatBottomPadded :: [Text] -> Text
- fitText :: Maybe Int -> Maybe Int -> Bool -> Bool -> Text -> Text
- linesPrepend :: Text -> Text -> Text
- linesPrepend2 :: Text -> Text -> Text -> Text
- unlinesB :: [Builder] -> Builder
- data WideBuilder = WideBuilder {
  - wbBuilder :: !Builder
  - wbWidth :: !Int
  }
- wbToText :: WideBuilder -> Text
- wbFromText :: Text -> WideBuilder
- wbUnpack :: WideBuilder -> String
- textTakeWidth :: Int -> Text -> Text
- readDecimal :: Text -> Integer
- tests_Text :: TestTree

# misc

textCapitalise :: Text -> Text #

textUnbracket :: Text -> Text #

Remove all matching pairs of square brackets and parentheses from the text.

wrap :: Text -> Text -> Text -> Text #

Wrap a Text with the surrounding Text.

textChomp :: Text -> Text #

Remove trailing newlines/carriage returns.

quoteIfSpaced :: Text -> Text #

Wrap a string in double quotes, and -prefix any embedded single quotes, if it contains whitespace and is not already single- or double-quoted.

textQuoteIfNeeded :: Text -> Text #

escapeDoubleQuotes :: Text -> Text #

escapeBackslash :: Text -> Text #

stripquotes :: Text -> Text #

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

# single-line layout

textElideRight :: Int -> Text -> Text #

formatText :: Bool -> Maybe Int -> Maybe Int -> Text -> Text #

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

textConcatTopPadded :: [Text] -> Text #

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

textConcatBottomPadded :: [Text] -> Text #

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

fitText :: Maybe Int -> Maybe Int -> Bool -> Bool -> Text -> Text #

General-purpose wide-char-aware single-line text 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.

linesPrepend :: Text -> Text -> Text #

Add a prefix to each line of a string.

linesPrepend2 :: Text -> Text -> Text -> Text #

Add a prefix to the first line of a string, and a different prefix to the remaining lines.

unlinesB :: [Builder] -> Builder #

Join a list of Text Builders with a newline after each item.

# wide-character-aware layout

data WideBuilder #

Helper for constructing Builders while keeping track of text width.

Constructors

WideBuilder |
Fields wbBuilder :: !Builder wbWidth :: !Int

#### Instances

Instances details

Monoid WideBuilder # |
Instance details Defined in Text.WideString Methods mempty :: WideBuilder # mappend :: WideBuilder -> WideBuilder -> WideBuilder # mconcat :: [WideBuilder] -> WideBuilder #
Semigroup WideBuilder # |
Instance details Defined in Text.WideString Methods (<>) :: WideBuilder -> WideBuilder -> WideBuilder # sconcat :: NonEmpty WideBuilder -> WideBuilder # stimes :: Integral b => b -> WideBuilder -> WideBuilder #
Show WideBuilder # |
Instance details Defined in Text.WideString Methods showsPrec :: Int -> WideBuilder -> ShowS # show :: WideBuilder -> String # showList :: [WideBuilder] -> ShowS #

wbToText :: WideBuilder -> Text #

Convert a WideBuilder to a strict Text.

wbFromText :: Text -> WideBuilder #

Convert a strict Text to a WideBuilder.

wbUnpack :: WideBuilder -> String #

Convert a WideBuilder to a String.

textTakeWidth :: Int -> Text -> Text #

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

# Reading

readDecimal :: Text -> Integer #

Read a decimal number from a Text. Assumes the input consists only of digit characters.

# tests

tests_Text :: TestTree #