blob: a9acc537a7a158be8d30b26496bee5f20e9524ff [file] [log] [blame]
James Kuszmaulb13e13f2023-11-22 20:44:04 -08001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#include "sysid/Util.h"
6
7#include <filesystem>
8#include <stdexcept>
9
10#include <imgui.h>
11#include <wpi/raw_ostream.h>
12
13void sysid::CreateTooltip(const char* text) {
14 ImGui::SameLine();
15 ImGui::TextDisabled(" (?)");
16
17 if (ImGui::IsItemHovered()) {
18 ImGui::BeginTooltip();
19 ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
20 ImGui::TextUnformatted(text);
21 ImGui::PopTextWrapPos();
22 ImGui::EndTooltip();
23 }
24}
25
26void sysid::CreateErrorPopup(bool& isError, std::string_view errorMessage) {
27 if (isError) {
28 ImGui::OpenPopup("Exception Caught!");
29 }
30
31 // Handle exceptions.
32 ImGui::SetNextWindowSize(ImVec2(480.f, 0.0f));
33 if (ImGui::BeginPopupModal("Exception Caught!")) {
34 ImGui::PushTextWrapPos(0.0f);
35 ImGui::TextColored(ImVec4(1.0f, 0.4f, 0.4f, 1.0f), "%s",
36 errorMessage.data());
37 ImGui::PopTextWrapPos();
38 if (ImGui::Button("Close")) {
39 ImGui::CloseCurrentPopup();
40 isError = false;
41 }
42 ImGui::EndPopup();
43 }
44}
45
46std::string_view sysid::GetAbbreviation(std::string_view unit) {
47 if (unit == "Meters") {
48 return "m";
49 } else if (unit == "Feet") {
50 return "ft";
51 } else if (unit == "Inches") {
52 return "in";
53 } else if (unit == "Radians") {
54 return "rad";
55 } else if (unit == "Degrees") {
56 return "deg";
57 } else if (unit == "Rotations") {
58 return "rot";
59 } else {
60 throw std::runtime_error("Invalid Unit");
61 }
62}
63
64void sysid::SaveFile(std::string_view contents,
65 const std::filesystem::path& path) {
66 // Create the path if it doesn't already exist.
67 std::filesystem::create_directories(path.root_directory());
68
69 // Open a fd_ostream to write to file.
70 std::error_code ec;
71 wpi::raw_fd_ostream ostream{path.string(), ec};
72
73 // Check error code.
74 if (ec) {
75 throw std::runtime_error("Cannot write to file: " + ec.message());
76 }
77
78 // Write contents.
79 ostream << contents;
80}