Graphviz和ASCII输出

83

能否使用Graphviz绘制ASCII图表?

就像这样:

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

得到了不期望的结果。

相反,我想获得类似的ASCII表示:

   this
  /    \
is      a
        |
       test

如何从dot文件格式绘制ASCII图表?


3
Python中的dot to ascii似乎很有前途。 - ggorlen
ADia 可能会有所帮助。 - pylover
5个回答

53

2
你可以从Graphviz导出JPEG,然后使用命令行工具进行转换,例如:jp2a --background=light --width=150。 - thamster
4
@thamster 这可能会导致文本难以辨认? - poolie
2
看起来有些人自己决定阻止版本特定的链接(http://szabgab.com/fixing-cpan-links-from-stack-overflow.html)。由于我发布的链接并不是版本特定的,而且它是CPAN本身 - 就让我们就此打住吧。在我的与版本无关的CPAN链接之前,我看到MetaCPAN已经失败了。 - spenthil
2
我认为社区最好添加额外的替代链接,而不是替换现有的链接。 - spenthil
10
  1. 安装 sudo apt-get install cpanminus
  2. 然后安装这个模块 sudo cpanm Graph::Easy
  3. 接下来你可以这样使用它:cat your.dot | graph-easy --from=dot --as_ascii
- gaRex
显示剩余5条评论

18

现在,在Ubuntu中,您可以直接安装和使用graph-easy:

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

> graph-easy dotfile.dot 
+----+     +------+
| is | <-- | this |
+----+     +------+
             |
             |
             v
           +------+
           |  a   |
           +------+
             |
             |
             v
           +------+
           | test |
           +------+

我不知道apt包,但我可以推荐graph-easy。它对于README.md文件非常有用。我希望它永远不会停止维护。 - Sridhar Sarnobat
我也没有意识到它可以直接从dot文件生成。 - Sridhar Sarnobat
1
同样适用于Debian。 - mirageglobe

15

这里是Linux的等效命令:

首先安装cpanminus

sudo apt install cpanminus
安装GraphEasy后。
sudo cpanm Graph::Easy

这里是一个使用示例

cat input.dot | graph-easy --from=dot --as_ascii

1
对于 sudo cpanm Graph::Easy,我得到了错误信息 ! Installing Graph::Easy failed. See /home/simon/.cpanm/work/1568099378.7482/build.log for details. Retry with --force to force install it. - Simon Streicher
1
从日志中可以看出,我的cpanm无法写入/usr/local/man。似乎是符号链接指向不存在的目录的问题:https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=583612#10。通过创建目录`sudo mkdir /usr/local/share/man/`来解决。 - Simon Streicher
谢谢,但我遇到了这个问题:“在 /usr/local/share/perl/5.26.1/Graph/Easy/Parser/Graphviz.pm 的第 928 行,尝试修改不可创建的数组值,下标为 -1,<STDIN> 块 1。”你有什么解决办法吗?有问题的代码行是 return if exists $self->{scope_stack}->[-1]->{_is_group}; - Petr Skocik
这应该被标记为正确答案,“graph-easy --from=dot --as_ascii”是回答问题的简短答案。 - Sridhar Sarnobat
从Ubuntu 20.04开始(可能在早期版本上也是如此,Debian上也很可能),可以通过sudo apt install libgraph-easy-perl直接安装graph-easy。 - nitrogen

12

11

通过Docker使用graph-easy。您可以安装whalebrew,并使用它运行graph-easy,而无需在本地计算机上安装太多依赖项,除了whalebrew和docker之外。

在MacOS上使用homebrew安装docker。

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

安装Whalebrew - https://github.com/whalebrew/whalebrew(检查其他安装方式)。
$ brew install whalebrew

通过Whalebrew安装Graph-Easy

$ whalebrew install tsub/graph-easy

现在通过以下方式运行它:

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

+---+     +---+
| a | --> | b |
+---+     +---+

对于那些没有使用Docker而选择cpanminus路线的人,请注意:请注意,如果您通过homebrew使用cpanm(cpanminus),它将调用依赖perl,该perl会安装到/usr/local/Cellar/perl/<perl version>/bin/graph-easy。因此,在每次perl版本升级时,您都将失去与graph-easy二进制文件的链接。您可以选择:(1)安装调用本地目录的软件包(brew info perl)或 (2)每次升级perl时重新安装所有软件包 - mirageglobe
我之前没有听说过 Whalebrew。如果你担心必须在 Docker 容器内运行某些东西,不用担心。Whalebrew 会为 Docker 镜像创建别名,这样你就可以像运行本地命令一样运行它们。 - Sridhar Sarnobat
1
@mirageglobe,感谢您向我展示了whalebrew。您提供的用例真是太棒了! - Greg Hilston

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接