From a1c87e1e155e793fbdb621cba809d053abadf128 Mon Sep 17 00:00:00 2001 From: Starlight220 <53231611+Starlight220@users.noreply.github.com> Date: Tue, 6 Apr 2021 23:19:49 +0300 Subject: [PATCH] [glass] LogView: Add "copy to clipboard" button (#3274) --- glass/src/lib/native/cpp/other/Log.cpp | 11 ++++++++++- glass/src/lib/native/include/glass/other/Log.h | 4 +++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/glass/src/lib/native/cpp/other/Log.cpp b/glass/src/lib/native/cpp/other/Log.cpp index 83bbe3778f..8e6db5d4aa 100644 --- a/glass/src/lib/native/cpp/other/Log.cpp +++ b/glass/src/lib/native/cpp/other/Log.cpp @@ -23,7 +23,7 @@ void LogData::Append(const wpi::Twine& msg) { } size_t oldSize = m_buf.size(); - wpi::raw_vector_ostream os{m_buf}; + wpi::raw_string_ostream os{m_buf}; msg.print(os); for (size_t newSize = m_buf.size(); oldSize < newSize; ++oldSize) { if (m_buf[oldSize] == '\n') { @@ -32,6 +32,10 @@ void LogData::Append(const wpi::Twine& msg) { } } +const std::string& LogData::GetBuffer() { + return m_buf; +} + void glass::DisplayLog(LogData* data, bool autoScroll) { if (data->m_buf.empty()) { return; @@ -65,6 +69,11 @@ void LogView::Display() { if (ImGui::Selectable("Clear")) { m_data->Clear(); } + const auto& buf = m_data->GetBuffer(); + if (ImGui::Selectable("Copy to Clipboard", false, + buf.empty() ? ImGuiSelectableFlags_Disabled : 0)) { + ImGui::SetClipboardText(buf.c_str()); + } ImGui::EndPopup(); } diff --git a/glass/src/lib/native/include/glass/other/Log.h b/glass/src/lib/native/include/glass/other/Log.h index 3d1ab146a1..ddaadaf918 100644 --- a/glass/src/lib/native/include/glass/other/Log.h +++ b/glass/src/lib/native/include/glass/other/Log.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include @@ -20,10 +21,11 @@ class LogData { void Clear(); void Append(const wpi::Twine& msg); + const std::string& GetBuffer(); private: size_t m_maxLines; - std::vector m_buf; + std::string m_buf; std::vector m_lineOffsets{0}; };