Skip to content

Commit

Permalink
Allow removing outer borders from TableStyles
Browse files Browse the repository at this point in the history
In TableStyle, split vertical separators into left, centre, and right variants.

groupV -> groupL, groupC, and groupR
headerV -> headerL, headerC, and headerR
  • Loading branch information
Xitian9 committed Sep 26, 2021
1 parent 604d09c commit d1bee05
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ main = putStrLn $ tableString [ column (expandUntil 30) left (charAlign ':') def
, asciiDoubleS
, asciiRoundS
, unicodeS
, withoutBorders unicodeS
, unicodeRoundS
, unicodeBoldS
, unicodeBoldStripedS
Expand Down
4 changes: 2 additions & 2 deletions src/Text/Layout/Table.hs
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,14 @@ tableLines specs TableStyle { .. } header rowGroups =
-- Vertical content lines
rowGroupLines = maybe concat (\seps -> intercalate [seps]) optGroupSepLine linesPerRowGroup
linesPerRowGroup = map rowGroupToLines rowGroups
rowGroupToLines = map (horizontalContentLine groupV) . applyRowMods . rows
rowGroupToLines = map (horizontalContentLine groupL groupC groupR) . applyRowMods . rows

-- Optional values for the header
(addHeaderLines, fitHeaderIntoCMIs, realTopH, realTopL, realTopC, realTopR)
= case header of
HeaderHS headerColSpecs hTitles
->
let headerLine = horizontalContentLine headerV (zipWith ($) headerRowMods hTitles)
let headerLine = horizontalContentLine headerL headerC headerR (zipWith ($) headerRowMods hTitles)
headerRowMods = zipWith3 headerCellModifier
headerColSpecs
cMSs
Expand Down
16 changes: 4 additions & 12 deletions src/Text/Layout/Table/Primitives/Table.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,12 @@ horizontalDetailLine
horizontalDetailLine hSpace delimL delimM delimR cells = mconcat . intersperse (stringB hSpace) $
stringB delimL : intersperse (stringB delimM) cells ++ [stringB delimR]

-- | A simplified version of 'hLineDetail' that will use the same delimiter
-- for everything.
hLine
:: StringBuilder b
=> String -- ^ The space characters that are used as padding.
-> String -- ^ The delimiter that is used for everything.
-> Row b -- ^ A row of builders.
-> b -- ^ The formatted line as a 'StringBuilder'.
hLine hSpace delim = horizontalDetailLine hSpace delim delim delim

-- | Render a line with actual content.
horizontalContentLine
:: StringBuilder b
=> String -- ^ The delimiter that is used for everything.
=> String -- ^ The delimiter that is used on the left side.
-> String -- ^ The delimeter that is used in between cells.
-> String -- ^ The delimeter that is used on the right side.
-> Row b -- ^ A row of builders.
-> b
horizontalContentLine = hLine " "
horizontalContentLine = horizontalDetailLine " "
86 changes: 71 additions & 15 deletions src/Text/Layout/Table/Style.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ data TableStyle = TableStyle
, headerTopR :: String
, headerTopC :: String
, headerTopH :: String
, headerV :: String
, groupV :: String
, headerL :: String
, headerR :: String
, headerC :: String
, groupL :: String
, groupR :: String
, groupC :: String
, groupSepH :: String
, groupSepC :: String
, groupSepLC :: String
Expand All @@ -29,6 +33,32 @@ data TableStyle = TableStyle
, groupBottomH :: String
}

-- | Remove the top, bottom, left, and right borders from a 'TableStyle'.
withoutBorders :: TableStyle -> TableStyle
withoutBorders = withoutTopBorder . withoutBottomBorder . withoutLeftBorder . withoutRightBorder

-- | Remove the top border from a 'TableStyle'.
withoutTopBorder :: TableStyle -> TableStyle
withoutTopBorder ts = ts { headerTopL = "", headerTopR = "", headerTopC = "", headerTopH = ""
, groupTopL = "", groupTopR = "", groupTopC = "", groupTopH = ""
}

