git-annex/Utility/Dot.hs

64 lines
1.7 KiB
Haskell
Raw Normal View History

2011-02-04 02:44:17 +00:00
{- a simple graphviz / dot(1) digraph description generator library
-
- Copyright 2010 Joey Hess <id@joeyh.name>
2011-02-04 02:44:17 +00:00
-
- License: BSD-2-clause
2011-02-04 02:44:17 +00:00
-}
2011-07-06 00:36:43 +00:00
module Utility.Dot where -- import qualified
2011-02-04 02:44:17 +00:00
{- generates a graph description from a list of lines -}
graph :: [String] -> String
2011-02-04 04:36:36 +00:00
graph s = unlines $ [header] ++ map indent s ++ [footer]
2012-12-13 04:24:19 +00:00
where
header = "digraph map {"
footer= "}"
2011-02-04 02:44:17 +00:00
{- a node in the graph -}
graphNode :: String -> String -> String
2011-02-04 04:06:23 +00:00
graphNode nodeid desc = label desc $ quote nodeid
2011-02-04 02:44:17 +00:00
{- an edge between two nodes -}
graphEdge :: String -> String -> Maybe String -> String
2011-07-15 16:47:14 +00:00
graphEdge fromid toid desc = indent $ maybe edge (`label` edge) desc
2012-12-13 04:24:19 +00:00
where
edge = quote fromid ++ " -> " ++ quote toid
2011-02-04 02:44:17 +00:00
2011-02-04 04:06:23 +00:00
{- adds a label to a node or edge -}
label :: String -> String -> String
2011-07-15 16:47:14 +00:00
label = attr "label"
2011-02-04 02:44:17 +00:00
2011-02-04 04:06:23 +00:00
{- adds an attribute to a node or edge
- (can be called multiple times for multiple attributes) -}
attr :: String -> String -> String -> String
attr a v s = s ++ " [ " ++ a ++ "=" ++ quote v ++ " ]"
2011-02-04 02:44:17 +00:00
2011-02-04 04:06:23 +00:00
{- fills a node with a color -}
fillColor :: String -> String -> String
2011-07-15 16:47:14 +00:00
fillColor color s = attr "fillcolor" color $ attr "style" "filled" s
2011-02-04 02:44:17 +00:00
{- apply to graphNode to put the node in a labeled box -}
2011-02-08 22:17:46 +00:00
subGraph :: String -> String -> String -> String -> String
subGraph subid l color s =
"subgraph " ++ name ++ " {\n" ++
ii setlabel ++
2011-02-08 22:26:38 +00:00
ii setfilled ++
2011-02-08 22:17:46 +00:00
ii setcolor ++
ii s ++
indent "}"
2012-12-13 04:24:19 +00:00
where
-- the "cluster_" makes dot draw a box
name = quote ("cluster_" ++ subid)
setlabel = "label=" ++ quote l
setfilled = "style=" ++ quote "filled"
setcolor = "fillcolor=" ++ quote color
ii x = indent (indent x) ++ "\n"
2011-02-04 04:06:23 +00:00
2011-02-04 04:36:36 +00:00
indent ::String -> String
2011-07-15 16:47:14 +00:00
indent s = '\t' : s
2011-02-04 04:06:23 +00:00
quote :: String -> String
quote s = "\"" ++ s' ++ "\""
2012-12-13 04:24:19 +00:00
where
s' = filter (/= '"') s