80

Is it possible to draw ASCII diagram using Graphviz?

Something like that:

digraph
{
  this -> is
  this -> a
  a -> test
}

Gives undesired result.

Instead, I would like to get similar ASCII representation:

   this
  /    \
is      a
        |
       test

How to draw ascii diagrams from dot-files format?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user360872
  • 1,261
  • 4
  • 15
  • 15

5 Answers5

52

If you are not perl averse, graph-easy (and the associated Graph::Easy package) can do exactly that:

http://search.cpan.org/~tels/Graph-Easy/

http://search.cpan.org/~tels/Graph-Easy/bin/graph-easy

On Mac you can install this with Homebrew and cpan:

brew install cpanminus
cpan Graph::Easy

It's easy to invoke after installation:

cat dotfile.dot   | /opt/local/libexec/perl5.12/sitebin/graph-easy
maxymoo
  • 35,286
  • 11
  • 92
  • 119
spenthil
  • 3,373
  • 29
  • 17
  • 2
    you could always export a jpeg from graphviz then convert with command line tools like: jp2a --background=light --width=150 – thamster Mar 26 '13 at 22:38
  • 3
    @thamster that'll probably make the text illegible? – poolie Dec 18 '13 at 06:34
  • @user3501996 why did you change the links? – Andreas Lyngstad Apr 05 '14 at 19:39
  • No clue why they did it - rolling back. – spenthil Apr 05 '14 at 22:38
  • 2
    Looks like some folks are talking it upon themselves to prevent version specific links (http://szabgab.com/fixing-cpan-links-from-stack-overflow.html). Since the links I posted aren't version specific and it is CPAN itself - lets leave it at that. *I see MetaCPAN failing well before my version-agnostic CPAN links do.* – spenthil Apr 08 '14 at 17:28
  • 2
    I think the community would be better served adding additional alternative links rather than replacing the existing ones. – spenthil Apr 08 '14 at 17:33
  • I found it very very useful, thanks @spenthil and thanks to the author of Graph::Easy – David Costa Feb 23 '16 at 10:12
  • 10
    1. Install `sudo apt-get install cpanminus`. 2. Then this module `sudo cpanm Graph::Easy`. 3. Then you can use it as `cat your.dot | graph-easy --from=dot --as_ascii` – gaRex Jun 29 '16 at 10:26
  • For anyone trying the example in the question, remove the newline before the `{` — see this bug in graph-easy from 2010: https://rt.cpan.org/Public/Bug/Display.html?id=63061 – ShreevatsaR Dec 19 '18 at 16:00
  • Unfortunately the graph-easy does not support subgraphs. – dosmanak Jun 03 '21 at 14:24
17

By now, in ubuntu, you can install and use graph-easy directly:

> sudo apt install libgraph-easy-perl
[...]

> graph-easy dotfile.dot 
+----+     +------+
| is | <-- | this |
+----+     +------+
             |
             |
             v
           +------+
           |  a   |
           +------+
             |
             |
             v
           +------+
           | test |
           +------+
Christian Fritz
  • 20,641
  • 3
  • 42
  • 71
15

Here is equivalent commands for linux:

First install cpanminus

sudo apt install cpanminus

After you can install GraphEasy

sudo cpanm Graph::Easy

Here is a sample usage

cat input.dot | graph-easy --from=dot --as_ascii
Cpp Forever
  • 900
  • 8
  • 16
  • 1
    For `sudo cpanm Graph::Easy` I get the error `! Installing Graph::Easy failed. See /home/simon/.cpanm/work/1568099378.7482/build.log for details. Retry with --force to force install it.` – Simon Streicher Sep 10 '19 at 07:10
  • 1
    From the logs, my cpanm can't write to `/usr/local/man`. Seems like a symlink to a nonexisting directory problem: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=583612#10 . Fixed by creating the directory `sudo mkdir /usr/local/share/man/` . – Simon Streicher Sep 10 '19 at 07:23
  • Thanks, but I'm getting `Modification of non-creatable array value attempted, subscript -1 at /usr/local/share/perl/5.26.1/Graph/Easy/Parser/Graphviz.pm line 928, chunk 1.` with this. Any idea on how to fix it? The offending line is `return if exists $self->{scope_stack}->[-1]->{_is_group};`. – Petr Skocik Aug 11 '20 at 12:23
  • This should be marked the right answer, `graph-easy --from=dot --as_ascii` is the short answer to the question – Sridhar Sarnobat Aug 10 '21 at 22:12
  • As of Ubuntu 20.04 (possibly earlier and likely on Debian as well), it's possible to install graph-easy directly with `sudo apt install libgraph-easy-perl` – nitrogen Oct 28 '21 at 21:55
12

Another option to use Graph::Easy's ASCII functionality is directly from your browser through this small service that I hosted:

https://dot-to-ascii.ggerganov.com

Georgi Gerganov
  • 1,006
  • 9
  • 17
10

Using graph-easy via docker. You can install whalebrew and use it to run graph-easy without installing too much dependancies on your local machine other than whalebrew and docker.

on MacOS with homebrew install docker

$ brew install docker
$ docker -v      # check if docker is running

Install whalebrew - https://github.com/whalebrew/whalebrew (check installation alternatives)

$ brew install whalebrew

Install graph-easy via whalebrew

$ whalebrew install tsub/graph-easy

Now run it via

$ echo '[a]->[b]' | graph-easy

+---+     +---+
| a | --> | b |
+---+     +---+
mirageglobe
  • 2,446
  • 2
  • 24
  • 30
  • Note for those going cpanminus route without docker : do note that if you use cpanm (cpanminus) via homebrew, it will invoke dependancy perl which installs to /usr/local/Cellar/perl//bin/graph-easy. thus on each version upgrade of perl, you will lose the link to graph-easy binary. You can either (1) install packages invoking a local dir (brew info perl) or (2) reinstall all packages everytime you upgrade perl – mirageglobe Apr 28 '19 at 08:01
  • I haven't heard of whalebrew until this. If you're scared of having to run things inside a docker container, don't be. `Whalebrew creates aliases for Docker images so you can run them as if they were native commands` – Sridhar Sarnobat Aug 10 '21 at 19:18
  • 1
    @mirageglobe Thank you for showing me `whalebrew`. What a wonderful use case you've provided! – Greg Hilston May 04 '22 at 17:31