logmeow/windows/main.cpp

96 lines
3.8 KiB
C++

#include <imgui.h>
#include <ctime>
#include <string>
#include <vector>
#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<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets) {
ImGui::PushFont(monospace_font);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
ImGuiListClipper clipper;
clipper.Begin(static_cast<int>(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<size_t>(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()]);
}
}
clipper.End();
ImGui::PopStyleVar();
ImGui::PopFont();
if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) {
ImGui::SetScrollHereY(1.0f);
}
}
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()
if (ImGui::BeginChild("ScrollingRegion", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar)) {
main_scrolling_region(monospace_font, logcat_entries, filtered_logcat_entry_offsets);
}
ImGui::EndChild();
ImGui::End();
}