#include #include "../log.h" #include "logs.h" static inline void logs_scrolling_region(ImFont* monospace_font, bool* autoscrolling) { ImGui::PushFont(monospace_font); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); ImGuiListClipper clipper; clipper.Begin(static_cast(log_entry_line_offsets.size())); while (clipper.Step()) { for (int i_u = clipper.DisplayStart; i_u < clipper.DisplayEnd; i_u++) { // what'd we do if we log the error about an error failing to show logs assert(i_u >= 0); size_t i = static_cast(i_u); const char* start_offset = &log_entries[log_entry_line_offsets[i]]; const char* end_offset = log_entry_line_offsets.size() > i + 1 ? &log_entries[log_entry_line_offsets[i + 1] - 1] : &log_entries[log_entries.size()]; ImGui::TextUnformatted(start_offset, end_offset); } } clipper.End(); ImGui::PopStyleVar(); ImGui::PopFont(); if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) { *autoscrolling = true; ImGui::SetScrollHereY(1.0f); } } void logs_window(ImFont* monospace_font, bool* autoscrolling, bool* p_open) { if (!ImGui::Begin("LogMeow Logs", p_open)) { ImGui::End(); return; } if (ImGui::Button("Clear")) { log_entries.clear(); log_entry_line_offsets = {0}; } ImGui::SameLine(); if (ImGui::Button("Copy")) { ImGui::SetClipboardText(log_entries.c_str()); } 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)) { logs_scrolling_region(monospace_font, autoscrolling); } ImGui::EndChild(); ImGui::End(); }