-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
darcs-hash:20081006025251-bd4d7-93da6f5a0c62181cacc074bfa7a0f87aa765c846.gz
- Loading branch information
0 parents
commit 4dad33d
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env runhaskell | ||
> import Distribution.Simple | ||
> main = defaultMain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |