The idea for this was circling a lot lately in my head, so it was only a question of time for me to want to materialize that somehow. And I finally got the inspiration to write about it in some kind of blog, so here it is.

Lately I’ve switched companies and had to learn a new codebase. At my previous place I had been working for 7 years and we basically wrote all the code from scratch. That may not sound really interesting but that enabled me to comprehend 100% of the system. And now, in the era of AI-generated hundreds-to-thousands-line feature PRs, it obviously becomes much more difficult I’d say. This was a clear signal for me that only writing/reading code is not optimal enough and it’ll take an enormous amount of time to reach the comprehension level I aim for.

At this moment I remembered about using diagrams to represent code flows/schemes - though it always required so much discipline to prepare nice diagrams that really clarify things instead of complicating things even more. If only it wasn’t a manual job to prepare them, if only there was some magic box that produces good enough structured diagrams <wink>.

From draw.io to Mermaid#

Previously when working on diagrams I relied on UI tools like draw.io, so with a mouse and a browser I could visualize things. When smart-enough AI-agents appeared, I definitely felt a boost in scaling my throughput while also being able to spend time on the quality of my solutions. I tried to integrate them here, but at the time there weren’t any successful examples of agents in the browser and working with a draw.io .xml artifact just wasn’t as comfortable to me. The only close enough thing I could get was generated ASCII diagrams in agent TUIs, which is better than nothing but often they have problems with alignments I’d say :)

+--------+      +-------------+      +--------------+
| Client | ---> | API Gateway | ---> | Auth Service |
+--------+      +-------------+      +--------------+
                      |
                      v
                +---------------+      +----------+
                | Order Processing Service | ---> | Postgres |
                +---------------+      +----------+
                      |                      ^
                      v                    |
                +-----------+              |
                |   Redis   | ----------------+
                +-----------+

And then I remembered about mermaid. It lets me define a desired diagram using DSL and I’m able to view/review/edit it like simple markdown (for more details visit https://mermaid.js.org/intro). For example:

graph LR
    Client[Client] --> GW[API Gateway]
    GW --> Auth[Auth Service]
    GW --> Orders[Order Processing Service]
    Orders --> DB[Postgres]
    Orders --> Cache[Redis]
    Cache --> DB

You can render it on https://mermaid.live and see a nicely rendered diagram.

What that means is that now I could visualize different components of the system, which in turn allows me to assess the coupling, connascence and decide what parts of the system I can jump into to learn their actual implementation.

Bringing it into Neovim#

While occasionally rendering diagrams on mermaid.live was ok, my approach is to have things I can work with locally, so my agent and I can quickly iterate.

At this moment the fastest solution was to just search for a proper extension/plugin for your favorite editor and call it a day. Or for example you can use Obsidian to view those markdowns.

But, as I’m in my happy Neovim user phase, I was eager to explore what it would take to build something for my own use and whether I’d be productive using that tool.

The first problem for me was to determine how I should render mermaid. There were 3 options of getting a diagram rendered:

  • in the browser, with some kind of hot-reload. The browser would then be opened on an external monitor or right next to my terminal.
  • drawn using Terminal Graphics Protocols
  • in ASCII

The first 2 options are nice and powerful but for my use-case seemed a bit too much, so I explored the 3rd option. After some searching and digging I stopped at mermaid-ascii (I encourage you to check it out and leave a star). I usually like to build parsing and rendering myself in hobby projects, but mermaid-ascii solved this so cleanly that I happily went with it.

Out of the box you can just pipe text (or your clipboard) into it and get this:

$ pbpaste | mermaid-ascii

┌────────┐     ┌─────────────┐     ┌──────────────────────────┐     ┌──────────┐
│        │     │             │     │                          │     │          │
│ Client ├────►│ API Gateway ├────►│       Auth Service       │  ┌─►│ Postgres │
│        │     │             │     │                          │  │  │          │
└────────┘     └──────┬──────┘     └──────────────────────────┘  │  └──────────┘
                      │                                          │        ▲
                      │                                          │        │
                      │                          ┌───────────────┘        │
                      │                          │                        │
                      │                          │                        │
                      │            ┌─────────────┴────────────┐     ┌─────┴────┐
                      │            │                          │     │          │
                      └───────────►│ Order Processing Service ├────►│  Redis   │
                                   │                          │     │          │
                                   └──────────────────────────┘     └──────────┘

The final part was to think how I could use it more naturally. Since for me the most straight-forward way was to open a markdown file in Neovim, I decided to integrate it in my local config. Here’s the high-level concept:

  1. Gather all mermaid code blocks in markdown and fetch their content.
  2. Pass that content to mermaid-ascii.
  3. Render it in a split.

And that’s all. Implementation is only ~140 lines and can be checked out here (forgive me my Lua).

Closing thoughts#

Could I have just opened Obsidian or installed a bigger plugin? Sure - and honestly, after feeling burned out in recent years, that had become my default choice.

┌────────────────────────────────────┐
│                                    │
│ Deadline pressure + mental fatigue │◄┐
│                                    │ │
└──────────────────┬─────────────────┘ │
                   │                   │
                   ▼                   │
┌────────────────────────────────────┐ │
│                                    │ │
│       Install a lib / plugin       │ │
│                                    │ │
└──────────────────┬─────────────────┘ │
                   │                   │
                   ▼                   │
┌────────────────────────────────────┐ │
│                                    │ │
│   Patch & move on to "real" work   ├─┘
│                                    │
└────────────────────────────────────┘  

I’m not considering “work problems” less important, it’s just that over the years (even before the AI-era), I developed a habit of considering many things as “solved problems by really smart people” and something I should not invest too much effort into. If I spend too much time configuring my fonts in the terminal or best keybindings for tmux - I’ll lose the time I could spend increasing my market value as an engineer by developing domain expertise, etc.

Writing 140 messy lines of Lua code clearly wasn’t the “optimal” choice, just a genuinely fun one. Even after a decade in the industry, small tools like this can make your daily setup feel yours - and remind you why coding is fun in the first place.