{"id":13837966,"url":"https://github.com/art-w/prettree","last_synced_at":"2026-01-18T08:20:51.723Z","repository":{"id":168016825,"uuid":"643638961","full_name":"art-w/prettree","owner":"art-w","description":"Pretty tree layout with an applicative functor","archived":false,"fork":false,"pushed_at":"2023-08-03T11:16:56.000Z","size":14,"stargazers_count":12,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-21T00:32:53.736Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"OCaml","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/art-w.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-05-21T19:43:08.000Z","updated_at":"2023-05-23T16:22:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"58be4d28-06ee-4b63-8d5e-a7e856e0e0cb","html_url":"https://github.com/art-w/prettree","commit_stats":null,"previous_names":["art-w/prettree"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/art-w/prettree","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/art-w%2Fprettree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/art-w%2Fprettree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/art-w%2Fprettree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/art-w%2Fprettree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/art-w","download_url":"https://codeload.github.com/art-w/prettree/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/art-w%2Fprettree/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264639844,"owners_count":23642313,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T15:01:31.989Z","updated_at":"2026-01-18T08:20:51.716Z","avatar_url":"https://github.com/art-w.png","language":"OCaml","funding_links":[],"categories":["OCaml"],"sub_categories":[],"readme":"![binary trees](https://art-w.github.io/prettree/bin.png)\n\nA small library to layout trees vertically in a pretty way: **[online documentation](https://art-w.github.io/prettree/prettree/Prettree)**\n- minimal width and height\n- parent centered above its children\n- nodes and leaves can have different sizes\n- custom padding between each subtrees\n\nNote that this library doesn't handle drawing the tree, it only computes the ideal position of every nodes and leaves!  You will need to define a function which traverses your custom tree, building a layout and its rendering function:\n\n```ocaml\ntype t = Leaf | Bin of t * t\n\nlet rec my_layout = function\n  | Leaf -\u003e\n      Prettree.make (leaf_width, leaf_height) @@ fun (x, y) -\u003e\n      (* TODO: draw a leaf at position (x, y) *)\n      (x, y) (* return the leaf position such that we can draw edges to it later *)\n  | Bin (left, right) -\u003e\n      Prettree.vert @@ (* parent is vertically over its children *)\n      let open Prettree.Syntax in\n      let+ (parent_x, parent_y) =\n        Prettree.make (parent_width, parent_height) @@ fun (x, y) -\u003e\n        (* TODO: draw the parent node at (x, y) *)\n        (x, y)\n      and+ () = Prettree.padding 1.0 (* vertical separation betwen parent and children *)\n      and+ (left_x, left_y), (right_x, right_y) =\n        Prettree.horz @@ (* siblings are horizontally aligned *)\n        let+ left_pos = my_layout left\n        and+ () = Prettree.padding 1.0 (* horizontal separation between left and right *)\n        and+ right_pos = my_layout right\n        in\n        left_pos, right_pos (* return children positions *)\n      in\n      (* TODO: draw edges from (parent_x, parent_y) to (left_x, left_y)\n                      and from (parent_x, parent_y) to (right_x, right_y)\n      *)\n      (parent_x, parent_y) (* return the parent position *)\n\nlet () =\n  (* compute our test tree layout *)\n  let test_layout = my_layout (Bin (Leaf, Leaf)) in\n  (* extract the layout to get the bounding box and our rendering function *)\n  let (tree_width, tree_height), my_render = Prettree.extract test_layout in\n  (* call our rendering function at some initial position (0,0) *)\n  let (root_x, root_y) = my_render (0.0, 0.0) in\n  (* ... *)\n```\n\n![varying node height and width](https://art-w.github.io/prettree/wh.png)\n\nThe `tests/` folder contains variations using `Graphics` to render the trees, and also unicode art:\n\n```\n                         ┏━━━━━━━━━━━┓\n                         ┃ fibonacci ┃\n                         ┗━━━━━┯━━━━━┛\n                               │\n                            ┏━━┷━┓\n                            ┃ 13 ┃\n                            ┗━━┯━┛\n          ┌────────────────────┴──────────────────┐\n        ┏━┷━┓                                   ┏━┷━┓\n        ┃ 5 ┃                                   ┃ 8 ┃\n        ┗━┯━┛                                   ┗━┯━┛\n   ┌──────┴───────┐                   ┌───────────┴───────────┐\n ┏━┷━┓          ┏━┷━┓               ┏━┷━┓                   ┏━┷━┓\n ┃ 2 ┃          ┃ 3 ┃               ┃ 3 ┃                   ┃ 5 ┃\n ┗━┯━┛          ┗━┯━┛               ┗━┯━┛                   ┗━┯━┛\n┌──┴──┐       ┌───┴────┐          ┌───┴────┐           ┌──────┴───────┐\n┷   ┏━┷━┓   ┏━┷━┓    ┏━┷━┓      ┏━┷━┓    ┏━┷━┓       ┏━┷━┓          ┏━┷━┓\n1   ┃ 1 ┃   ┃ 1 ┃    ┃ 2 ┃      ┃ 1 ┃    ┃ 2 ┃       ┃ 2 ┃          ┃ 3 ┃\n    ┗━┯━┛   ┗━┯━┛    ┗━┯━┛      ┗━┯━┛    ┗━┯━┛       ┗━┯━┛          ┗━┯━┛\n    ┌─┴─┐   ┌─┴─┐   ┌──┴──┐     ┌─┴─┐   ┌──┴──┐     ┌──┴──┐       ┌───┴────┐\n    ┷   ┷   ┷   ┷   ┷   ┏━┷━┓   ┷   ┷   ┷   ┏━┷━┓   ┷   ┏━┷━┓   ┏━┷━┓    ┏━┷━┓\n    0   1   0   1   1   ┃ 1 ┃   0   1   1   ┃ 1 ┃   1   ┃ 1 ┃   ┃ 1 ┃    ┃ 2 ┃\n                        ┗━┯━┛               ┗━┯━┛       ┗━┯━┛   ┗━┯━┛    ┗━┯━┛\n                        ┌─┴─┐               ┌─┴─┐       ┌─┴─┐   ┌─┴─┐   ┌──┴──┐\n                        ┷   ┷               ┷   ┷       ┷   ┷   ┷   ┷   ┷   ┏━┷━┓\n                        0   1               0   1       0   1   0   1   1   ┃ 1 ┃\n                                                                            ┗━┯━┛\n                                                                            ┌─┴─┐\n                                                                            ┷   ┷\n                                                                            0   1\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fart-w%2Fprettree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fart-w%2Fprettree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fart-w%2Fprettree/lists"}