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.
| 6590 | // Demonstrate creating a simple console window, with scrolling, filtering, completion and history. |
| 6591 | // For the console example, we are using a more C++ like approach of declaring a class to hold both data and functions. |
| 6592 | struct ExampleAppConsole |
| 6593 | { |
| 6594 | char InputBuf[256]; |
| 6595 | ImVector<char*> Items; |
| 6596 | ImVector<const char*> Commands; |
| 6597 | ImVector<char*> History; |
| 6598 | int HistoryPos; // -1: new line, 0..History.Size-1 browsing history. |
| 6599 | ImGuiTextFilter Filter; |
| 6600 | bool AutoScroll; |
| 6601 | bool ScrollToBottom; |
| 6602 | |
| 6603 | ExampleAppConsole() |
| 6604 | { |
| 6605 | IMGUI_DEMO_MARKER("Examples/Console"); |
| 6606 | ClearLog(); |
| 6607 | memset(InputBuf, 0, sizeof(InputBuf)); |
| 6608 | HistoryPos = -1; |
| 6609 | |
| 6610 | // "CLASSIFY" is here to provide the test case where "C"+[tab] completes to "CL" and display multiple matches. |
| 6611 | Commands.push_back("HELP"); |
| 6612 | Commands.push_back("HISTORY"); |
| 6613 | Commands.push_back("CLEAR"); |
| 6614 | Commands.push_back("CLASSIFY"); |
| 6615 | AutoScroll = true; |
| 6616 | ScrollToBottom = false; |
| 6617 | AddLog("Welcome to Dear ImGui!"); |
| 6618 | } |
| 6619 | ~ExampleAppConsole() |
| 6620 | { |
| 6621 | ClearLog(); |
| 6622 | for (int i = 0; i < History.Size; i++) |
| 6623 | free(History[i]); |
| 6624 | } |
| 6625 | |
| 6626 | // Portable helpers |
| 6627 | static int Stricmp(const char* s1, const char* s2) { int d; while ((d = toupper(*s2) - toupper(*s1)) == 0 && *s1) { s1++; s2++; } return d; } |
| 6628 | 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; } |
| 6629 | 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); } |
| 6630 | static void Strtrim(char* s) { char* str_end = s + strlen(s); while (str_end > s && str_end[-1] == ' ') str_end--; *str_end = 0; } |
| 6631 | |
| 6632 | void ClearLog() |
| 6633 | { |
| 6634 | for (int i = 0; i < Items.Size; i++) |
| 6635 | free(Items[i]); |
| 6636 | Items.clear(); |
| 6637 | } |
| 6638 | |
| 6639 | void AddLog(const char* fmt, ...) IM_FMTARGS(2) |
| 6640 | { |
| 6641 | // FIXME-OPT |
| 6642 | char buf[1024]; |
| 6643 | va_list args; |
| 6644 | va_start(args, fmt); |
| 6645 | vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args); |
| 6646 | buf[IM_ARRAYSIZE(buf)-1] = 0; |
| 6647 | va_end(args); |
| 6648 | Items.push_back(Strdup(buf)); |
| 6649 | } |
nothing calls this directly
no test coverage detected