Skip to content

Commit

Permalink
don't use string_view with nlohmann json
Browse files Browse the repository at this point in the history
  • Loading branch information
jstkdng committed Jul 9, 2023
1 parent 5b5bb2b commit f40b28b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
1 change: 0 additions & 1 deletion include/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "util/ptr.hpp"

#include <string>
#include <memory>
#include <thread>
#include <cstdlib>
#include <string_view>
Expand Down
13 changes: 4 additions & 9 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <thread>
#include <tuple>
#include <string_view>

#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>
Expand Down Expand Up @@ -86,8 +83,6 @@ Application::~Application()

void Application::execute(const std::string_view cmd)
{
using namespace std::string_view_literals;

if (!canvas) {
return;
}
Expand All @@ -100,11 +95,11 @@ void Application::execute(const std::string_view cmd)
}
logger->info("Command received: {}", json.dump());

const std::string& action = json.at("action"sv);
const std::string& identifier = json.at("identifier"sv);
const std::string& action = json.at("action");
const std::string& identifier = json.at("identifier");

if (action == "add") {
if (!json.at("path"sv).is_string()) {
if (!json.at("path").is_string()) {
logger->error("Path received is not valid");
return;
}
Expand All @@ -117,7 +112,7 @@ void Application::execute(const std::string_view cmd)
} else if (action == "remove") {
canvas->remove_image(identifier);
} else if (action == "tmux") {
handle_tmux_hook(std::string{json.at("hook"sv)});
handle_tmux_hook(std::string{json.at("hook")});
} else {
logger->warn("Command not supported");
}
Expand Down
17 changes: 7 additions & 10 deletions src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#include "flags.hpp"
#include "dimensions.hpp"

#include <string_view>

#ifdef ENABLE_OPENCV
# include <opencv2/imgcodecs.hpp>
#endif
Expand All @@ -36,11 +34,10 @@

namespace fs = std::filesystem;
using njson = nlohmann::json;
using namespace std::string_view_literals;

auto Image::load(const njson& command, const Terminal& terminal) -> std::unique_ptr<Image>
{
const std::string& filename = command.at("path"sv);
const std::string& filename = command.at("path");
if (!fs::exists(filename)) {
return nullptr;
}
Expand Down Expand Up @@ -152,7 +149,7 @@ auto Image::get_dimensions(const njson& json, const Terminal& terminal) -> std::
int max_height = 0;
string width_key = "max_width";
string height_key = "max_height";
const string scaler = json.value("scaler"sv, "contain");
const string scaler = json.value("scaler", "contain");
if (json.contains("width")) {
width_key = "width";
height_key = "height";
Expand All @@ -166,14 +163,14 @@ auto Image::get_dimensions(const njson& json, const Terminal& terminal) -> std::
max_width = json.at(width_key);
max_height = json.at(height_key);
}
if (json.at("x"sv).is_string()) {
const string& xcoords = json.at("x"sv);
const string& ycoords = json.at("y"sv);
if (json.at("x").is_string()) {
const string& xcoords = json.at("x");
const string& ycoords = json.at("y");
xcoord = std::stoi(xcoords);
ycoord = std::stoi(ycoords);
} else {
xcoord = json.at("x"sv);
ycoord = json.at("y"sv);
xcoord = json.at("x");
ycoord = json.at("y");
}
return std::make_shared<Dimensions>(terminal, xcoord, ycoord, max_width, max_height, scaler);
}

0 comments on commit f40b28b

Please sign in to comment.