New completions: figlet, mdbook, ranger, pygmentize + xz fix #3378

Closed
wants to merge 9 commits into
from

Projects

None yet

4 participants

@moverest
Contributor

Add figlet, mdbook, ranger and pygmentize and fix xz completions.

moverest added some commits Sep 14, 2016
@faho faho and 1 other commented on an outdated diff Sep 14, 2016
share/completions/pygmentize.fish
+complete -c pygmentize -s F -d "Set filter" -x -a "(__fish_print_pygmentize filters Filter)"
+complete -c pygmentize -s S -d "Print style definition for given style" -x -a "(__fish_print_pygmentize styles Style)"
+complete -c pygmentize -s L -d "List lexers, formaters, styles or filters" -x -a "lexers formaters styles filters"
+complete -c pygmentize -s N -d "Guess and print lexer name based on given file"
+complete -c pygmentize -s H -d "Print detailed help" -x -a "lexer formatter filter"
+complete -c pygmentize -s v -d "Print detailed traceback on unhandled exceptions"
+complete -c pygmentize -s h -d "Print help"
+complete -c pygmentize -s V -d "Print package version"
+
+function __test_args
+ echo ... >> /tmp/args
+ __fish_print_cmd_args >> /tmp/args
+ return 1
+end
+
+complete -n "__test_args" -d Hello
@faho
faho Sep 14, 2016 Member

I believe you didn't want to include this.

@moverest
moverest Sep 14, 2016 Contributor

Indeed...

@faho faho and 1 other commented on an outdated diff Sep 14, 2016
share/completions/pygmentize.fish
@@ -0,0 +1,30 @@
+function __fish_print_pygmentize --argument-names type description
+ for line in (pygmentize -L $type | sed -e "s%^\* \(.*\):%\1%;tx;d;:x" | string split ", ")
@faho
faho Sep 14, 2016 Member

Any example output here? We might be able to use string instead of sed.

@moverest
moverest Sep 14, 2016 Contributor
> pygmentize -L filters
Pygments version 2.1.3, (c) 2006-2015 by Georg Brandl.

Filters:
--------
* gobble:
    Gobbles source code lines (eats initial characters).
* highlight:
    Highlight a normal Name (and Name.*) token with a different token type.
* tokenmerge:
    Merges consecutive tokens with the same token type in the output stream of a lexer.
* raiseonerror:
    Raise an exception when the lexer generates an error token.
* codetagify:
    Highlight special code tags in comments and docstrings.
* whitespace:
    Convert tabs, newlines and/or spaces to visible characters.
* keywordcase:
    Convert keywords to lowercase or uppercase or capitalize them, which means first letter uppercase, rest lowercase.

What it does is that it selects all lines that match the regex (lines beginning with * and containing a :) and extracts the names while discarding every lines that haven't been matched.

@moverest
moverest Sep 14, 2016 Contributor
> pygmentize -L filters | string match -r "\* (.*):"
* raiseonerror:
raiseonerror
* gobble:
gobble
* whitespace:
whitespace
* highlight:
highlight
* tokenmerge:
tokenmerge
* codetagify:
codetagify
* keywordcase:
keywordcase

It returns what I want and the entire line. I don't know how to get rid of it (without doing something horrible). :-/

@faho
faho Sep 14, 2016 edited Member

Well, that's easy - string match -r '^\* \S+:' | string replace -r '^\* (\S+):' '$1'. This would be another case where a "only-matching" option would be useful.

In this case, we can probably save that to a variable and use the following line as description.

Try this:

set -l lines (pygmentize -L $type | string match -r '^(?:\* |    ).*(?:)$' | string replace -r '\* (.*):$' '$1' | string trim)

while set -q lines[2]
    printf '%s\t%s\n' $lines[1] $lines[2]
    set -e lines[1]
    set -e lines[1]
end
@floam
Member
floam commented Sep 14, 2016 edited

Totally unrelated, but this thread made me notice that OS X comes with pygments, and pygmentize can highlight fish script by default. Neat.

Nope - something installed it into /usr/bin other than Apple, or pkgutil would show a pkgid here. Nevermind!

