summaryrefslogtreecommitdiffstats
path: root/app/Pretty.hs
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2024-07-21 17:41:45 +0200
committerLibravatar Alexander Foremny <aforemny@posteo.de>2024-08-08 15:48:20 +0200
commit07acb8985844bf1df34eeab13abc0f6ca279d93f (patch)
treee71af8e80fdcc3d8e4c67a689f58558ddfeb78cf /app/Pretty.hs
parent85b4be2d0d4ea2c70883f026d1a5bde6230f626e (diff)
fix: depend on sensors
Diffstat (limited to 'app/Pretty.hs')
-rw-r--r--app/Pretty.hs108
1 files changed, 59 insertions, 49 deletions
diff --git a/app/Pretty.hs b/app/Pretty.hs
index 8054acc..d2988a9 100644
--- a/app/Pretty.hs
+++ b/app/Pretty.hs
@@ -1,3 +1,4 @@
+{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
{-# OPTIONS_GHC -fno-warn-unused-top-binds #-}
@@ -5,6 +6,7 @@ module Pretty
( Doc (..),
Pretty,
pretty,
+ Diagram,
diagram,
module Pretty.Color,
color,
@@ -28,55 +30,63 @@ instance Pretty Char where
instance Pretty String where
pretty = Lit Nothing
-diagram :: Int -> [Float] -> Doc
-diagram w xs = Col (map chart (discretize xs'))
- where
- xs' = replicate (2 * w - length xs) 0 ++ reverse (take (2 * w) xs)
-
- chart :: (Int, Int) -> Doc
- chart n = colorize n (pretty (chart' n))
-
- chart' (0, 0) = '⠀'
- chart' (0, 1) = '⢀'
- chart' (0, 2) = '⢠'
- chart' (0, 3) = '⢰'
- chart' (0, 4) = '⢸'
- chart' (1, 0) = '⡀'
- chart' (1, 1) = '⣀'
- chart' (1, 2) = '⣠'
- chart' (1, 3) = '⣰'
- chart' (1, 4) = '⣸'
- chart' (2, 0) = '⡄'
- chart' (2, 1) = '⣄'
- chart' (2, 2) = '⣤'
- chart' (2, 3) = '⣴'
- chart' (2, 4) = '⣼'
- chart' (3, 0) = '⡆'
- chart' (3, 1) = '⣆'
- chart' (3, 2) = '⣦'
- chart' (3, 3) = '⣶'
- chart' (3, 4) = '⣾'
- chart' (4, 0) = '⡇'
- chart' (4, 1) = '⣇'
- chart' (4, 2) = '⣧'
- chart' (4, 3) = '⣷'
- chart' (4, 4) = '⣿'
- chart' _ = error "chart': argument >4 (or <0)"
-
- colorize (n, m) = colorize' (max n m)
-
- colorize' 0 = colorDull Green
- colorize' 1 = color Green
- colorize' 2 = colorDull Yellow
- colorize' 3 = color Yellow
- colorize' 4 = color Red
- colorize' _ = error "colorize': argument >4 (or <0)"
-
- discretize :: [Float] -> [(Int, Int)]
- discretize [] = []
- discretize (_ : []) = []
- discretize (x1 : x2 : xs) =
- (round (x1 * 4), round (x2 * 4)) : discretize xs
+instance Pretty Float where
+ pretty = pretty . Diagram 1 . (: [])
+
+data Diagram a = Diagram Int a deriving (Functor, Show)
+
+diagram :: Int -> a -> Diagram a
+diagram = Diagram
+
+instance Pretty (Diagram [Float]) where
+ pretty (Diagram ((2 *) -> n) (take n -> xs)) = Col (map chart (discretize xs'))
+ where
+ xs' = replicate (n - length xs) 0 ++ xs
+
+ chart :: (Int, Int) -> Doc
+ chart n = colorize n (pretty (chart' n))
+
+ chart' (0, 0) = '⠀'
+ chart' (0, 1) = '⢀'
+ chart' (0, 2) = '⢠'
+ chart' (0, 3) = '⢰'
+ chart' (0, 4) = '⢸'
+ chart' (1, 0) = '⡀'
+ chart' (1, 1) = '⣀'
+ chart' (1, 2) = '⣠'
+ chart' (1, 3) = '⣰'
+ chart' (1, 4) = '⣸'
+ chart' (2, 0) = '⡄'
+ chart' (2, 1) = '⣄'
+ chart' (2, 2) = '⣤'
+ chart' (2, 3) = '⣴'
+ chart' (2, 4) = '⣼'
+ chart' (3, 0) = '⡆'
+ chart' (3, 1) = '⣆'
+ chart' (3, 2) = '⣦'
+ chart' (3, 3) = '⣶'
+ chart' (3, 4) = '⣾'
+ chart' (4, 0) = '⡇'
+ chart' (4, 1) = '⣇'
+ chart' (4, 2) = '⣧'
+ chart' (4, 3) = '⣷'
+ chart' (4, 4) = '⣿'
+ chart' x = error (show x)
+
+ colorize (n, m) = colorize' (max n m)
+
+ colorize' 0 = colorDull Green
+ colorize' 1 = color Green
+ colorize' 2 = colorDull Yellow
+ colorize' 3 = color Yellow
+ colorize' 4 = color Red
+ colorize' x = error (show x)
+
+ discretize :: [Float] -> [(Int, Int)]
+ discretize [] = []
+ discretize (_ : []) = []
+ discretize (x1 : x2 : xs) =
+ (round (x1 * 4), round (x2 * 4)) : discretize xs
color :: Color -> Doc -> Doc
color c = color' (Vivid, c)