Fixed ImGuiTextFilter triming of leading/trailing blanks. Documented "Filtering" section of demo better.

This commit is contained in:
ocornut 2014-08-25 17:19:04 +01:00
parent 710b9b68b1
commit d17a586738
2 changed files with 12 additions and 4 deletions

View file

@ -725,6 +725,7 @@ static void RegisterAliveId(const ImGuiID& id)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Helper: Key->value storage
void ImGuiStorage::Clear() void ImGuiStorage::Clear()
{ {
Data.clear(); Data.clear();
@ -797,6 +798,7 @@ void ImGuiStorage::SetAllInt(int v)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Helper: Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]"
ImGuiTextFilter::ImGuiTextFilter() ImGuiTextFilter::ImGuiTextFilter()
{ {
InputBuf[0] = 0; InputBuf[0] = 0;
@ -888,6 +890,7 @@ bool ImGuiTextFilter::PassFilter(const char* val) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Helper: Text buffer for logging/accumulating text
void ImGuiTextBuffer::append(const char* fmt, ...) void ImGuiTextBuffer::append(const char* fmt, ...)
{ {
va_list args; va_list args;
@ -5854,7 +5857,12 @@ void ShowTestWindow(bool* open)
if (ImGui::CollapsingHeader("Filtering")) if (ImGui::CollapsingHeader("Filtering"))
{ {
static ImGuiTextFilter filter; static ImGuiTextFilter filter;
filter.Draw(); ImGui::Text("Filter usage:\n"
" \"\" display all lines\n"
" \"xxx\" display lines containing \"xxx\"\n"
" \"xxx,yyy\" display lines containing \"xxx\" or \"yyy\"\n"
" \"-xxx\" hide lines containing \"xxx\"");
filter.Draw();
const char* lines[] = { "aaa1.c", "bbb1.c", "ccc1.c", "aaa2.cpp", "bbb2.cpp", "ccc2.cpp", "abc.h", "hello, world" }; const char* lines[] = { "aaa1.c", "bbb1.c", "ccc1.c", "aaa2.cpp", "bbb2.cpp", "ccc2.cpp", "abc.h", "hello, world" };
for (size_t i = 0; i < IM_ARRAYSIZE(lines); i++) for (size_t i = 0; i < IM_ARRAYSIZE(lines); i++)
if (filter.PassFilter(lines[i])) if (filter.PassFilter(lines[i]))

View file

@ -107,7 +107,7 @@ public:
// Helpers at bottom of the file: // Helpers at bottom of the file:
// - if (IMGUI_ONCE_UPON_A_FRAME) // Execute a block of code once per frame only // - if (IMGUI_ONCE_UPON_A_FRAME) // Execute a block of code once per frame only
// - struct ImGuiTextFilter // Parse and apply text filter. In format "aaaaa[,bbbb][,ccccc]" // - struct ImGuiTextFilter // Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]"
// - struct ImGuiTextBuffer // Text buffer for logging/accumulating text // - struct ImGuiTextBuffer // Text buffer for logging/accumulating text
// - struct ImGuiStorage // Custom key value storage (if you need to alter open/close states manually) // - struct ImGuiStorage // Custom key value storage (if you need to alter open/close states manually)
// - struct ImDrawList // Draw command list // - struct ImDrawList // Draw command list
@ -449,7 +449,7 @@ private:
bool TryIsNewFrame() const { const int current_frame = ImGui::GetFrameCount(); if (LastFrame == current_frame) return false; LastFrame = current_frame; return true; } bool TryIsNewFrame() const { const int current_frame = ImGui::GetFrameCount(); if (LastFrame == current_frame) return false; LastFrame = current_frame; return true; }
}; };
// Helper: Parse and apply text filter. In format "aaaaa[,bbbb][,ccccc]" // Helper: Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]"
struct ImGuiTextFilter struct ImGuiTextFilter
{ {
struct TextRange struct TextRange
@ -463,7 +463,7 @@ struct ImGuiTextFilter
const char* end() const { return e; } const char* end() const { return e; }
bool empty() const { return b == e; } bool empty() const { return b == e; }
char front() const { return *b; } char front() const { return *b; }
static bool isblank(char c) { return c == ' ' && c == '\t'; } static bool isblank(char c) { return c == ' ' || c == '\t'; }
void trim_blanks() { while (b < e && isblank(*b)) b++; while (e > b && isblank(*(e-1))) e--; } void trim_blanks() { while (b < e && isblank(*b)) b++; while (e > b && isblank(*(e-1))) e--; }
void split(char separator, ImVector<TextRange>& out); void split(char separator, ImVector<TextRange>& out);
}; };