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

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

Closed
wants to merge 9 commits into from

Conversation

@moverest
Copy link
Contributor

moverest commented Sep 14, 2016

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

moverest added 5 commits Sep 14, 2016
The previous completion gave every files.
Here, we only show files with the .xz, .txz, .lzma or .tlz extension.
return 1
end

complete -n "__test_args" -d Hello

This comment has been minimized.

Copy link
@faho

faho Sep 14, 2016

Member

I believe you didn't want to include this.

This comment has been minimized.

Copy link
@moverest

moverest Sep 14, 2016

Author Contributor

Indeed...

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

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

Copy link
@moverest

moverest Sep 14, 2016

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

Copy link
@moverest

moverest Sep 14, 2016

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

Copy link
@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
moverest added 2 commits Sep 14, 2016
@floam
Copy link
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
Copy link
Contributor Author

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
Copy link
Member

floam commented Sep 14, 2016

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

@moverest
Copy link
Contributor Author

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 Extend completion support New completions: figlet, mdbook, ranger, pygmentize + xz fix Sep 15, 2016
@@ -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.

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

Copy link
@moverest

moverest Sep 16, 2016

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

Copy link
@moverest

moverest Sep 16, 2016

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

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

Copy link
@moverest

moverest Sep 16, 2016

Author Contributor

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

This comment has been minimized.

Copy link
@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
Copy link
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
Copy link
Contributor Author

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
Copy link
Member

zanchey commented Sep 18, 2016

These commits have been lost somehow from master.

@zanchey zanchey reopened this Sep 18, 2016
@zanchey
Copy link
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
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants
You can’t perform that action at this time.