logmeow/windows/main.cpp

134 lines
5.7 KiB
C++

#include <imgui.h>
#include <ctime>
#include <string>
#include <vector>
#include "../misc.h"
#include "../logcat_entry.h"
#include "../logcat_thread.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::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)]];
struct tm tm;
char time_as_str[128] = {0};
strftime(time_as_str, 127 * sizeof(char), "%c", localtime_r(&logcat_entry->time, &tm));
ImGui::TableNextRow();
if (ImGui::TableSetColumnIndex(0)) ImGui::TextUnformatted(time_as_str);
if (logcat_entry->user && ImGui::TableSetColumnIndex(1)) {
ImGui::TextUnformatted(*logcat_entry->user);
}
if (ImGui::TableSetColumnIndex(2)) ImGui::Text("%zu", logcat_entry->pid);
if (ImGui::TableSetColumnIndex(3)) ImGui::Text("%zu", logcat_entry->tid);
if (ImGui::TableSetColumnIndex(4)) ImGui::TextUnformatted(buffer_to(logcat_entry->buffer));
if (ImGui::TableSetColumnIndex(5)) ImGui::TextUnformatted(priority_to(logcat_entry->priority));
if (ImGui::TableSetColumnIndex(6)) ImGui::TextUnformatted(logcat_entry->tag);
if (ImGui::TableSetColumnIndex(7)) ImGui::TextUnformatted(logcat_entry->message);
}
}
clipper.End();
ImGui::PopFont();
if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) {
ImGui::SetScrollHereY(1.0f);
}
ImGui::EndTable();
}
void main_window(bool latest_log_entries_read, ImFont* monospace_font, LogcatThread& logcat_thread,
std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets,
const Config& active_config, Config& inactive_config,
bool* show_settings_window, bool* show_filters_window, bool* show_exclusions_window, bool* show_logs_window) {
ImGui::SetNextWindowPos(ImGui::GetMainViewport()->WorkPos);
ImGui::SetNextWindowSize(ImGui::GetMainViewport()->WorkSize);
if (!ImGui::BeginWithCloseShortcut("LogMeow", nullptr,
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus)) {
ImGui::End();
return;
}
if (ImGui::Button("Settings") && !*show_settings_window) {
inactive_config.logcat_command = active_config.logcat_command;
inactive_config.normal_font_size = active_config.normal_font_size;
inactive_config.monospace_font_size = active_config.monospace_font_size;
*show_settings_window = true;
}
ImGui::SameLine();
if (ImGui::Button("Filters") && !*show_filters_window) {
copy_filters(inactive_config.filters, active_config.filters);
*show_filters_window = true;
}
ImGui::SameLine();
if (ImGui::Button("Exclusions") && !*show_exclusions_window) {
copy_filters(inactive_config.exclusions, active_config.exclusions);
*show_exclusions_window = true;
}
ImGui::SameLine();
bool open_logs;
if (!latest_log_entries_read) {
open_logs = ImGui::RedButton("Logs");
} else {
open_logs = ImGui::Button("Logs");
}
if (open_logs) {
*show_logs_window = true;
}
bool can_send_logcat_request = logcat_thread.logcat_process_request.load() == LogcatProcessRequest::None;
bool logcat_running = logcat_thread.logcat_process_running.test();
ImGui::AlignTextToFramePadding();
ImGui::TextUnformatted("Logcat:");
ImGui::SameLine();
if (!can_send_logcat_request) {
ImGui::BeginDisabled();
}
if (ImGui::Button("Start", !logcat_running)) {
logcat_thread.logcat_process_request.store(LogcatProcessRequest::Start);
}
ImGui::SameLine();
if (ImGui::Button("Stop", logcat_running)) {
logcat_thread.logcat_process_request.store(LogcatProcessRequest::Stop);
}
ImGui::SameLine();
if (ImGui::Button("Restart", logcat_running)) {
logcat_thread.logcat_process_request.store(LogcatProcessRequest::Start);
}
if (!can_send_logcat_request) {
ImGui::EndDisabled();
}
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();
}