Autocomplete with C ++ (NOT with a shell)

I am trying to add an autocomplete function to my command line application. So far, all the answers say that this is a shell priority, but in my case it is different. My program goes into a loop (to receive commands) in main() , so I don't think this has anything to do with the shell. How can I achieve such a goal?

This is my program. It starts parsing when the user presses Enter ( std::getline() ). How can I get user input at runtime without using any external libraries?

 while (input != "exit") { std::cout << "\nCommand >> "; std::getline(std::cin, input); com.parse(input); } 
+6
source share
2 answers

As David Rodriguez said, using readline, GNU does the work (I had to use the library) on linux.

This is the official documentation, it has some C examples, but they are too confusing, so I used this to create custom autocomplete.

+2
source

You can use ReadConsoleInput to find out which keys will be pressed by the user. It can be found here: msdn: http://msdn.microsoft.com/en-us/library/windows/desktop/ms684961%28v=vs.85%29.aspx

Then you can use WriteConsole , which writes letters to the console from the current cursor position to try to autocomplete what the user types.

Then use SetConsoleCursorPosition back where the pointer was before your WriteConsole call. This will allow the user to continue typing where he left off. Just use WriteConsol to fill in the blanks to cancel autocompletion in case you make a mistake.

I don’t think that getline() will catch the letters from WriteConsole so that you can track what the user types, but also what you add using WriteConsole , or just keep track of which command he will write your thought and then call this if he presses Enter after you have suggested a command.

My last SetConsoleAttributes would be to use SetConsoleAttributes to change the color of the added row to light gray, to show the user that this is a sentence, not what he wrote.

+1
source

Source: https://habr.com/ru/post/950637/


All Articles