lib: Introduce takeEnd to get rid of some reverse . take n . reverse.
This commit is contained in:
parent
73141aa645
commit
ed99aea7d5
@ -302,11 +302,10 @@ commentSpace = (" "++)
|
||||
-- appropriately bracketed/parenthesised for the given posting type.
|
||||
showAccountName :: Maybe Int -> PostingType -> AccountName -> String
|
||||
showAccountName w = fmt
|
||||
where
|
||||
fmt RegularPosting = take w' . T.unpack
|
||||
fmt VirtualPosting = parenthesise . reverse . take (w'-2) . reverse . T.unpack
|
||||
fmt BalancedVirtualPosting = bracket . reverse . take (w'-2) . reverse . T.unpack
|
||||
w' = fromMaybe 999999 w
|
||||
where
|
||||
fmt RegularPosting = maybe id take w . T.unpack
|
||||
fmt VirtualPosting = parenthesise . maybe id (takeEnd . subtract 2) w . T.unpack
|
||||
fmt BalancedVirtualPosting = bracket . maybe id (takeEnd . subtract 2) w . T.unpack
|
||||
|
||||
parenthesise :: String -> String
|
||||
parenthesise s = "("++s++")"
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
-- | String formatting helpers, starting to get a bit out of control.
|
||||
|
||||
module Hledger.Utils.String (
|
||||
takeEnd,
|
||||
-- * misc
|
||||
lowercase,
|
||||
uppercase,
|
||||
@ -57,6 +58,14 @@ import Text.Printf (printf)
|
||||
import Hledger.Utils.Parse
|
||||
import Hledger.Utils.Regex
|
||||
|
||||
|
||||
-- | Take elements from the end of a list.
|
||||
takeEnd n l = go (drop n l) l
|
||||
where
|
||||
go (_:xs) (_:ys) = go xs ys
|
||||
go [] r = r
|
||||
go _ [] = []
|
||||
|
||||
lowercase, uppercase :: String -> String
|
||||
lowercase = map toLower
|
||||
uppercase = map toUpper
|
||||
@ -86,7 +95,7 @@ stripbrackets = dropWhile (`elem` "([") . reverse . dropWhile (`elem` "])") . re
|
||||
|
||||
elideLeft :: Int -> String -> String
|
||||
elideLeft width s =
|
||||
if length s > width then ".." ++ reverse (take (width - 2) $ reverse s) else s
|
||||
if length s > width then ".." ++ takeEnd (width - 2) s else s
|
||||
|
||||
elideRight :: Int -> String -> String
|
||||
elideRight width s =
|
||||
|
||||
Loading…
Reference in New Issue
Block a user