pkgutil --file-info /usr/bin/pygmentize
volume: /
path: /usr/bin/pygmentize
@moverest
Contributor

I really like this tool. I have defined an alias function cats which does pygmentize -g. It works quite well with fish scripts.

@floam
Member
floam commented Sep 14, 2016

fish_indent --ansi highlights better :) But it only does fish.

@moverest
Contributor

That's why I use pygmentize. I can throw everything at it without having to think much and the highlights are pretty good. It doesn't even need the extension to guess some languages.

@floam floam changed the title from Extend completion support to New completions: figlet, mdbook, ranger, pygmentize + xz fix Sep 15, 2016
share/completions/figlet.fish
@@ -0,0 +1,29 @@
+function __fish_print_figlet_fonts
+ find (figlet -I2) -type f ^ /dev/null | sed -e 's$/.*/\([^/.]*\)\.[ft]lf$\1\tFont$;tx;d;:x'
@faho
faho Sep 15, 2016 Member

What does figlet -I2 output?

This might be nicer with string, though it's something we can also do later.

@moverest
moverest Sep 16, 2016 Contributor
> figlet -I2
/usr/share/figlet/fonts
> find (figlet -I2) -type -f ^ /dev/null
/usr/share/figlet/fonts/slant.flf
/usr/share/figlet/fonts/ilhebrew.flc
/usr/share/figlet/fonts/banner.flf
/usr/share/figlet/fonts/646-jp.flc
/usr/share/figlet/fonts/646-irv.flc
/usr/share/figlet/fonts/646-es.flc
/usr/share/figlet/fonts/8859-4.flc
/usr/share/figlet/fonts/646-cn.flc
/usr/share/figlet/fonts/646-fr.flc
/usr/share/figlet/fonts/standard.flf
/usr/share/figlet/fonts/646-pt.flc
/usr/share/figlet/fonts/ivrit.flf
/usr/share/figlet/fonts/hz.flc
... and some more ...

We only want the name of the fonts (files with the extension .flf or .tlf). Here, that would be:

slant
banner
standard
... and some more ...
@moverest
moverest Sep 16, 2016 Contributor

This does the same thing:

function __fish_print_figlet_fonts
    set -l lines (find (figlet -I 2) -type f | string match -r '/.*/(.*)\.[ft]lc')

    while set -q lines[2]
        printf '%s\tFont\n' $lines[2]
        set -e lines[1]
        set -e lines[1]
    end
end
@faho
faho Sep 16, 2016 Member

How about

set -l dir (figlet -I 2)
set -l files $dir/*.flf $dir/*.tlf

printf '%s\tFont\n' (string replace -r '.*/([^/]+)\.[ft]lf' '$1' -- $files)

?

You might want to test the performance here. To do that, define the function, execute it and echo $CMD_DURATION right after. That'll give you the time in milliseconds.

@moverest
moverest Sep 16, 2016 edited Contributor

Your solution is much faster (yours: 4ms; sed: 12ms; find + match: 25ms). I'll commit it.

@faho
faho Sep 16, 2016 Member

So my intuition was correct. The trick, and this applies to all shells, is to use builtins unless you're dealing with large datasets, when specialized tools (GNU's grep is blindlingly fast) might make sense. There's a constant cost to forking off an external command.

@faho faho added this to the fish 2.4.0 milestone Sep 16, 2016
@faho
Member
faho commented Sep 16, 2016

Merged as ed7bf83..fc3cd77 (I squashed the improvements on top of the original commit).

Thanks!

@faho faho closed this Sep 16, 2016
@moverest
Contributor
moverest commented Sep 16, 2016 edited

I have to say that I really like to contribute here. I am always learning something new.
I may have some more in the near future (feh, aria2c).

@zanchey
Member
zanchey commented Sep 18, 2016

These commits have been lost somehow from master.

@zanchey zanchey reopened this Sep 18, 2016
@zanchey
Member
zanchey commented Sep 18, 2016

Fixed with 92e3a3c - sorry for the noise.

@zanchey zanchey closed this Sep 18, 2016
@moverest moverest deleted the unknown repository branch Sep 22, 2016
@moverest moverest restored the unknown repository branch Sep 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment