Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
darcs-hash:20081006025251-bd4d7-93da6f5a0c62181cacc074bfa7a0f87aa765c846.gz
  • Loading branch information
byorgey committed Oct 6, 2008
0 parents commit 4dad33d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Setup.lhs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env runhaskell
> import Distribution.Simple
> main = defaultMain
54 changes: 54 additions & 0 deletions Text/PrettyPrint/Boxes.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module Text.PrettyPrint.Boxes where

data Box = Box { rows :: Int
, cols :: Int
, content :: Content
}
deriving (Show)

data Content = Blank
| Text String
| Row [Box]
| Col [Box]
deriving (Show)

char :: Char -> Box
char c = Box 1 1 (Text [c])

text :: String -> Box
text t = Box 1 (length t) (Text t)

(<>) :: Box -> Box -> Box
l <> r = hcat [l,r]

(//) :: Box -> Box -> Box
l // r = vcat [l,r]

hcat :: [Box] -> Box
hcat bs = Box h w (Row $ map (padH h) bs)
where h = maximum . (0:) . map rows $ bs
w = sum . map cols $ bs

padH :: Int -> Box -> Box
padH h b | rows b < h = b // Box (h - rows b) (cols b) Blank
padH _ b = b

vcat :: [Box] -> Box
vcat bs = Box h w (Col $ map (padW w) bs)
where h = sum . map rows $ bs
w = maximum . (0:) . map cols $ bs

padW :: Int -> Box -> Box
padW w b | cols b < w = b <> Box (rows b) (w - cols b) Blank
padW _ b = b

render :: Box -> String
render b = unlines $ map (flip extractLine b) [0..rows b - 1]

extractLine :: Int -> Box -> String
extractLine _ (Box _ c Blank) = replicate c ' '
extractLine _ (Box _ _ (Text t)) = t
extractLine i (Box _ _ (Row bs)) = concatMap (extractLine i) bs
extractLine i (Box _ _ (Col [])) = ""
extractLine i (Box r c (Col (b:bs))) | i < rows b = extractLine i b
| otherwise = extractLine (i - rows b) (Box (r - rows b) c (Col bs))
11 changes: 11 additions & 0 deletions boxes.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: boxes
version: 0.0
synopsis: <Project description>
description: <Project description>
category: Text
license: BSD3
license-file: LICENSE
author: Brent Yorgey
maintainer: byorgey@gmail.com
build-Depends: base
ghc-options:

0 comments on commit 4dad33d

Please sign in to comment.