You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 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
After playing around with it, I have a solution that should work on both Linux and MacOS without having to install any extra tools. It's kind of a dirty hack, but it's pure awk, tr, and sed (and optionally sort and uniq).
First of all, the language list needs to be parsed. The implementation of bat --list-languages wasn't really meant to be read by anything but a human, but with a little hackery, awk is perfectly capable of parsing it provided the following two properties are met:
The name of the first language is not the longest one in the list (breaks the double space assumption).
The name of the first language does not contain two consecutive spaces (same reason as above).
If these properties are met, the following awk script can be used:
NR==1 { # Run this for the first column.
dc =0# Set the column split counter to zero.while (substr($0, dc, 2) !="") dc++; # Skip past the language name column.while (substr($0, dc, 1) =="") dc++; # Skip past the whitespace that separates the columns.
};
{
printsubstr($0, dc) # Prints everything after the `dc` variable (i.e. only the second column).
}
After piping bat --list-languages to that script, the output will look something like this:
as
csv, tsv
applescript, script editor
s, S
adoc, asciidoc, asc
asa
yasm, nasm, asm, inc, mac
awk
bat, cmd
From here, we have two options: use the first entry, or just split them all. Since the first entry isn't always the most common extension for the language, it's safer to just use them all. That can be achieved by using tr to replace the commas with newlines, and sed to clean up the empty lines and garbage (leading/trailing) whitespace.
... | tr ',''\n'| sed 's/^ \+//; s/ \+$//; /^$/d'
After that, you can optionally sort and de-duplicate any languages using ... | sort | uniq.
Options:
If you want to exclude non-extension files, you can modify the sed patterns to do the following:
/^.\+\..\+/d - Delete any lines that match filename.ext but not .file
/^[A-Z]/d - Delete any lines that start with an uppercase letter (i.e. languages that are probably just config files).
The text was updated successfully, but these errors were encountered:
I was just taking a look through the fish completions, and I noticed the TODO at line 19.
bat/assets/completions/bat.fish
Lines 19 to 20 in e7c5561
After playing around with it, I have a solution that should work on both Linux and MacOS without having to install any extra tools. It's kind of a dirty hack, but it's pure
awk
,tr
, andsed
(and optionallysort
anduniq
).Explanation:
First of all, the language list needs to be parsed. The implementation of
bat --list-languages
wasn't really meant to be read by anything but a human, but with a little hackery,awk
is perfectly capable of parsing it provided the following two properties are met:If these properties are met, the following awk script can be used:
After piping
bat --list-languages
to that script, the output will look something like this:From here, we have two options: use the first entry, or just split them all. Since the first entry isn't always the most common extension for the language, it's safer to just use them all. That can be achieved by using
tr
to replace the commas with newlines, andsed
to clean up the empty lines and garbage (leading/trailing) whitespace.After that, you can optionally sort and de-duplicate any languages using
... | sort | uniq
.Options:
If you want to exclude non-extension files, you can modify the
sed
patterns to do the following:/^.\+\..\+/d
- Delete any lines that matchfilename.ext
but not.file
/^[A-Z]/d
- Delete any lines that start with an uppercase letter (i.e. languages that are probably just config files).The text was updated successfully, but these errors were encountered: