ocramz / heidi

heidi : tidy data in Haskell
Other
27 stars 3 forks source link

pretty printing/ASCII visualization of encoded rows #2

Open ocramz opened 5 years ago

ocramz commented 5 years ago

e.g. ascii :

+-------------+-----------------+
| Person      | House           |
+-------+-----+-------+---------+
| Name  | Age | Color | Price   |
+-------+-----+-------+---------+
| David | 63  | Green | $170000 |
| Ava   | 34  | Blue  | $115000 |
| Sonia | 12  | Green | $150000 |
+-------+-----+-------+---------+

https://hackage.haskell.org/package/colonnade-1.2.0.1/docs/Colonnade.html#g:10

ocramz commented 4 years ago

generate CSV header from the type with generics-sop metadata

ocramz commented 4 years ago

After a fair bit of trial and error, header produces a simple representation of an arbitrary type which can be pretty-printed as a tree :

data A = MkA Int deriving (Eq, Show, G.Generic, Heidi)
data B = MkB Int Char deriving (Eq, Show, G.Generic, Heidi)
data B2 = MkB2 { b21 :: Int, b22 :: Char } deriving (Eq, Show, G.Generic, Heidi)
data C = MkC1 {c1 :: Int} | MkC2 A | MkC3 () deriving (Eq, Show, G.Generic, Heidi)
data R = MkR { r1 :: B2, r2 :: C , r3 :: B } deriving (Eq, Show, G.Generic, Heidi)
λ> printBox $ headerBox $ header (Proxy @R)
                       R                        
------------------------------------------------
                      MkR                       
                       R                        
------------*------------*----------------------
     r1     |     r3     |          r2          
     B2           B                 C           
 ----------   ----------   ------+------+------ 
    MkB2         MkB        MkC3 | MkC1 | MkC2  
     B2           B          ()    Int     A    
 ----*-----   ----*-----                  ---   
 b21 | b22     _0 |  _1                   MkA   
 Int   Char   Int   Char                  Int   
ocramz commented 4 years ago

In case someone else wants to move this forward, my thinking so far is as follows: