@@ -241,40 +241,58 @@ gridTable :: Monad m
241241 -> [[[Block]]]
242242 -> m Doc
243243gridTable opts blocksToDoc headless aligns widths headers rows = do
244+ -- the number of columns will be used in case of even widths
244245 let numcols = maximum (length aligns : length widths :
245246 map length (headers:rows))
247+ -- handleGivenWidths wraps the given blocks in order for them to fit
248+ -- in cells with given widths. the returned content can be
249+ -- concatenated with borders and frames
246250 let handleGivenWidths widths' = do
247251 let widthsInChars' = map (
248252 (\x -> if x < 1 then 1 else x) .
249253 (\x -> x - 3) . floor .
250254 (fromIntegral (writerColumns opts) *)
251255 ) widths'
252− rawHeaders' <- zipWithM blocksToDoc
253− (map (\w -> opts{writerColumns =
254− min (w - 2) (writerColumns opts)}) widthsInChars')
255− headers
256+ -- replace page width (in columns) in the options with a
257+ -- given width if smaller (adjusting by two)
258+ useWidth w = opts{writerColumns = min (w - 2) (writerColumns opts)}
259+ -- prepare options to use with header and row cells
260+ columnOptions = map useWidth widthsInChars'
261+ rawHeaders' <- zipWithM blocksToDoc columnOptions headers
256262 rawRows' <- mapM
257− (\cs -> zipWithM blocksToDoc
258− (map (\w -> opts{writerColumns =
259− min (w - 2) (writerColumns opts)}) widthsInChars')
260− cs)
263+ (\cs -> zipWithM blocksToDoc columnOptions cs)
261264 rows
262265 return (widthsInChars', rawHeaders', rawRows')
263− let handleZeroWidths = do
266+ -- handleFullWidths tries to wrap cells to the page width or even
267+ -- more in cases where `--wrap=none`. thus the content here is left
268+ -- as wide as possible
269+ let handleFullWidths = do
264270 rawHeaders' <- mapM (blocksToDoc opts) headers
265271 rawRows' <- mapM (mapM (blocksToDoc opts)) rows
266272 let numChars [] = 0
267273 numChars xs = maximum . map offset $ xs
268274 let widthsInChars' =
269275 map numChars $ transpose (rawHeaders' : rawRows')
276+ return (widthsInChars', rawHeaders', rawRows')
277+ -- handleZeroWidths calls handleFullWidths to check whether a wide
278+ -- table would fit in the page. if the produced table is too wide,
279+ -- it calculates even widths and passes the content to
280+ -- handleGivenWidths
281+ let handleZeroWidths = do
282+ (widthsInChars', rawHeaders', rawRows') <- handleFullWidths
270283 if sum widthsInChars' > writerColumns opts
271284 then -- use even widths
272285 handleGivenWidths
273286 (replicate numcols (1.0 / fromIntegral numcols) :: [Double])
274287 else return (widthsInChars', rawHeaders', rawRows')
275− (widthsInChars, rawHeaders, rawRows) <- if all (== 0) widths
276− then handleZeroWidths
277− else handleGivenWidths widths
288+ -- render the contents of header and row cells differently depending
289+ -- on command line options, widths given in this specific table, and
290+ -- cells' contents
291+ let handleWidths
292+ | (writerWrapText opts) == WrapNone = handleFullWidths
293+ | all (== 0) widths = handleZeroWidths
294+ | otherwise = handleGivenWidths widths
295+ (widthsInChars, rawHeaders, rawRows) <- handleWidths
278296 let hpipeBlocks blocks = hcat [beg, middle, end]
279297 where h = maximum (1 : map height blocks)
280298 sep' = lblock 3 $ vcat (replicate h (text " | "))