lib: Refactor and improve comments for new mixed amount display functions.
This commit is contained in:
parent
9de238757b
commit
b39de5989f
@ -130,7 +130,6 @@ module Hledger.Data.Amount (
|
|||||||
import Control.Monad (foldM)
|
import Control.Monad (foldM)
|
||||||
import Data.Char (isDigit)
|
import Data.Char (isDigit)
|
||||||
import Data.Decimal (decimalPlaces, normalizeDecimal, roundTo)
|
import Data.Decimal (decimalPlaces, normalizeDecimal, roundTo)
|
||||||
import Data.Foldable (toList)
|
|
||||||
import Data.Function (on)
|
import Data.Function (on)
|
||||||
import Data.List (genericSplitAt, groupBy, intercalate, mapAccumL,
|
import Data.List (genericSplitAt, groupBy, intercalate, mapAccumL,
|
||||||
partition, sortBy)
|
partition, sortBy)
|
||||||
@ -679,8 +678,10 @@ showMixedUnnormalised showamt mmin mmax c (Mixed as) =
|
|||||||
}
|
}
|
||||||
|
|
||||||
elided = maybe id elideTo mmax astrs
|
elided = maybe id elideTo mmax astrs
|
||||||
elideTo m xs = maybeAppend (elisionDisplay mmax sepwidth (length long) =<< lastMay short) short
|
elideTo m xs = maybeAppend elisionStr short
|
||||||
where (short, long) = partition ((m>=) . adLength) xs
|
where
|
||||||
|
elisionStr = elisionDisplay (Just m) sepwidth (length long) $ lastDef nullAmountDisplay short
|
||||||
|
(short, long) = partition ((m>=) . adLength) xs
|
||||||
|
|
||||||
-- | General function to display a MixedAmount on a single line. It
|
-- | General function to display a MixedAmount on a single line. It
|
||||||
-- takes a function to display each Amount, an optional minimum width to
|
-- takes a function to display each Amount, an optional minimum width to
|
||||||
@ -707,20 +708,27 @@ showMixedOneLineUnnormalised showamt mmin mmax c (Mixed as) =
|
|||||||
pad = applyN (fromMaybe 0 mmin - width) (' ':)
|
pad = applyN (fromMaybe 0 mmin - width) (' ':)
|
||||||
|
|
||||||
elided = maybe id elideTo mmax astrs
|
elided = maybe id elideTo mmax astrs
|
||||||
elideTo m = addElide m . takeFitting m . withElided
|
elideTo m = addElide . takeFitting m . withElided
|
||||||
addElide m xs = fromMaybe (toList . elisionDisplay (Just m) 0 n $ AmountDisplay 0 "" 0 0) $ do
|
-- Add the last elision string to the end of the display list
|
||||||
eDisplay <- snd <$> lastMay xs
|
addElide [] = []
|
||||||
pure . maybeAppend eDisplay $ map fst xs
|
addElide xs = maybeAppend (snd $ last xs) $ map fst xs
|
||||||
|
-- Return the elements of the display list which fit within the maximum width
|
||||||
|
-- (including their elision strings)
|
||||||
takeFitting m = filter (\(_,e) -> maybe True ((m>=) . adTotal) e)
|
takeFitting m = filter (\(_,e) -> maybe True ((m>=) . adTotal) e)
|
||||||
. takeWhile (\(amt,_) -> adTotal amt <= m)
|
. takeWhile (\(amt,_) -> adTotal amt <= m)
|
||||||
withElided = zipWith (\m amt -> (amt, elisionDisplay Nothing sepwidth m amt)) [n-1,n-2..0]
|
-- Add the elision strings (if any) to each amount
|
||||||
|
withElided = zipWith (\num amt -> (amt, elisionDisplay Nothing sepwidth num amt)) [n-1,n-2..0]
|
||||||
|
|
||||||
data AmountDisplay = AmountDisplay
|
data AmountDisplay = AmountDisplay
|
||||||
{ adAmount :: !Amount
|
{ adAmount :: !Amount -- ^ Amount displayed
|
||||||
, adString :: !String
|
, adString :: !String -- ^ String representation of the Amount
|
||||||
, adLength :: !Int
|
, adLength :: !Int -- ^ Length of the string representation
|
||||||
, adTotal :: !Int
|
, adTotal :: !Int -- ^ Cumulative length of MixedAmount this Amount is part of,
|
||||||
}
|
-- including separators
|
||||||
|
} deriving (Show)
|
||||||
|
|
||||||
|
nullAmountDisplay :: AmountDisplay
|
||||||
|
nullAmountDisplay = AmountDisplay nullamt "" 0 0
|
||||||
|
|
||||||
amtDisplayList :: Int -> (Amount -> String) -> [Amount] -> [AmountDisplay]
|
amtDisplayList :: Int -> (Amount -> String) -> [Amount] -> [AmountDisplay]
|
||||||
amtDisplayList sep showamt = snd . mapAccumL display (-sep)
|
amtDisplayList sep showamt = snd . mapAccumL display (-sep)
|
||||||
@ -734,7 +742,7 @@ amtDisplayList sep showamt = snd . mapAccumL display (-sep)
|
|||||||
-- The string "m more", added to the previous running total
|
-- The string "m more", added to the previous running total
|
||||||
elisionDisplay :: Maybe Int -> Int -> Int -> AmountDisplay -> Maybe AmountDisplay
|
elisionDisplay :: Maybe Int -> Int -> Int -> AmountDisplay -> Maybe AmountDisplay
|
||||||
elisionDisplay mmax sep n lastAmt
|
elisionDisplay mmax sep n lastAmt
|
||||||
| n > 0 = Just $AmountDisplay 0 str len (adTotal lastAmt + len)
|
| n > 0 = Just $ AmountDisplay 0 str len (adTotal lastAmt + len)
|
||||||
| otherwise = Nothing
|
| otherwise = Nothing
|
||||||
where
|
where
|
||||||
fullString = show n ++ " more.."
|
fullString = show n ++ " more.."
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user