MCPcopy Create free account
hub / github.com/OpenDungeons/OpenDungeons / tryCompleteCommand

Method tryCompleteCommand

source/modes/ConsoleInterface.cpp:129–190  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

127}
128
129bool ConsoleInterface::tryCompleteCommand(const String_t& prefix, String_t& completedCmd)
130{
131 std::vector<const String_t*> matches;
132 for(auto& element : mCommandMap)
133 {
134 if(boost::algorithm::starts_with(element.first, prefix))
135 {
136 matches.push_back(&element.first);
137 }
138 }
139
140 if(matches.empty())
141 return false;
142
143 if(matches.size() == 1)
144 {
145 completedCmd = *(*matches.begin());
146 return true;
147 }
148
149 // There are several matches. We display them
150 for(auto match : matches)
151 {
152 const String_t& str = *match;
153 print(str);
154 }
155 print("\n");
156
157 // We try to complete until there is a difference between the matches in
158 // a way like Linux does
159 completedCmd = prefix;
160 std::size_t index = completedCmd.length();
161 // We take the first entry as reference
162 const String_t& refStr = *(*matches.begin());
163 while(index < refStr.length())
164 {
165 bool isDif = false;
166 for(auto match : matches)
167 {
168 const String_t& str = *match;
169
170 if(str.length() <= index)
171 {
172 isDif = true;
173 break;
174 }
175
176 if(str[index] != refStr[index])
177 {
178 isDif = true;
179 break;
180 }
181 }
182
183 if(isDif)
184 break;
185
186 completedCmd += refStr[index];

Callers 2

BOOST_AUTO_TEST_CASEFunction · 0.80
keyPressedMethod · 0.80

Calls

no outgoing calls

Tested by 1

BOOST_AUTO_TEST_CASEFunction · 0.64