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

Add completions for --language when using the fish shell. #555

Merged
merged 4 commits into from
May 14, 2019

Conversation

eth-p
Copy link
Collaborator

@eth-p eth-p commented May 2, 2019

This a pull request to solve the TODO at line 19 in the fish shell completions.

# TODO: add parameter completion for available languages using option: -xka "(bat --list-languages | cat)"
# but replace 'cat' with some sed/awk like command that only outputs lines of valid options for this flag

The basic principle of the solution is the same as what I described in #553, and it has the same caveats, but this is a better implementation that only uses awk.

Features:

image

  • Lists all languages from bat --list-languages.
  • Displays the full name of the language.
  • Filters out languages that are probably config files (starts with an upper case letter).

Caveats:

In order to properly parse the output of bat --list-languages, two conditions need to be met with the first language that appears in the list.

  1. There is at least two spaces after the language name (i.e. is not the longest name).
  2. The language name does not contain two consecutive spaces in it.

@sharkdp
Copy link
Owner

sharkdp commented May 2, 2019

Awesome stuff :-)

While I really like your awk magic here, is there anything that we could do "inside" bat that would help with writing completion code like this? Maybe a (hidden) command-line option or subcommand that would print information like the supported languages or the list of themes in an easily parse-able way?

@eth-p
Copy link
Collaborator Author

eth-p commented May 3, 2019

One option would be to have bat --list-languages use the following format without line wrapping when printing to a non-terminal stdout:

language <tab> extension extension extension ...

This would be a lot easier to parse with awk:

BEGIN {
    FS = "\t";
}

{
    name = $1;
    split($2, exts, ' ')
    for (i in exts) {
        print exts[i]"\t"name; # For each extension, print a completion with the name of the language.
    }
}

@sharkdp
Copy link
Owner

sharkdp commented May 10, 2019

I'd be open to this change. I would probably use

language:ext1,ext2,ext3

as I am not a big fan of tab characters. I guess that should also be easy to parse.

That being said, I'm also okay with merging this PR as is - as long as we don't have the required change to --list-languages.

@eth-p
Copy link
Collaborator Author

eth-p commented May 10, 2019

I added the change to use the language:ext1,ext2,ext format for non-interactive printing.

To detect non-interactive printing, I decided to piggyback off of config.loop_through. Doing so required less changes, and it's behaviour would be more consistent with how bat works normally.

@sharkdp
Copy link
Owner

sharkdp commented May 14, 2019

Thank you very much!

To detect non-interactive printing, I decided to piggyback off of config.loop_through. Doing so required less changes, and it's behaviour would be more consistent with how bat works normally.

Yes, it's not perfect ("loop through" doesn't really make sense for --list-languages) but I guess it's fine here.

@sharkdp sharkdp merged commit 06b8dcb into sharkdp:master May 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants