Skip to content

Unicode 2d drawing for Data.Tree #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Data/Tree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
module Data.Tree(
Tree(..), Forest,
-- * Two-dimensional drawing
drawTree, drawForest,
drawTree, drawForest, drawTreeU, drawForestU,
-- * Extraction
flatten, levels, foldTree,
-- * Building trees
Expand Down Expand Up @@ -144,6 +144,24 @@ draw (Node x ts0) = lines x ++ drawSubTrees ts0

shift first other = zipWith (++) (first : repeat other)

-- | Neat 2-dimensional drawing of a tree. Unicode paths
drawTreeU :: Tree String -> String
drawTreeU = unlines . drawU

-- | Neat 2-dimensional drawing of a forest. Unicode paths
drawForestU :: Forest String -> String
drawForestU = unlines . map drawTreeU

drawU :: Tree String -> [String]
drawU (Node x ts0) = x : drawSubTrees ts0
where
drawSubTrees [] = []
drawSubTrees [t] =
shift "\x2514\x2500" " " (draw t)
drawSubTrees (t:ts) =
shift "\x251c\x2500" "\x2502 " (draw t) ++ drawSubTrees ts

shift first other = zipWith (++) (first : repeat other)
-- | The elements of a tree in pre-order.
flatten :: Tree a -> [a]
flatten t = squish t []
Expand Down