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

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

Closed
wants to merge 9 commits into
from

Conversation

Projects
None yet
4 participants
@moverest
Contributor

moverest commented Sep 14, 2016

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

moverest added some commits Sep 14, 2016

Fix xz file completion
The previous completion gave every files.
Here, we only show files with the .xz, .txz, .lzma or .tlz extension.
Show outdated Hide outdated share/completions/pygmentize.fish
return 1
end
complete -n "__test_args" -d Hello

This comment has been minimized.

@faho

faho Sep 14, 2016

Member

I believe you didn't want to include this.

@faho

faho Sep 14, 2016

Member

I believe you didn't want to include this.

This comment has been minimized.

@moverest

moverest Sep 14, 2016

Contributor

Indeed...

@moverest

moverest Sep 14, 2016

Contributor

Indeed...

Show outdated Hide outdated 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 ", ")

This comment has been minimized.

@faho

faho Sep 14, 2016

Member

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

@faho

faho Sep 14, 2016

Member

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

This comment has been minimized.

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

This comment has been minimized.

@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). :-/

@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). :-/

This comment has been minimized.

@faho

faho Sep 14, 2016

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
@faho

faho Sep 14, 2016

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

This comment has been minimized.

Show comment
Hide comment
@floam

floam Sep 14, 2016

Member

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
Member

floam commented Sep 14, 2016

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

This comment has been minimized.

Show comment
Hide comment
@moverest

moverest Sep 14, 2016

Contributor

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

Contributor

moverest commented Sep 14, 2016

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

@floam

This comment has been minimized.

Show comment
Hide comment
@floam

floam Sep 14, 2016

Member

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

Member

floam commented Sep 14, 2016

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

@moverest

This comment has been minimized.

Show comment
Hide comment
@moverest

moverest Sep 14, 2016

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.

Contributor

moverest commented Sep 14, 2016

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

Show outdated Hide outdated 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'

This comment has been minimized.

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

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

This comment has been minimized.

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

This comment has been minimized.

@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
@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

This comment has been minimized.

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

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

This comment has been minimized.

@moverest

moverest Sep 16, 2016

Contributor

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

@moverest

moverest Sep 16, 2016

Contributor

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

This comment has been minimized.

@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 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

This comment has been minimized.

Show comment
Hide comment
@faho

faho Sep 16, 2016

Member

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

Thanks!

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

This comment has been minimized.

Show comment
Hide comment
@moverest

moverest Sep 16, 2016

Contributor

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

Contributor

moverest commented Sep 16, 2016

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

This comment has been minimized.

Show comment
Hide comment
@zanchey

zanchey Sep 18, 2016

Member

These commits have been lost somehow from master.

Member

zanchey commented Sep 18, 2016

These commits have been lost somehow from master.

@zanchey zanchey reopened this Sep 18, 2016

@zanchey

This comment has been minimized.

Show comment
Hide comment
@zanchey

zanchey Sep 18, 2016

Member

Fixed with 92e3a3c - sorry for the noise.

Member

zanchey commented Sep 18, 2016

Fixed with 92e3a3c - sorry for the noise.

@zanchey zanchey closed this Sep 18, 2016

@moverest moverest deleted the moverest:completions branch Sep 22, 2016

@moverest moverest restored the moverest:completions branch Sep 22, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment