Skip to content

Instantly share code, notes, and snippets.

@muesli4
Last active February 16, 2020 11:39
Show Gist options
  • Save muesli4/694e87b241dac6553b487aebfac4561e to your computer and use it in GitHub Desktop.
Save muesli4/694e87b241dac6553b487aebfac4561e to your computer and use it in GitHub Desktop.
Formatting cells
class Cell a where
-- | Drop a number of characters from the left side. Treats negative numbers
-- as zero.
dropLeft :: Int -> a -> a
-- | Drop a number of characters from the right side. Treats negative
-- numbers as zero.
dropRight :: Int -> a -> a
-- | Drop characters from both sides. Treats negative numbers as zero.
dropBoth :: Int -> Int -> a -> a
dropBoth l r = dropRight r . dropLeft l
-- | Returns the length of the visible characters as displayed on the
-- output medium.
visibleLength :: a -> Int
-- | Measure the preceeding and following characters
measureAlignment :: (Char -> Bool) -> a -> AlignInfo
buildCell :: StringBuilder b => a -> b
class Semigroup a => StringBuilder a where
stringB :: String -> a
charB :: Char -> a
replicateCharB :: Int -> Char -> a
instance StringBuilder String where
string = id
char = (: [])
replicateChar = replicate
instance Cell String where
dropLeft = drop
dropRight n = reverse . drop n . reverse
visibleLength = length
measureAlignment p xs = case break p xs of
(ls, s : rs) -> AlignInfo (length ls) $ Just (length rs)
(ls, []) -> AlignInfo (length ls) Nothing
buildCell = string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment