From fbf717db33922b3207bd2307d86b94b7ea6cd1da Mon Sep 17 00:00:00 2001 From: blankie Date: Sun, 29 Jan 2023 14:56:05 +0700 Subject: [PATCH] Table-ify logcat --- windows/main.cpp | 61 +++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/windows/main.cpp b/windows/main.cpp index a3861c8..e0d8690 100644 --- a/windows/main.cpp +++ b/windows/main.cpp @@ -6,47 +6,53 @@ #include "../logcat_entry.h" #include "main.h" -static std::string leftpad(std::string str, size_t characters) { - if (str.size() < characters) { - return str.insert(0, characters - str.size(), ' '); - } - return str; -} - -static inline void main_scrolling_region(ImFont* monospace_font, std::vector& logcat_entries, std::vector& filtered_logcat_entry_offsets) { - ImGui::PushFont(monospace_font); +static inline void render_table(ImFont* monospace_font, std::vector& logcat_entries, std::vector& filtered_logcat_entry_offsets) { ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); + ImGui::TableSetupScrollFreeze(0, 1); + ImGui::TableSetupColumn("Time", ImGuiTableColumnFlags_None); + ImGui::TableSetupColumn("User", ImGuiTableColumnFlags_None); + ImGui::TableSetupColumn("PID", ImGuiTableColumnFlags_None); + ImGui::TableSetupColumn("TID", ImGuiTableColumnFlags_None); + ImGui::TableSetupColumn("Buffer", ImGuiTableColumnFlags_None); + ImGui::TableSetupColumn("Priority", ImGuiTableColumnFlags_None); + ImGui::TableSetupColumn("Tag", ImGuiTableColumnFlags_None); + ImGui::TableSetupColumn("Message", ImGuiTableColumnFlags_None); + ImGui::TableHeadersRow(); + + ImGui::PushFont(monospace_font); + ImGuiListClipper clipper; clipper.Begin(static_cast(filtered_logcat_entry_offsets.size())); while (clipper.Step()) { for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) { - LogcatEntry* logcat_entry = &logcat_entries[filtered_logcat_entry_offsets[static_cast(i)]]; + const LogcatEntry* logcat_entry = &logcat_entries[filtered_logcat_entry_offsets[static_cast(i)]]; char time_as_str[128] = {0}; strftime(time_as_str, 127 * sizeof(char), "%c", localtime(&logcat_entry->time)); - std::string header = std::string(1, '[') + time_as_str + ' ' - + leftpad(logcat_entry->user.value_or(" "), 5) + ' ' - + leftpad(std::to_string(logcat_entry->pid), 5) + ' ' - + leftpad(std::to_string(logcat_entry->tid), 5) + ' ' - + leftpad(buffer_to(logcat_entry->buffer), 6) + ' ' - + leftpad(priority_to(logcat_entry->priority), 7) + "] "; - ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetStyle().Colors[ImGuiCol_TextDisabled]); - ImGui::TextUnformatted(header.data(), &header[header.size()]); - ImGui::PopStyleColor(); - - std::string line = logcat_entry->tag + ": " + logcat_entry->message; - ImGui::SameLine(); - ImGui::TextUnformatted(line.data(), &line[line.size()]); + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); ImGui::TextUnformatted(time_as_str); + if (logcat_entry->user) { + ImGui::TableSetColumnIndex(1); + ImGui::TextUnformatted(logcat_entry->user->data(), &logcat_entry->user->data()[logcat_entry->user->size()]); + } + ImGui::TableSetColumnIndex(2); ImGui::Text("%zu", logcat_entry->pid); + ImGui::TableSetColumnIndex(3); ImGui::Text("%zu", logcat_entry->tid); + ImGui::TableSetColumnIndex(4); ImGui::TextUnformatted(buffer_to(logcat_entry->buffer)); + ImGui::TableSetColumnIndex(5); ImGui::TextUnformatted(priority_to(logcat_entry->priority)); + ImGui::TableSetColumnIndex(6); ImGui::TextUnformatted(logcat_entry->tag.data(), &logcat_entry->tag.data()[logcat_entry->tag.size()]); + ImGui::TableSetColumnIndex(7); ImGui::TextUnformatted(logcat_entry->message.data(), &logcat_entry->message.data()[logcat_entry->message.size()]); } } clipper.End(); - ImGui::PopStyleVar(); ImGui::PopFont(); + ImGui::PopStyleVar(); if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) { ImGui::SetScrollHereY(1.0f); } + + ImGui::EndTable(); } void main_window(bool latest_log_entries_read, ImFont* monospace_font, @@ -87,9 +93,10 @@ void main_window(bool latest_log_entries_read, ImFont* monospace_font, // copied from imgui/imgui_demo.cpp: [SECTION] Example App: Debug Console / ShowExampleAppConsole() // and [SECTION] Example App: Long Text / ShowExampleAppLongText() // and [SECTION] Example App: Debug Log / ShowExampleAppLog() - if (ImGui::BeginChild("ScrollingRegion", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar)) { - main_scrolling_region(monospace_font, logcat_entries, filtered_logcat_entry_offsets); + // and Tables/Vertical scrolling, with clipping + const constexpr ImGuiTableFlags flags = ImGuiTableFlags_ScrollY | ImGuiTableFlags_ScrollX | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable; + if (ImGui::BeginTable("logcat", 8, flags)) { + render_table(monospace_font, logcat_entries, filtered_logcat_entry_offsets); } - ImGui::EndChild(); ImGui::End(); }