| 65 | } |
| 66 | |
| 67 | std::string CommandsProcessor::TryCompleteCommand( const char* command_string ) const |
| 68 | { |
| 69 | const std::string command= ParseCommad( command_string ).first; |
| 70 | |
| 71 | std::vector<std::string> candidates; |
| 72 | |
| 73 | // Get matching commands. |
| 74 | for( unsigned int m= 0u; m < commands_maps_.size(); m++ ) |
| 75 | { |
| 76 | const CommandsMapConstPtr commads_map= commands_maps_[m].lock(); |
| 77 | if( commads_map != nullptr ) |
| 78 | { |
| 79 | for( const CommandsMap::value_type& command_value : *commads_map ) |
| 80 | { |
| 81 | const char* const str= command_value.first.c_str(); |
| 82 | if( std::strstr( str, command.c_str() ) == str ) |
| 83 | candidates.push_back( command_value.first ); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Get matching settings variables. |
| 89 | settings_.GetSettingsKeysStartsWith( command.c_str(), candidates ); |
| 90 | |
| 91 | if( candidates.size() == 0u ) |
| 92 | return command; |
| 93 | |
| 94 | // Print candidates. |
| 95 | if( candidates.size() > 1u ) |
| 96 | { |
| 97 | Log::Info( ">", command ); |
| 98 | std::sort( candidates.begin(), candidates.end() ); |
| 99 | for( const std::string& candidate : candidates ) |
| 100 | Log::Info( " ", candidate ); |
| 101 | } |
| 102 | |
| 103 | // Find common prefix for candidates. |
| 104 | unsigned int pos= command.size(); |
| 105 | while( 1u ) |
| 106 | { |
| 107 | if( pos >= candidates[0].size() ) |
| 108 | break; |
| 109 | |
| 110 | bool equals= true; |
| 111 | for( const std::string& candidate : candidates ) |
| 112 | if( pos >= candidate.size() || candidate[pos] != candidates[0][pos] ) |
| 113 | { |
| 114 | equals= false; |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | if( !equals ) |
| 119 | break; |
| 120 | pos++; |
| 121 | } |
| 122 | |
| 123 | return std::string( candidates[0].begin(), candidates[0].begin() + pos ); |
| 124 | } |
no test coverage detected