logmeow/misc.cpp

94 lines
2.3 KiB
C++
Raw Permalink Normal View History

2023-01-16 04:05:25 +00:00
#include <iomanip>
#include <sstream>
2023-01-16 08:41:26 +00:00
#include <system_error>
#include <imgui/imgui.h>
2023-06-21 11:45:08 +00:00
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#include <imgui/imgui_internal.h>
2023-06-21 11:45:08 +00:00
#pragma GCC diagnostic pop
2023-01-16 04:05:25 +00:00
#include "misc.h"
std::string quote(const std::string& str) {
std::stringstream ss;
ss << std::quoted(str);
return ss.str();
}
2023-01-16 08:41:26 +00:00
2023-01-17 15:35:08 +00:00
void throw_system_error(int err, const char* what) {
if (err == ENOMEM) {
throw std::bad_alloc();
}
throw std::system_error(err, std::generic_category(), what);
2023-01-16 08:41:26 +00:00
}
2023-01-17 15:35:08 +00:00
void throw_system_error(int err, std::string what) {
if (err == ENOMEM) {
throw std::bad_alloc();
}
throw std::system_error(err, std::generic_category(), std::move(what));
2023-01-16 08:41:26 +00:00
}
2023-01-17 15:35:08 +00:00
void throw_system_error(const char* what) {
throw_system_error(errno, what);
2023-01-16 08:41:26 +00:00
}
2023-01-17 15:35:08 +00:00
void throw_system_error(std::string what) {
throw_system_error(errno, std::move(what));
2023-01-16 08:41:26 +00:00
}
2023-01-29 10:08:27 +00:00
void ImGui::TextUnformatted(const std::string& str) {
ImGui::TextUnformatted(str.data(), &str.data()[str.size()]);
}
bool ImGui::RedButton(const char* label) {
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));
2023-02-02 12:50:28 +00:00
bool res = ImGui::Button(label);
2023-01-29 10:08:27 +00:00
ImGui::PopStyleColor(3);
return res;
}
2023-02-02 12:50:28 +00:00
bool ImGui::Button(const char* label, bool enabled) {
if (!enabled) {
ImGui::BeginDisabled();
}
bool res = ImGui::Button(label) && enabled;
2023-02-02 12:50:28 +00:00
if (!enabled) {
ImGui::EndDisabled();
}
return res;
}
2023-02-04 07:31:50 +00:00
bool ImGui::ArrowButton(const char* label, ImGuiDir dir, bool enabled) {
if (!enabled) {
ImGui::BeginDisabled();
}
bool res = ImGui::ArrowButton(label, dir) && enabled;
if (!enabled) {
ImGui::EndDisabled();
}
return res;
}
2023-02-04 07:31:50 +00:00
bool ImGui::BeginWithCloseShortcut(const char* label, bool* p_open, ImGuiWindowFlags flags) {
bool res = ImGui::Begin(label, p_open, flags);
if (p_open && ImGui::Shortcut(ImGuiMod_Shortcut | ImGuiKey_W, 0, ImGuiInputFlags_Repeat)) {
*p_open = false;
2023-02-11 15:15:21 +00:00
return false;
2023-02-04 07:31:50 +00:00
}
return res;
}
2023-02-11 15:15:21 +00:00
bool ImGui::IsKeyPressed(ImGuiKeyChord key_chord, bool repeat) {
return ImGui::Shortcut(key_chord, 0, repeat ? ImGuiInputFlags_Repeat : 0);
}