intHistoryPos;// -1: new line, 0..History.size()-1 browsing history.
ImVector<constchar*>Commands;
voidClear()
ExampleAppConsole()
{
ClearLog();
HistoryPos=-1;
Commands.push_back("HELP");
Commands.push_back("HISTORY");
Commands.push_back("CLEAR");
Commands.push_back("CLASSIFY");// "classify" is here to provide an example of "C"+[tab] completing to "CL" and displaying matches.
}
~ExampleAppConsole()
{
ClearLog();
for(size_ti=0;i<Items.size();i++)
ImGui::MemFree(History[i]);
}
voidClearLog()
{
for(size_ti=0;i<Items.size();i++)
ImGui::MemFree(Items[i]);
Items.clear();
NewItems=true;
ScrollToBottom=true;
}
voidAddLog(constchar*fmt,...)
{
charbuf[512];
charbuf[1024];
va_listargs;
va_start(args,fmt);
ImFormatStringV(buf,IM_ARRAYSIZE(buf),fmt,args);
va_end(args);
Items.push_back(ImStrdup(buf));
NewItems=true;
ScrollToBottom=true;
}
voidRun(constchar*title,bool*opened)
{
if(!ImGui::Begin(title,opened,ImVec2(520,600)))
{
ImGui::End();
return;
}
ImGui::TextWrapped("This example implement a console with basic coloring, completion and history. A more elaborate implementation may want to store entries along with extra data such as timestamp, emitter, etc.");
ImGui::TextWrapped("Enter 'HELP' for help, press TAB to use text completion.");
// TODO: display from bottom
// TODO: clip manually
if(ImGui::SmallButton("Add Dummy Text"))AddLog("some text\nsome more text\ndisplay very important message here!\n");ImGui::SameLine();
if(ImGui::SmallButton("Add Dummy Error"))AddLog("[error] something went wrong");ImGui::SameLine();
if(ImGui::IsItemHovered())ImGui::SetKeyboardFocusHere(-1);// Auto focus on hover
ImGui::PopStyleVar();
ImGui::Separator();
// Display every line as a separate entry so we can change their color or add custom widgets. If you only want raw text you can use ImGui::TextUnformatted(log.begin(), log.end());
// NB- if you have lots of text this approach may be too inefficient. You can seek and display only the lines that are on display using a technique similar to what TextUnformatted() does,
// or faster if your entries are already stored into a table.
ImGui::TextWrapped("This example implement a simple console. A more elaborate implementation may want to store individual entries along with extra data such as timestamp, emitter, etc.");
ImGui::TextWrapped("Press TAB to use text completion.");
// TODO: display from bottom
// TODO: clip manually
// TODO: history
staticExampleAppConsoleconsole;
staticcharinput[256]="";
if(ImGui::SmallButton("Add Dummy Text"))console.AddLog("some text\nsome more text");
ImGui::SameLine();
if(ImGui::SmallButton("Add Dummy Error"))console.AddLog("[error] something went wrong");
// Display every line as a separate entry so we can change their color or add custom widgets. If you only want raw text you can use ImGui::TextUnformatted(log.begin(), log.end());
// NB- if you have lots of text this approach may be too inefficient. You can seek and display only the lines that are on display using a technique similar to what TextUnformatted() does,
// or faster if your entries are already stored into a table.