logmeow/windows/main.cpp

103 lines
4.6 KiB
C++

#include <imgui.h>
#include <ctime>
#include <string>
#include <vector>
#include "../logcat_entry.h"
#include "main.h"
static inline void render_table(ImFont* monospace_font, std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& 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<int>(filtered_logcat_entry_offsets.size()));
while (clipper.Step()) {
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
const LogcatEntry* logcat_entry = &logcat_entries[filtered_logcat_entry_offsets[static_cast<size_t>(i)]];
char time_as_str[128] = {0};
strftime(time_as_str, 127 * sizeof(char), "%c", localtime(&logcat_entry->time));
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::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,
std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets,
const Config& active_config, Config& inactive_config,
const Filters& active_filters, Filters& inactive_filters,
bool* show_settings_window, bool* show_filters_window, bool* show_logs_window, bool* run_event_loop) {
if (!ImGui::Begin("LogMeow", run_event_loop)) {
ImGui::End();
return;
}
if (ImGui::Button("Settings") && !*show_settings_window) {
inactive_config = active_config;
*show_settings_window = true;
}
ImGui::SameLine();
if (ImGui::Button("Filters") && !*show_filters_window) {
copy_filters(inactive_filters, active_filters);
*show_filters_window = true;
}
ImGui::SameLine();
if (!latest_log_entries_read) {
ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor::HSV(0.0f, 0.6f, 0.6f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)ImColor::HSV(0.0f, 0.7f, 0.7f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(0.0f, 0.8f, 0.8f));
}
if (ImGui::Button("Logs")) {
*show_logs_window = true;
}
if (!latest_log_entries_read) {
ImGui::PopStyleColor(3);
}
ImGui::Separator();
// 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()
// 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::End();
}