Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Implement Tab Completion

I'm trying to figure out how to implement tab completion for subcommands in a C++ application. I would like it to function much like Git's tab completion. I'm trolling through Git's source, but it's not jumping out at me.

I've searched for ways to implement tab completion and haven't found a straight-forward answer, so I'm guessing it might not necessarily be a feature each individual application has to implement. Is tab completion a feature of the particular shell the application is being executed from? What are the basics I need to know about getting my application to support tab completion (particularly in C++)?

like image 353
E-rich Avatar asked Mar 10 '11 04:03

E-rich


People also ask

How do you use tab completion?

This feature can dramatically help you speed up typing commands. Just hit Tab while typing a command, option, or file name and the shell environment will automatically complete what you're typing or suggest options to you.

What is tab completion in Python?

Tab Completion and History Editing. Completion of variable and module names is automatically enabled at interpreter startup so that the Tab key invokes the completion function; it looks at Python statement names, the current local variables, and the available module names.


2 Answers

The question was answered in the comments.

Is tab completion a feature of the particular shell the application is being executed from?

yes

What are the basics I need to know about getting my application to support tab completion (particularly in C++)?

basically learn more about bash-completion

like image 172
E-rich Avatar answered Sep 28 '22 15:09

E-rich


I've searched for ways to implement tab completion and haven't found a straight-forward answer

Look at the code here. This should give you a pretty good starting point.

What are the basics I need to know about getting my application to support tab completion

You should be familiar with Trie data structure, as this is the common data structure used to implement tab completion. There are lots of tutorials explaining it online, look it up.

Pseudo-code (given a list of strings):

For each string in the list, store its characters in Trie data structure.

when the user hit tab key:

(GeeksForGeeks) Given a query prefix, we search for all words having this query.

  1. Search for given query using standard Trie search algorithm.
  2. If query prefix itself is not present, return -1 to indicate the same.
  3. If query is present and is end of word in Trie, print query. This can quickly checked by seeing if last matching node has isEndWord flag set. We use this flag in Trie to mark end of word nodes for purpose of searching.
  4. If last matching node of query has no children, return.
  5. Else recursively print all nodes under subtree of last matching node.
like image 38
SubMachine Avatar answered Sep 28 '22 16:09

SubMachine