Basic tab-completition seems to be finished. It's working like requested in the old thread.

actorid
k1ll 14 years ago
parent 0bf2a2df41
commit ac43d482c5

@ -171,6 +171,22 @@ namespace MWGui
MyGUI::KeyCode key,
MyGUI::Char _char)
{
if( key == MyGUI::KeyCode::Tab)
{
std::vector<std::string> matches;
listNames();
command->setCaption(complete( command->getCaption(), matches ));
#if 0
int i = 0;
for(std::vector<std::string>::iterator it=matches.begin(); it < matches.end(); it++,i++ )
{
printOK( *it );
if( i == 50 )
break;
}
#endif
}
if(command_history.empty()) return;
// Traverse history with up and down arrows
@ -237,4 +253,80 @@ namespace MWGui
command->setCaption("");
}
std::string Console::complete( std::string input, std::vector<std::string> &matches )
{
using namespace std;
string output=input;
string tmp=input;
/* Does the input string contain things that don't have to be completed? If yes erase them. */
for(string::reverse_iterator rit=tmp.rbegin(); rit < tmp.rend(); rit++) {
if( *rit == ' ' ) {
if( rit == tmp.rbegin() )
{
tmp.clear();
break;
}
tmp.erase(tmp.begin(), (rit).base());
break;
}
}
/* Erase the input from the output string so we can easily append the completed form later. */
output.erase(output.end()-tmp.length(), output.end());
/* Is there still something in the input string? If not just display all commands and return the unchanged input. */
if( tmp.length() == 0 ) {
matches=mNames;
return input;
}
/* Iterate through the vector. */
for(vector<string>::iterator it=mNames.begin(); it < mNames.end();it++) {
bool string_different=false;
/* Is the string shorter than the input string? If yes skip it. */
if( (*it).length() < tmp.length() )
continue;
/* Is the beginning of the string different from the input string? If yes skip it. */
for( string::iterator iter=tmp.begin(), iter2=(*it).begin(); iter < tmp.end();iter++, iter2++) {
if( *iter != *iter2 ) {
string_different=true;
break;
}
}
if( string_different )
continue;
/* The beginning of the string matches the input string, save it for the next test. */
matches.push_back(*it);
}
/* There are no matches. Return the unchanged input. */
if( matches.empty() )
{
return input;
}
/* Only one match. We're done. */
if( matches.size() == 1 ) {
return output.append(matches.front() + string(" "));
}
/* Check if all matching strings match further than input. If yes complete to this match. */
int i = tmp.length();
for(string::iterator iter=matches.front().begin()+tmp.length(); iter < matches.front().end(); iter++, i++) {
for(vector<string>::iterator it=matches.begin(); it < matches.end();it++) {
if( (*it)[i] != *iter ) {
/* Append the longest match to the end of the output string*/
return output.append(matches.front().substr( 0, i));
}
}
}
/* All keywords match with the shortest. Append it to the output string and return it. */
return output.append(matches.front());
}
}

@ -80,6 +80,8 @@ namespace MWGui
MyGUI::Char _char);
void acceptCommand(MyGUI::EditPtr _sender);
std::string complete( std::string input, std::vector<std::string> &matches );
};
}
#endif

Loading…
Cancel
Save