0

I have a function. How can I add to it an auto-completion with an available font name?

So if I type something like doSomethingWithFont Ubun<tab> it'll be completed to doSomethingWithFont Ubuntu\ Mono for example.

doSomethingWithFont () {
    echo $1
}

_completeWithFontName () {
    # ????
}

complete -F _completeWithFontName doSomethingWithFont
1
  • 1
    This sounds like two separate questions: (a) How to get a list of the available fonts and (b) how to create tab completion for that. Nov 20 at 12:19
0

You might want to use the fc_list command to list available fonts, in the format you want them to be entered.

This is how that fits into your sample code:

    _completeWithFontName() { < <(fc-list -f "%{family}\n" | grep "^$2" | sort -u) mapfile -t COMPREPLY; }
    complete -F _completeWithFontName doSomethingWithFont

Note that you should not pipe into the mapfile command: piping puts the destination command into a subshell, and you would loose the new contents of the COMPREPLY array immediately, on exit of that subshell. Redirecting < from a process substitution <(...) works better.

The -f parameter to fc-list sets the format, the simple grep command selects those font names that match the first characters entered on the command line, and the sort is for convenience. I also add a -u (unique) parameter to the sort because in this example I list only the families; each variant would appear as a duplicate, so we should sort them out.

7
  • Thank you. The problem only with spaces in font names. For example I have fonts with names : Samyak Devanagari, Samyak Gujarati, Samyak Malayalam, Samyak Tamil. The tab-completion will stop after the first space.
    – tehkonst
    Nov 21 at 9:19
  • That is an issue with all completions. You could, like you would do to match e.g. filenames with spaces in them, enter quoted prefixes, e.g. "Ubu<tab>; the complete functionality will handle that just fine, and even add a closing quote as soon as you <tab> when your prefix has gotten unique. Without quoting, you can escape spaces: Ubuntu\ Co<tab> will complete to Ubuntu\ Condensed.
    – db-inf
    Nov 21 at 9:30
  • File names don't have this issue, autocompletion escapes symbols automaticaly. I didn't think about starting argument with doublequote for proper completion, it's interesting.
    – tehkonst
    Nov 21 at 9:37
  • I assumed to much. It turns out that to escape spaces with a backslash and have Ubuntu\ Co<tab> complete to Ubuntu\ Condensed, you would have to make your function handle the backslash as an escape; the complete functionality passes it to your function in $2 as a plain backslash character followed by a space or whatever you entered. That means roughly remove unquoted backslashes, except for the 2nd of `\\` (if that ever happens in a fontname), and on output again replace spaces with escaped spaces.
    – db-inf
    Nov 21 at 9:42
  • @tehkonst About spaces and filename completion: that may depend on the files present. Try for instance touch "a file" "afile" "a document" and then try to complete ls a<tab> and ls a <tab> (a space after the 'a'). Interesting things happen, that clearly have been programmed to make them happen like that for a reason. Now it's up to you to do the same for your font completion.
    – db-inf
    Nov 21 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.