From 2780b38df2acdcf7ba3fc05a54282338553a65fe Mon Sep 17 00:00:00 2001 From: Tissevert Date: Thu, 9 May 2019 16:42:42 +0200 Subject: [PATCH] Add distinction between an empty node and a leaf to allow truncating the output right after a node --- src/Stream.hs | 1 + src/Tree.hs | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Stream.hs b/src/Stream.hs index 608f124..fbb8d9d 100644 --- a/src/Stream.hs +++ b/src/Stream.hs @@ -16,6 +16,7 @@ instance Functor Stream where Stream $ (\(a, stream) -> (f a, fmap f stream)) <$> pairs instance Tree Stream where + getStructure (Stream []) = Leaf getStructure (Stream pairs) = Node $ (\(s, stream) -> (s, getStructure stream)) <$> pairs diff --git a/src/Tree.hs b/src/Tree.hs index 43c3d05..2ca262d 100644 --- a/src/Tree.hs +++ b/src/Tree.hs @@ -3,7 +3,7 @@ module Tree ( , Structure(..) ) where -data Structure = Node [(String, Structure)] +data Structure = Leaf | Node [(String, Structure)] class Functor a => Tree a where getStructure :: a String -> Structure @@ -12,7 +12,8 @@ class Functor a => Tree a where draw = unlines . getLines . getStructure . fmap show getLines :: Structure -> [String] -getLines (Node []) = ["╼"] +getLines Leaf = ["╼"] +getLines (Node []) = [""] getLines (Node [(s, structure)]) = showBlock '─' (s, structure) getLines (Node ((s, structure):pairs)) = concat $ showBlock '┬' (s, structure) : showBlocks pairs