Skip to content

Commit ad2ea4e

Browse files
committed
Display table as proper markdown.
Markdown doesn't do multiline headings unless you have an explicit break in the header.
1 parent cb90d11 commit ad2ea4e

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/Data/DataFrame/Display/Terminal/PrettyPrint.hs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@ showTable header types rows =
5050
border = T.intercalate "---" [T.replicate width (T.singleton '-') | width <- widths]
5151
separator = T.intercalate "-|-" [T.replicate width (T.singleton '-') | width <- widths]
5252
fillCols fill cols = T.intercalate " | " [fill c width col | (c, width, col) <- zip3 cs widths cols]
53-
in T.unlines $ border : fillCols colTitleFill header : separator : fillCols colTitleFill types : separator : map (fillCols colValueFill) rows
53+
in T.unlines $ border : fillCols colTitleFill header : separator : fillCols colTitleFill types : separator : map (fillCols colValueFill) rows
54+
55+
showTableProperMarkdown :: [T.Text] -> [T.Text] -> [[T.Text]] -> T.Text
56+
showTableProperMarkdown header types rows =
57+
let headerWithTypes = zipWith (\h t -> h <> "<br>" <> t) header types
58+
cs = map (\h -> ColDesc center h left) headerWithTypes
59+
widths = [maximum $ map T.length col | col <- transpose $ headerWithTypes : rows]
60+
border = T.intercalate "---" [T.replicate width (T.singleton '-') | width <- widths]
61+
separator = T.intercalate "-|-" [T.replicate width (T.singleton '-') | width <- widths]
62+
fillCols fill cols = T.intercalate " | " [fill c width col | (c, width, col) <- zip3 cs widths cols]
63+
in T.unlines $ border : fillCols colTitleFill headerWithTypes : separator : map (fillCols colValueFill) rows

src/Data/DataFrame/Internal/DataFrame.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ instance Eq DataFrame where
3939

4040
instance Show DataFrame where
4141
show :: DataFrame -> String
42-
show d = T.unpack (asText d)
42+
show d = T.unpack (asText d False)
4343

44-
asText :: DataFrame -> T.Text
45-
asText d =
44+
asText :: DataFrame -> Bool -> T.Text
45+
asText d properMarkdown =
4646
let header = "index" : map fst (sortBy (compare `on` snd) $ M.toList (columnIndices d))
4747
types = V.toList $ V.filter (/= "") $ V.map getType (columns d)
4848
getType Nothing = ""
@@ -68,7 +68,7 @@ asText d =
6868
rows =
6969
transpose $
7070
zipWith (curry (V.toList . getTextColumnFromFrame d)) [0..] header
71-
in showTable header ("Int":types) rows
71+
in (if properMarkdown then showTableProperMarkdown else showTable) header ("Int":types) rows
7272

7373
-- | O(1) Creates an empty dataframe
7474
empty :: DataFrame

0 commit comments

Comments
 (0)