Ascii Tree

repository·master·Indexed 41 days ago

https://github.com/spandanb/asciitree

A Python library for generating visual ASCII representations of arbitrary tree structures in the terminal. Features the `make_and_print_tree` function for visualization via custom value and children extractors, and `update_param` for configuring screen width, margins, and charsets. Supports content wrapping, node splitting, and pagination for large trees.

Tokens
836
Snippets
3
Records
4
Agent score
1%

What's inside asciitree

  1. How asciitree handles large trees

    master

    When a tree exceeds the configured screen_width, asciitree performs the following:

    1. Content Wrapping: Node contents are wrapped if they exceed the box_max_width.
    2. Node Splitting: If the tree is still too wide, it is split at a node. Some children will be rendered on one side of the split, and the remaining children on the other.
    3. Pagination: When a node is split, an information box is added as a child to the node indicating the 'page number' where the tree continues.
  2. Install asciitree

    master

    To install asciitree, clone the repository and run the setup script using python3.

    git clone ...
    cd ...
    python3 setup.py install
  3. Configure asciitree parameters

    master

    You can customize the visual output of the tree using the update_param function. This affects global settings for subsequent calls to make_and_print_tree.

    Supported parameters:

    • screen_width: The maximum character width assumed by the tree (default is 180).
    • margin: The distance between nodes.
    • padding: The distance between box contents and the border.
    • box_max_width: The maximum width of an individual node box.
    • charset: The character set used for drawing (e.g., 'ascii').
    from ascii_tree import update_param, make_and_print_tree
    
    # Set the character set to standard ASCII
    update_param('charset', 'ascii')
    
    # Adjust the assumed screen width
    update_param('screen_width', 120)
    
    # Adjust spacing and box constraints
    update_param('margin', 2)
    update_param('padding', 1)
    update_param('box_max_width', 30)
    
    make_and_print_tree(root, lambda n: n.val, lambda n: n.children)
  4. Print arbitrary trees with make_and_print_tree

    master

    The make_and_print_tree function allows you to visualize any tree structure. Because the library doesn't know the internal structure of your objects, you must provide two callback functions:

    1. A value extractor: A function that takes a node and returns its display value.
    2. A children extractor: A function that takes a node and returns a list of its child nodes.

    Example usage with a custom class:

    from ascii_tree import make_and_print_tree
    
    class NTreeNode:
        def __init__(self, val):
            self.val = val
            self.children = []
    
    root = NTreeNode('Root Node')
    root.children = [NTreeNode('Child 1'), NTreeNode('Child 2')]
    
    # Extract value using lambda or a function
    get_value = lambda n: n.val
    # Extract children using lambda or a function
    get_children = lambda n: n.children
    
    make_and_print_tree(root, get_value, get_children)