{"id":18158254,"url":"https://github.com/piotrmurach/tty-tree","last_synced_at":"2025-04-06T19:13:02.814Z","repository":{"id":59158184,"uuid":"89731252","full_name":"piotrmurach/tty-tree","owner":"piotrmurach","description":"Print directory or structured data in a tree like format","archived":false,"fork":false,"pushed_at":"2024-03-07T22:38:55.000Z","size":75,"stargazers_count":57,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T17:11:09.382Z","etag":null,"topics":["directory","ruby","ruby-gem","tree-structure","tty","tty-components"],"latest_commit_sha":null,"homepage":"https://ttytoolkit.org","language":"Ruby","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/piotrmurach.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"piotrmurach"}},"created_at":"2017-04-28T17:54:34.000Z","updated_at":"2024-12-30T06:32:03.000Z","dependencies_parsed_at":"2024-03-07T23:31:22.105Z","dependency_job_id":"c9c43f89-847b-4d84-a646-94a191f34978","html_url":"https://github.com/piotrmurach/tty-tree","commit_stats":{"total_commits":81,"total_committers":3,"mean_commits":27.0,"dds":0.06172839506172845,"last_synced_commit":"d7f1008bbb3a34d6d6a01967e345afbdf9ebb205"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Ftty-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Ftty-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Ftty-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Ftty-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/piotrmurach","download_url":"https://codeload.github.com/piotrmurach/tty-tree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247535519,"owners_count":20954576,"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":["directory","ruby","ruby-gem","tree-structure","tty","tty-components"],"created_at":"2024-11-02T07:06:02.702Z","updated_at":"2025-04-06T19:13:02.785Z","avatar_url":"https://github.com/piotrmurach.png","language":"Ruby","funding_links":["https://github.com/sponsors/piotrmurach"],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://ttytoolkit.org\" target=\"_blank\"\u003e\u003cimg width=\"130\" src=\"https://github.com/piotrmurach/tty/raw/master/images/tty.png\" alt=\"TTY Toolkit logo\" /\u003e\u003c/a\u003e\n\u003c/div\u003e\n\n# TTY::Tree [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]\n\n[![Gem Version](https://badge.fury.io/rb/tty-tree.svg)][gem]\n[![Actions CI](https://github.com/piotrmurach/tty-tree/workflows/CI/badge.svg?branch=master)][gh_actions_ci]\n[![Build status](https://ci.appveyor.com/api/projects/status/q3s4gagj8xnospw4?svg=true)][appveyor]\n[![Code Climate](https://codeclimate.com/github/piotrmurach/tty-tree/badges/gpa.svg)][codeclimate]\n[![Coverage Status](https://coveralls.io/repos/github/piotrmurach/tty-tree/badge.svg)][coverage]\n[![Inline docs](http://inch-ci.org/github/piotrmurach/tty-tree.svg?branch=master)][inchpages]\n\n[gitter]: https://gitter.im/piotrmurach/tty\n[gem]: http://badge.fury.io/rb/tty-tree\n[gh_actions_ci]: https://github.com/piotrmurach/tty-tree/actions?query=workflow%3ACI\n[appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-tree\n[codeclimate]: https://codeclimate.com/github/piotrmurach/tty-tree\n[coverage]: https://coveralls.io/github/piotrmurach/tty-tree\n[inchpages]: http://inch-ci.org/github/piotrmurach/tty-tree\n\n\u003e Print directory or structured data in a tree like format.\n\n**TTY::Tree** provides independent directory or hash data rendering component for [TTY](https://github.com/piotrmurach/tty) toolkit.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem \"tty-tree\"\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install tty-tree\n\n## Contents\n\n* [1. Usage](#1-usage)\n* [2. Interface](#2-interface)\n  * [2.1 new](#21-new)\n    * [2.1.1 :level](#211-level)\n    * [2.1.2 :file_limit](#212-file_limit)\n    * [2.1.3 :show_hidden](#213-show_hidden)\n    * [2.1.4 :only_dirs](#214-only_dirs)\n  * [2.2 render](#22-render)\n    * [2.2.1 :indent](#221-indent)\n\n## 1. Usage\n\n`TTY::Tree` accepts as input a directory path:\n\n```ruby\ntree = TTY::Tree.new(Dir.pwd)\ntree = TTY::Tree.new(\"dir-name\")\n```\n\nIt can also be given as its input a `hash` data structure with keys representing directories and values as `array`s representing directory contents:\n\n```ruby\ndata = {\n  dir1: [\n    \"config.dat\",\n    {dir2: [\n      {dir3: [\"file3-1.txt\"]},\n      \"file2-1.txt\"\n    ]},\n    \"file1-1.txt\",\n    \"file1-2.txt\"\n  ]\n}\n\ntree = TTY::Tree.new(data)\n```\n\nYou can also construct tree with a DSL:\n\n```ruby\ntree = TTY::Tree.new do\n  node \"dir1\" do\n    node \"config.dat\"\n    node \"dir2\" do\n      node \"dir3\" do\n        leaf \"file3-1.txt\"\n      end\n      leaf \"file2-1.txt\"\n    end\n    node \"file1-1.txt\"\n    leaf \"file1-2.txt\"\n  end\nend\n```\n\nThe `TTY::Tree` can print the content in various formats. By default, a directory format is used by invoking `render`:\n\n```ruby\nputs tree.render\n# =\u003e\n# dir1\n# ├── config.dat\n# ├── dir2\n# │   ├── dir3\n# │   │   └── file3-1.txt\n# │   └── file2-1.txt\n# ├── file1-1.txt\n# └── file1-2.txt\n```\n\nThe `render` call returns a string and leaves it up to the consumer how to handle the tree-like output.\n\n## 2. Interface\n\n### 2.1 new\n\nIn order to create `TTY::Tree` you need to provide either a path to directory which can be a `String`, `Pathname` or `Dir`:\n\n```ruby\ntree = TTY::Tree.new(Dir.pwd)\ntree = TTY::Tree.new(\"dir-name\")\ntree = TTY::Tree.new(Pathname.pwd)\n```\n\nOr hash data structure:\n\n```ruby\ndata = {\n  dir1: [\n    \"config.dat\",\n    ...\n  ]\n}\n\ntree = TTY::Tree.new(data)\n```\n\nAs a shortcut notation, you can create a tree using `[]` like so:\n\n```ruby\ntree = TTY::Tree[Dir.pwd]\n```\n\nYou can also use DSL to build a tree by using `node` and `leaf` methods:\n\n```ruby\ntree = TTY::Tree.new do\n  node \"dir1\" do\n    node \"config.dat\"\n    node \"dir2\" do\n      node \"dir3\" do\n        leaf \"file3-1.txt\"\n      end\n      leaf \"file2-1.txt\"\n    end\n    node \"file1-1.txt\"\n    leaf \"file1-2.txt\"\n  end\nend\n```\n\n#### 2.1.1 `:level`\n\nThe maximum level of depth for this tree when parsing directory. The initial directory is treated as index `0`.\n\n```ruby\ntree = TTY::Tree.new(\"dir-name\", level: 2)\n# =\u003e parse directories as deep as 2 levels\n```\n\n#### 2.1.2 `:file_limit`\n\nPrevent `TTY::Tree` descending directories with more than a given number of entries:\n\n```ruby\ntree = TTY::Tree.new(\"dir-name\", file_limit: 2)\n```\n\n#### 2.1.3 `:show_hidden`\n\nIn order to for `TTY::Tree` to include hidden files in its output use `:show_hidden` option like so:\n\n```ruby\ntree = TTY::Tree.new(\"dir-name\", show_hidden: true)\n```\n\n#### 2.1.4 `:only_dirs`\n\nTo only display directory entries in the output use `:only_dirs` option:\n\n```ruby\ntree = TTY::Tree.new(\"dir-name\", only_dirs: true)\n```\n\nBy default, hidden directories are not included in the output. If you wish to show hidden directories as well do:\n\n```ruby\ntree = TTY::Tree.new(\"dir-name\", only_dirs: true, show_hidden: true)\n```\n\n### 2.2 render\n\nBy default, content is printed using `TTY::PathRenderer`. If you prefer a numeric notation of nested content, you can use `TTY::NumberRenderer` to enumerates each nested node like so:\n\n```ruby\nputs tree.render(as: :number)\n# =\u003e\n# dir1\n# 1.1 config.dat\n# 1.2 dir2\n#     2.3 dir3\n#         3.4 file3-1.txt\n#     2.5 file2-1.txt\n# 1.6 file1-1.txt\n# 1.7 file1-2.txt\n```\n\n#### 2.2.1 `:indent`\n\nThe number of spaces to use when indenting nested directories. By default, `4` spaces are used.\n\n```ruby\ntree.render(as: :dir, indent: 2)\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/tty-tree. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## Copyright\n\nCopyright (c) 2017 Piotr Murach. See LICENSE for further details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotrmurach%2Ftty-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpiotrmurach%2Ftty-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotrmurach%2Ftty-tree/lists"}