lib: Introduce takeEnd to get rid of some reverse . take n . reverse.
This commit is contained in:
parent
73141aa645
commit
ed99aea7d5
@ -303,10 +303,9 @@ commentSpace = (" "++)
|
|||||||
showAccountName :: Maybe Int -> PostingType -> AccountName -> String
|
showAccountName :: Maybe Int -> PostingType -> AccountName -> String
|
||||||
showAccountName w = fmt
|
showAccountName w = fmt
|
||||||
where
|
where
|
||||||
fmt RegularPosting = take w' . T.unpack
|
fmt RegularPosting = maybe id take w . T.unpack
|
||||||
fmt VirtualPosting = parenthesise . reverse . take (w'-2) . reverse . T.unpack
|
fmt VirtualPosting = parenthesise . maybe id (takeEnd . subtract 2) w . T.unpack
|
||||||
fmt BalancedVirtualPosting = bracket . reverse . take (w'-2) . reverse . T.unpack
|
fmt BalancedVirtualPosting = bracket . maybe id (takeEnd . subtract 2) w . T.unpack
|
||||||
w' = fromMaybe 999999 w
|
|
||||||
|
|
||||||
parenthesise :: String -> String
|
parenthesise :: String -> String
|
||||||
parenthesise s = "("++s++")"
|
parenthesise s = "("++s++")"
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
-- | String formatting helpers, starting to get a bit out of control.
|
-- | String formatting helpers, starting to get a bit out of control.
|
||||||
|
|
||||||
module Hledger.Utils.String (
|
module Hledger.Utils.String (
|
||||||
|
takeEnd,
|
||||||
-- * misc
|
-- * misc
|
||||||
lowercase,
|
lowercase,
|
||||||
uppercase,
|
uppercase,
|
||||||
@ -57,6 +58,14 @@ import Text.Printf (printf)
|
|||||||
import Hledger.Utils.Parse
|
import Hledger.Utils.Parse
|
||||||
import Hledger.Utils.Regex
|
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, uppercase :: String -> String
|
||||||
lowercase = map toLower
|
lowercase = map toLower
|
||||||
uppercase = map toUpper
|
uppercase = map toUpper
|
||||||
@ -86,7 +95,7 @@ stripbrackets = dropWhile (`elem` "([") . reverse . dropWhile (`elem` "])") . re
|
|||||||
|
|
||||||
elideLeft :: Int -> String -> String
|
elideLeft :: Int -> String -> String
|
||||||
elideLeft width s =
|
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 :: Int -> String -> String
|
||||||
elideRight width s =
|
elideRight width s =
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user