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

Provide useful output when bat is given a directory instead of a file #1940

Open
jasonkarns opened this issue Nov 9, 2021 · 6 comments
Open
Labels
feature-request New feature or request

Comments

@jasonkarns
Copy link

It's pretty common (for me) to run bat thing when thing is a directory. It's usually because I want to see what's in thing but (initially) I may not know it's a directory.

Since bat's purpose (in my words) is to be a more useful cat, I think it would be helpful to print more useful output when given –accidentally or otherwise– a directory. My initial suggestion would be to print the directory's contents as if via ls.

Granted, it still should be considered an error to invoke either cat and therefore also bat with a directory. So I would still consider the exit code to be 1; and it should continue to print [bat error]: 'thing' is a directory.

But bat could emit a file listing to STDOUT (or STDERR) along with the existing error message.

Thoughts?

@jasonkarns jasonkarns added the feature-request New feature or request label Nov 9, 2021
@Enselic
Copy link
Collaborator

Enselic commented Nov 9, 2021

Personally I would consider this to be out-of-scope for bat.

It would be easy to write a wrapper script that invokes ls if the argument is a dir and bat if the argument is a file. With some trickery you can even call that script bat and have it on your PATH.

@sharkdp
Copy link
Owner

sharkdp commented Nov 9, 2021

This has been requested before and I previously had a similar reply like @Enselic. On the other hand, I do have to admit that it also happens to me from time to time 😄. Maybe we could indeed print a simple directory listing. Similar to what vim <directory> shows, but without any of the controls. We could also call out to ls, but that comes with a lot of portability concerns (I do have some experience from fd --list-details).

But I'm also okay with closing this as "won't do". It's not really in the scope of bat.

@Enselic
Copy link
Collaborator

Enselic commented Nov 10, 2021

This has indeed been requested before. I could easily find three examples that have been closed: #1156, #971, #427.

Let's keep this one open then and see what happens :)

@keith-hall
Copy link
Collaborator

The tool mentioned at #800 (comment) also can provide this functionality.

@sharkdp
Copy link
Owner

sharkdp commented May 30, 2022

Potential solution: #2203

@kohane27
Copy link

I was having this exact issue and thanks to @keith-hall for linking to the awesome program rsop created by @desbma. Thank you to you two awesome people!

Here's my little guide for using rsop along with fzf.

In the fzf preview window, if it's a directory, it'll use lsd to preview the directory; if it's a file, it'll use bat to preview the file.

  1. Install rsop
  2. Copy config.toml.advanced to ~/.config/rsop/config.toml:
curl -o ~/.config/rsop/config.toml https://raw.githubusercontent.com/desbma/rsop/master/config/config.toml.advanced
  1. Use rsop in fzf like this: fzf --preview='rsp {}'
  2. Cheers!

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

No branches or pull requests

5 participants