-- | Remove the bottom border from a 'TableStyle'.
withoutBottomBorder :: TableStyle -> TableStyle
withoutBottomBorder ts = ts { groupBottomC = "", groupBottomL = "", groupBottomR = "", groupBottomH = "" }

-- | Remove the left border from a 'TableStyle'.
withoutLeftBorder :: TableStyle -> TableStyle
withoutLeftBorder ts = ts { headerTopL = "", headerSepLC = "", headerL = ""
, groupL = "", groupSepLC = "", groupTopL = "", groupBottomL = ""
}

-- | Remove the right border from a 'TableStyle'.
withoutRightBorder :: TableStyle -> TableStyle
withoutRightBorder ts = ts { headerTopR = "", headerSepRC = "", headerR = ""
, groupR = "", groupSepRC = "", groupTopR = "", groupBottomR = ""
}

-- | My usual ASCII table style.
asciiRoundS :: TableStyle
asciiRoundS = TableStyle
Expand All @@ -40,8 +70,12 @@ asciiRoundS = TableStyle
, headerTopR = "."
, headerTopC = "."
, headerTopH = "-"
, headerV = "|"
, groupV = "|"
, headerL = "|"
, headerR = "|"
, headerC = "|"
, groupL = "|"
, groupR = "|"
, groupC = "|"
, groupSepH = "-"
, groupSepC = "+"
, groupSepLC = ":"
Expand All @@ -67,8 +101,12 @@ asciiS = TableStyle
, headerTopR = "+"
, headerTopC = "+"
, headerTopH = "-"
, headerV = "|"
, groupV = "|"
, headerL = "|"
, headerR = "|"
, headerC = "|"
, groupL = "|"
, groupR = "|"
, groupC = "|"
, groupSepH = "-"
, groupSepC = "+"
, groupSepLC = "+"
Expand All @@ -94,8 +132,12 @@ asciiDoubleS = TableStyle
, headerTopR = "++"
, headerTopC = "++"
, headerTopH = "-"
, headerV = "||"
, groupV = "||"
, headerL = "||"
, headerR = "||"
, headerC = "||"
, groupL = "||"
, groupR = "||"
, groupC = "||"
, groupSepH = "-"
, groupSepC = "++"
, groupSepLC = "++"
Expand All @@ -121,8 +163,12 @@ unicodeS = TableStyle
, headerTopR = ""
, headerTopC = ""
, headerTopH = ""
, headerV = ""
, groupV = ""
, headerL = ""
, headerR = ""
, headerC = ""
, groupL = ""
, groupR = ""
, groupC = ""
, groupSepH = ""
, groupSepC = ""
, groupSepLC = ""
Expand All @@ -148,7 +194,9 @@ unicodeBoldHeaderS = unicodeS
, headerTopR = ""
, headerTopC = ""
, headerTopH = ""
, headerV = ""
, headerL = ""
, headerR = ""
, headerC = ""
}

-- | Same as 'unicodeS' but uses round edges.
Expand Down Expand Up @@ -178,8 +226,12 @@ unicodeBoldS = TableStyle
, headerTopR = ""
, headerTopC = ""
, headerTopH = ""
, headerV = ""
, groupV = ""
, headerL = ""
, headerR = ""
, headerC = ""
, groupL = ""
, groupR = ""
, groupC = ""
, groupSepH = ""
, groupSepC = ""
, groupSepLC = ""
Expand Down Expand Up @@ -209,8 +261,12 @@ unicodeDoubleFrameS = TableStyle
, headerTopR = ""
, headerTopC = ""
, headerTopH = ""
, headerV = ""
, groupV = ""
, headerL = ""
, headerR = ""
, headerC = ""
, groupL = ""
, groupR = ""
, groupC = ""
, groupSepH = ""
, groupSepC = ""
, groupSepLC = ""
Expand Down

0 comments on commit d1bee05

Please sign in to comment.