GitHub - justindmassey/diamond: tree cli

3 min read Original article ↗

Diamond is an interactive tool for growing and exploring trees using dot-paths.

You type paths. Diamond inserts them into a tree.

If a path causes no insertion, Diamond prints the matching endpoint subtrees.

The tree is stored as indented text in diamond.tree.


Usage

cd diamond
npm install
node .

Example

Input:

person.name.first
person.name.last
person.address.city

Tree:

person
    name
        first
        last
    address
        city

Paths

Each line is a dot-separated path.

means:

Missing segments are created automatically.


Fan-out

Diamond’s key feature is fan-out.

If the first segment is non-empty, Diamond searches the entire tree for nodes with that name and applies the rest of the path to all matches.

Example tree:

company
    person
        name
            first
school
    person
        name
            first

Input:

Result:

company
    person
        name
            first
        address
            city
school
    person
        name
            first
        address
            city

If no matching seed nodes exist, Diamond creates the full path starting from the root.


Root-anchored paths

A leading . anchors the path at the root and disables fan-out.

This follows the path strictly from the root.


Printing

Diamond prints results only when nothing was inserted.

When printing:

  • The endpoint subtree is printed.
  • If multiple matches occur, the full path to the endpoint is printed in magenta.

Example query:

Output:

person.name
name
    first
    last

Wildcards

* matches any node at that level.

Example tree:

people
    alice
        name
            first
    bob
        name
            first

Input:

Result:

people
    alice
        name
            first
        address
            city
    bob
        name
            first
        address
            city

The wildcard fans out over existing children only. It does not create nodes by itself.


Forced append

Prefix a segment with + to always create a new node.

This appends a new note node even if one already exists. The . in front of the + is optional.


Wrap operator

A trailing + wraps the existing children of a node.

creates a new tasks node under project and moves all previous children of project under that new node.

Example:

Before

project
    task1
    task2
    task3

Command

After

project
    tasks
        task1
        task2
        task3

Existing nodes with the same name are wrapped as well.

Before

project
    task1
    tasks
        archived
    task2

Command

After

project
    tasks
        task1
        tasks
            archived
        task2

If more segments follow, the dot after the wrap operator must be written explicitly:


Deletion

Prefix a path with - to delete matching nodes.

Special case:

Clears the entire tree.


Splice operator

A trailing - removes matching nodes but keeps their children.

Example:

Before

person
    address
        city
        zip
    name
        first

Command

After

person
    city
    zip
    name
        first

The address node is removed and its children are spliced into the parent.


Variables

Variables can be assigned with:

Example:

field=name
person.field.first

becomes:

Remove a variable:

Variables exist only during the current session.


Inspecting the root

Entering a single dot prints all top-level subtrees:

Pressing enter on an empty line clears the screen and prints the tree.


Storage

Diamond stores the tree in:

Format:


Philosophy

Diamond is designed to be:

  • minimal
  • interactive
  • structurally expressive

You grow a tree by typing paths.

Sometimes you insert structure. Sometimes you discover what already exists.