| 261 | } |
| 262 | |
| 263 | int prompt_loop(std::recursive_mutex * lock, CommandHistory & history) |
| 264 | { |
| 265 | raw_buffer.clear(); // make sure the buffer is empty! |
| 266 | size_t plen = prompt.size(); |
| 267 | raw_cursor = 0; |
| 268 | int history_index = 0; |
| 269 | // The latest history entry is always our current buffer, that |
| 270 | // initially is just an empty string. |
| 271 | const std::string empty; |
| 272 | history.add(empty); |
| 273 | |
| 274 | CONSOLE_SCREEN_BUFFER_INFO inf = { 0 }; |
| 275 | GetConsoleScreenBufferInfo(console_out, &inf); |
| 276 | size_t cols = inf.dwSize.X; |
| 277 | output(prompt.c_str(), plen, 0, inf.dwCursorPosition.Y); |
| 278 | inf.dwCursorPosition.X = (SHORT)plen; |
| 279 | SetConsoleCursorPosition(console_out, inf.dwCursorPosition); |
| 280 | |
| 281 | while (1) |
| 282 | { |
| 283 | INPUT_RECORD rec; |
| 284 | DWORD count; |
| 285 | lock->unlock(); |
| 286 | if (ReadConsoleInputA(console_in, &rec, 1, &count) == 0) { |
| 287 | lock->lock(); |
| 288 | return Console::SHUTDOWN; |
| 289 | } |
| 290 | lock->lock(); |
| 291 | if (rec.EventType != KEY_EVENT || !rec.Event.KeyEvent.bKeyDown) |
| 292 | continue; |
| 293 | switch (rec.Event.KeyEvent.wVirtualKeyCode) |
| 294 | { |
| 295 | case VK_RETURN: // enter |
| 296 | history.remove(); |
| 297 | return raw_buffer.size(); |
| 298 | case VK_BACK: // backspace |
| 299 | if (raw_cursor > 0 && raw_buffer.size() > 0) |
| 300 | { |
| 301 | raw_buffer.erase(raw_cursor-1,1); |
| 302 | raw_cursor--; |
| 303 | prompt_refresh(); |
| 304 | } |
| 305 | break; |
| 306 | case VK_LEFT: // left arrow |
| 307 | if (raw_cursor > 0) |
| 308 | { |
| 309 | raw_cursor--; |
| 310 | prompt_refresh(); |
| 311 | } |
| 312 | break; |
| 313 | case VK_RIGHT: // right arrow |
| 314 | if (raw_cursor != raw_buffer.size()) |
| 315 | { |
| 316 | raw_cursor++; |
| 317 | prompt_refresh(); |
| 318 | } |
| 319 | break; |
| 320 | case VK_UP: |