Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: show a progress indicator during long autocompletion

I have a bash autocompletion function which consults a database to provide possible completions for the current command line in bash. It takes about 3 seconds to complete and during this time the user doesn't have any indication that he has triggered the autocompletion -- prompting him to press TAB a few more times, leading to the autocompletion possibly being run several more times.

Is it possible to manipulate the command line in a way to show that there is something being done, as soon as autocompletion is initiated? For example, when I press TAB twice after the command foo, I would like the following to happen immediately:

$ foo
autocompleting...

and then after the possible completions are determined, to change to:

$ foo
bar        baz        jazz

Alternatively, if I type foo j and press TAB once, the following:

$ foo j
autocompleting...

should change to

$ foo jazz

without any extra text beneath it, as expected.

The next step would be to consider if it's possible to have a dynamic output while the autocompletion is running, for example, printing the characters \ | / - in a single place, as if a line is rotating in place -- to visually indicate that something is happening. Would this be possible?

like image 921
Irfy Avatar asked Jan 28 '15 13:01

Irfy


1 Answers

Sure, it's possible, I think you'll find everything you need in this answer (so this might be considered a duplicate question): Using BASH to display a progress (working) indicator

Your question is slightly different in that you'll want to remove the spinner and then output your results. So you might consider inverting the process that is backgrounded. In other words, background your spinner, and then it can be explicitly killed when your autocomplete function is finished (but prior to outputting results if that's possible for your code).

like image 100
Mike D Avatar answered Sep 19 '22 06:09

Mike D