From 3496781f3ebad2901b60f19b388c893d8ad8aa37 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Thu, 7 Jul 2016 23:13:59 +0200 Subject: prettier output of distributions --- src/Main.hs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/Main.hs') diff --git a/src/Main.hs b/src/Main.hs index 75f379b..449e21d 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -56,6 +56,7 @@ import Sequence.Formula import Text.Layout.Table import Text.Read (readMaybe) +import Text.Printf import Data.Text (Text) import qualified Data.Text as Text @@ -382,8 +383,12 @@ printVal = withArg $ \formula -> withFocus $ \focusId -> do where outline Nothing = shellPutErrLn "No such value" outline (Just (Map.toList -> [(v, _)])) = shellPutStrLn $ show v - outline (Just (sortBy (comparing snd) . Map.toList -> vals)) = mapM_ (shellPutStrLn . outline') vals - outline' (v, prob) = pad' 3 (show $ round' 10 prob) ++ " → " ++ show v - round' :: RealFrac a => Float -> a -> Float - round' n = (/ n) . fromIntegral . (round :: RealFrac a => a -> Integer) . (* n) . realToFrac - pad' m ys = replicate (m - length (take m ys)) ' ' ++ ys + outline (Just (sortBy (comparing fst) . Map.toList -> vals)) = mapM_ (shellPutStrLn . outline') vals + where + barLength = 100 + outline' (v, prob) = printf "%*s: %.2f [%-*s]" + (maximum lengths) (show v) + (fromRational prob :: Double) + barLength (replicate (round $ fromInteger barLength * normalize prob) '#') + lengths = map (length . show . fst) vals + normalize p = p / maximum (map snd vals) -- cgit v1.2.3