Demonstrate creating a simple console window, with scrolling, filtering, completion and history. For the console example, we are using a more C++ like approach of declaring a class to hold both data and functions.
| 6261 | // Demonstrate creating a simple console window, with scrolling, filtering, completion and history. |
| 6262 | // For the console example, we are using a more C++ like approach of declaring a class to hold both data and functions. |
| 6263 | struct ExampleAppConsole |
| 6264 | { |
| 6265 | char InputBuf[256]; |
| 6266 | ImVector<char*> Items; |
| 6267 | ImVector<const char*> Commands; |
| 6268 | ImVector<char*> History; |
| 6269 | int HistoryPos; // -1: new line, 0..History.Size-1 browsing history. |
| 6270 | ImGuiTextFilter Filter; |
| 6271 | bool AutoScroll; |
| 6272 | bool ScrollToBottom; |
| 6273 | |
| 6274 | ExampleAppConsole() |
| 6275 | { |
| 6276 | ClearLog(); |
| 6277 | memset(InputBuf, 0, sizeof(InputBuf)); |
| 6278 | HistoryPos = -1; |
| 6279 | |
| 6280 | // "CLASSIFY" is here to provide the test case where "C"+[tab] completes to "CL" and display multiple matches. |
| 6281 | Commands.push_back("HELP"); |
| 6282 | Commands.push_back("HISTORY"); |
| 6283 | Commands.push_back("CLEAR"); |
| 6284 | Commands.push_back("CLASSIFY"); |
| 6285 | AutoScroll = true; |
| 6286 | ScrollToBottom = false; |
| 6287 | AddLog("Welcome to Dear ImGui!"); |
| 6288 | } |
| 6289 | ~ExampleAppConsole() |
| 6290 | { |
| 6291 | ClearLog(); |
| 6292 | for (int i = 0; i < History.Size; i++) |
| 6293 | free(History[i]); |
| 6294 | } |
| 6295 | |
| 6296 | // Portable helpers |
| 6297 | static int Stricmp(const char* s1, const char* s2) { int d; while ((d = toupper(*s2) - toupper(*s1)) == 0 && *s1) { s1++; s2++; } return d; } |
| 6298 | static int Strnicmp(const char* s1, const char* s2, int n) { int d = 0; while (n > 0 && (d = toupper(*s2) - toupper(*s1)) == 0 && *s1) { s1++; s2++; n--; } return d; } |
| 6299 | static char* Strdup(const char* s) { IM_ASSERT(s); size_t len = strlen(s) + 1; void* buf = malloc(len); IM_ASSERT(buf); return (char*)memcpy(buf, (const void*)s, len); } |
| 6300 | static void Strtrim(char* s) { char* str_end = s + strlen(s); while (str_end > s && str_end[-1] == ' ') str_end--; *str_end = 0; } |
| 6301 | |
| 6302 | void ClearLog() |
| 6303 | { |
| 6304 | for (int i = 0; i < Items.Size; i++) |
| 6305 | free(Items[i]); |
| 6306 | Items.clear(); |
| 6307 | } |
| 6308 | |
| 6309 | void AddLog(const char* fmt, ...) IM_FMTARGS(2) |
| 6310 | { |
| 6311 | // FIXME-OPT |
| 6312 | char buf[1024]; |
| 6313 | va_list args; |
| 6314 | va_start(args, fmt); |
| 6315 | vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args); |
| 6316 | buf[IM_ARRAYSIZE(buf)-1] = 0; |
| 6317 | va_end(args); |
| 6318 | Items.push_back(Strdup(buf)); |
| 6319 | } |
| 6320 |
nothing calls this directly
no test coverage detected