logmeow/fragments/export.cpp

70 lines
2.6 KiB
C++
Raw Normal View History

2023-03-28 17:03:16 +00:00
#include <imgui/imgui.h>
#include <nativefiledialog-extended/src/include/nfd.h>
#include <optional>
#include "../file.h"
#include "../logcat_entry.h"
#include "export.h"
enum class ExportAmount : int {
All = 0,
Filtered = 1,
};
2023-03-29 10:08:31 +00:00
static inline void export_logcat_entries(File& file, const LogcatEntries& logcat_entries, ExportAmount export_amount, bool export_buffers);
2023-03-28 17:03:16 +00:00
static inline std::optional<File> save_file_picker();
2023-03-29 10:08:31 +00:00
void export_fragment(const LogcatEntries& logcat_entries) {
2023-03-28 17:03:16 +00:00
static ExportAmount export_amount = ExportAmount::Filtered;
static bool export_buffers = true;
ImGui::RadioButton("Export all entries", reinterpret_cast<int*>(&export_amount), static_cast<int>(ExportAmount::All));
ImGui::RadioButton("Export filtered entries", reinterpret_cast<int*>(&export_amount), static_cast<int>(ExportAmount::Filtered));
ImGui::Checkbox("Export buffers", &export_buffers);
ImGui::Separator();
if (ImGui::Button("Export to File")) {
std::optional<File> file = save_file_picker();
if (file) {
2023-03-29 10:08:31 +00:00
export_logcat_entries(*file, logcat_entries, export_amount, export_buffers);
2023-03-28 17:03:16 +00:00
ImGui::CloseCurrentPopup();
}
}
ImGui::SameLine();
if (ImGui::Button("Export to Clipboard")) {
// TODO exist
}
}
2023-03-29 10:08:31 +00:00
static inline void export_logcat_entries(File& file, const LogcatEntries& logcat_entries, ExportAmount export_amount, bool export_buffers) {
2023-03-28 17:03:16 +00:00
// TODO actually do the job
2023-03-29 10:08:31 +00:00
file.write(std::string("Count of all entries: ") + std::to_string(logcat_entries.size_all()) + '\n');
file.write(std::string("Count of filtered entries: ") + std::to_string(logcat_entries.size_filtered()) + '\n');
2023-03-28 17:03:16 +00:00
file.write(std::string("Export amount: ") + (export_amount == ExportAmount::All ? "all" : "filtered") + '\n');
file.write(std::string("Export buffers: ") + (export_buffers ? "true" : "false") + '\n');
}
static inline std::optional<File> save_file_picker() {
nfdchar_t* path;
nfdfilteritem_t filters[1] = {{"Log file", "log"}};
nfdresult_t res = NFD_SaveDialog(&path, filters, 1, nullptr, "logcat.log");
if (res == NFD_OKAY) {
try {
File file(path, "w");
NFD_FreePath(path);
return std::move(file);
} catch (const std::exception& e) {
NFD_FreePath(path);
log(std::string("Failed to open file from file picker: ") + e.what());
}
} else if (res == NFD_CANCEL) {
// dialog was canceled, shrug
} else {
log(std::string("Failed to open file picker: ") + NFD_GetError());
}
return std::nullopt;
}