logmeow/windows/main.cpp

87 lines
3.4 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>& filtered_logcat_entries) {
ImGui::PushFont(monospace_font);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
ImGuiListClipper clipper;
clipper.Begin(static_cast<int>(filtered_logcat_entries.size()));
while (clipper.Step()) {
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
LogcatEntry* logcat_entry = &filtered_logcat_entries[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>& filtered_logcat_entries,
const Config& active_config, Config& inactive_config, bool* show_settings_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 (!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, filtered_logcat_entries);
}
ImGui::EndChild();
ImGui::End();
}