Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward slashes break the display #5

Open
vizcay opened this issue Jan 3, 2022 · 4 comments
Open

Forward slashes break the display #5

vizcay opened this issue Jan 3, 2022 · 4 comments
Labels

Comments

@vizcay
Copy link

vizcay commented Jan 3, 2022

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)
@vizcay vizcay added the bug label Jan 3, 2022
@vizcay
Copy link
Author

vizcay commented Jan 9, 2022

So here is the problem:

@name = @path.basename

#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.

@piotrmurach
Copy link
Owner

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.

@vizcay
Copy link
Author

vizcay commented Sep 4, 2022

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.

@bpossolo
Copy link

bpossolo commented Dec 16, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants