Click on the top of the blue word code eggs

Plain ASCII text represents images, not just for B

Author: weishu

Blog: http://www.jianshu.com/u/e347b97e2f0c

We write code in plain text, and with Markdown we can write documents in plain text, but what about images, which are a more intuitive way of expressing information, in plain text?

By the way, have you ever seen a comment like this

ASCII art image

That’s right, the extreme ASCII annotation method is the subject of our discussion.

What are the benefits of using plain ASCII text to represent images? There are roughly the following points:

  1. B; There’s nothing to explain.

  2. You can use images to fully express information in code comments; No picture to say a jb? A picture is worth a thousand words. So far, there seems to be no IDE that supports placing images directly in code editing, as well as in other text-only situations. For example, RFC documents are TXT, many of which are pure ASCII representation.

  3. You think it’s just a plain text image? It can be converted to normal real images in PNG format and supports SVG vector images!

Well, isn’t it possible to say that some of markdown’s extended formats also support flowcharts? Flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; flowsheet; Yes, they can represent images in plain text, but they’re not real images; They cannot be embedded in text code and can only be seen visually after rendering.

Another person said, I know asciiflow, a site that can draw such a flow chart, perfect for my needs. But when you draw by hand, do you have to consider the details of the image? Size, placement, alignment? We should focus on the image itself, not how to draw it. Why is Markdown so easy to use? It’s because we don’t have to worry about the format of the document, what font, what level of headings, and so on, and we can just focus on the creation itself.

Let’s just say you agree with the merits of using ASCII to represent images, but… Does this diagram have to be typed out character by hand? If you do, don’t bother! Add a space in front of the light, all the following lines need to change; We need an automated tool.

Graph::Easy

Graph::Easy is today’s hero; It is a package in Perl that describes images directly using Perl code; Of course, we certainly don’t learn Perl to draw a graph;

The power of this package is that it defines a very simple DSL (Domain-specific Language) for describing images that we can use as code (rest assured, the syntax is very simple); You don’t have to worry about the layout of the image; The language is processed to produce ASCII images, which are placed directly in code comments; If necessary, it can also be converted to PNG or vector image format.

Here’s a simple example to get a feel for it:

The rendered ASCII diagram for this DSL looks like this:

02

The installation

  1. You need to install the Graphviz package, which you can download from graphviz’s website. Brew Install Graphviz for MAC users; For other Linux distributions, see the official website (http://www.graphviz.org/).

  2. Install perl; MAC and Linux users can skip it; If not, go to the Perl official website (http://www.perl.org/) to find out how to install it. It is said that there is a fool package activePerl under Windows; Please do your own search.

  3. Install the cpan; This is perl package management, like NPM, PIP, apt-get; On the MAC, enter cpan on the CLI, and then next. Other system reference cpan website (http://www.cpan.org/modules/INSTALL.html)

  4. Graph: installation: Easy; This step is easy; Enter cpan on the command line to enter the cpan shell. Then type Install Graph::Easy.

03

use

Use is divided into two steps

  1. Use the Graph::Easy DSL syntax to describe the image as a text file, such as simple.txt

  2. Use graph-easy to process this file: graph-easy simple.txt

The easiest way to use it is this; Of course, Graph::Easy doesn’t just support its own DSL syntax, it also supports more general image description languages like DOT; You can read dot input directly and produce other images in ASCII, PNG, and SVG formats.

04

grammar

annotation

Comments are expressed with #; Note that the # must be followed by a space; For historical reasons; Graph::Easy also uses the # color, which will fail parsing without a space.

The blank space

Whitespace usually has no effect, multiple null characters are combined into one, and newline null characters are ignored; The following statements are equivalent.


Node (the Node)

The nodes enclosed in brackets are simply shapes; For example, rectangles and circles in flow charts;

Multiple nodes can be separated by commas:

The code image above looks like this:

Edge (Edges)

What connects the nodes are edges; Graph::Easy’s DSL supports edges in several styles:

You can label the edges as follows:

The results are as follows:

Properties (Attributes)

You can add attributes to nodes and edges; Labels, directions, etc.; Attribute: value is displayed in braces {}, similar to the CSS attribute: value.

The DSL input above is as follows:

Graph::Easy provides a lot of attributes; In addition, the Graph: : Easy document (http://bloodgate.com/perl/graph/manual/index.html) is very detailed, suggest read through; Knowing the principles and details is a huge help in drawing and layout. I’ll translate it if I have time.

05

The instance

Is the syntax very simple? Armed with this knowledge, we can create our own flowcharts; Have a try! Try a schematic of the MVP mode

  • Create a new file vi mvp.txt. Enter the following code:

  • Save and exit; Run graph-easy Mvp. TXT and enter the following output:

Two lines of code! Automatic alignment, position adjustment, arrows, labels, etc. We don’t worry about how the graphics should be drawn at all, we focus on describing the graphics themselves; What are you waiting for?! Give it a try!!

Messages have benefits, please see the specific rules

“Help you develop good habits”