tty-tree
tty-tree copied to clipboard
Forward slashes break the display
Describe the problem
Everything before the last forward slash is lost.
Steps to reproduce the problem
puts TTY::Tree.new('users' => ['01/01/2022']).render
users
└── 2022
Describe your environment
- Ruby version: 2.6.5
- TTY::Tree version: tty-tree (0.4.0)
So here is the problem: https://github.com/piotrmurach/tty-tree/blob/5febe1647b4a7c77e2057da37f2b8fd5ded2fd03/lib/tty/tree/node.rb#L42
#basename is extracting the last part of the text. The conflict is that the README claims:
Print directory or structured data in a tree like format.
If this aims to print structured data, well this is a bug. If not is by design.
Hi Pablo,
Thanks for using tty-tree gem.
When I wrote this gem my focus was on processing and displaying info for directories. That's what I needed it for. In this context, files and dir names with forward slashes are invalid, or put other way, they indicate new directory level. So in this sense, this is not a bug.
Now, your expectation for printing a structured data is justifiable given my description. We could certainly assume that anytime hash gets provided we can take the data and render as is without any processing(e.i. skip walking down the directory structure). This would involve modifying the tree walking behaviour to only apply basename extraction for actual paths.
Would you have time to work on this and submit PR with tests? One way to consider solving this would be to introduce a new node type and use it in hash traversal.
Hi, sorry I took a quick look but seemed like it will take a solid couple of hours to do all that and I'm a bit low on time.
Just wanted to leave a link to https://github.com/physacco/ruby-cli-tree/ that is what I've finally ended up using if anyone gets across the same issue. Is just a much simpler tree printer.
we monkey patched the directory_renderer.rb to use node.path rather than node.name (which is the path basename) when printing the node
require 'tty-tree'
module TTY
class Tree
class DirectoryRenderer
private
def render_node(acc, node, pipe_mark, space_mark)
acc << node.prefix.gsub(/:pipe/, pipe_mark).gsub(/:space/, space_mark)
unless node.root?
acc << MARKERS[:unicode][node.leaf? ? :leaf : :branch]
acc << ' '
end
# acc << node.name.to_s + "\n"
acc << node.path.to_s + "\n"
end
end # DirRenderer
end # Tree
end # TTY
maybe the repo author would consider an option to print the path rather than name when calling the tree's render function