A tree-rendering utility module for mq. Renders nested data (dicts, arrays, scalars) or flat path lists as ASCII tree art, similar to the Unix tree command.
- Render any tree node as ASCII tree art (
├──,└──,│) - Convert arbitrary mq values (dicts, arrays, scalars) into a tree
- Build a tree from a flat list of path strings (e.g. file paths,
ghq listoutput) - Minimal node representation (
{"label": ..., "children": [...]}) so trees can also be built by hand
Copy tree.mq to your mq module directory, or reference it with -L.
cp tree.mq ~/.local/mq/config/If mq was built with the http-import feature, you can import directly from GitHub without any local setup:
mq -I raw 'import "github.com/harehare/tree.mq" | tree::tree_render_paths(split(., "\n"))' files.txtPin to a specific release with @vX.Y.Z:
mq -I raw 'import "github.com/harehare/tree.mq@v0.1.0" | ...'mq -L /path/to/modules -I raw \
'import "tree" | tree::tree_render_paths(split(., "\n"))' files.txt| Function | Description |
|---|---|
tree_leaf(label) |
Creates a leaf node with no children |
tree_branch(label, children) |
Creates a branch node from a label and an array of child nodes |
| Function | Description |
|---|---|
tree_from_value(value, label="root") |
Recursively converts a dict, array, or scalar into a tree node |
tree_from_paths(paths, sep="/") |
Builds a tree node from an array of path strings, rooted at .. sep is matched as a regex (escape metacharacters, e.g. "\\.") |
| Function | Description |
|---|---|
tree_render(node) |
Renders a tree node as ASCII tree art |
| Function | Description |
|---|---|
tree_render_value(value, label="root") |
tree_render(tree_from_value(value, label)) in one call |
tree_render_paths(paths, sep="/") |
tree_render(tree_from_paths(paths, sep)) in one call |
mq -L . -I raw 'import "tree" | tree::tree_render_value({"name": "Alice", "pets": ["cat", "dog"]})'root
├── name: Alice
└── pets
├── [0]: cat
└── [1]: dog
mq -L . -I raw 'import "tree" | tree::tree_render_paths(["src/main.rs", "src/lib.rs", "src/util/foo.rs", "README.md"])'.
├── src
│ ├── main.rs
│ ├── lib.rs
│ └── util
│ └── foo.rs
└── README.md
mq -L . -I raw 'import "tree" | tree::tree_render(tree::tree_branch("root", [tree::tree_leaf("a"), tree::tree_branch("b", [tree::tree_leaf("c")])]))'root
├── a
└── b
└── c
MIT