From 6d3d5b94adb532839457d9e89a42d0e5a0cf6f8f Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 24 Aug 2021 21:31:13 +0200 Subject: [PATCH] Add statustemp. --- CMakeLists.txt | 3 ++ statustemp.cpp | 116 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 statustemp.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5bb1561..8a80741 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,3 +30,6 @@ target_link_libraries(statusweather add_executable(statusip "statusip.cpp" "helpers.cpp") target_link_libraries(statusip PRIVATE fmt::fmt Boost::program_options restclient-cpp) + +add_executable(statustemp "statustemp.cpp" "helpers.cpp") +target_link_libraries(statustemp PRIVATE fmt::fmt Boost::program_options) diff --git a/statustemp.cpp b/statustemp.cpp new file mode 100644 index 0000000..43d99d4 --- /dev/null +++ b/statustemp.cpp @@ -0,0 +1,116 @@ +// Print hardware temperature for i3 status bar. +/* i3blocks config: + * [temperature] + * label=🌡 + * interval=persist + * command=statustemp --file=/sys/devices/platform/… + * markup=pango + */ + +#include "helpers.hpp" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +std::string get_file_path(int argc, char *argv[]) +{ + namespace po = boost::program_options; + + po::options_description options("Options"); + // clang-format off + options.add_options() + ("help,h", "Display this help and exit.") + ("file", po::value()->required()->value_name("PATH"), + "The file path the temperature is stored in."); + // clang-format on + + po::variables_map vm; + po::store(po::command_line_parser(argc, argv).options(options).run(), vm); + + const auto path{[]() + { + auto path{helpers::get_env("XDG_CONFIG_HOME")}; + if (path.empty()) + { + path = helpers::get_env("HOME"); + if (!path.empty()) + { + path += "/.config"; + } + } + return path += "/statustemp.cfg"; + }()}; + std::ifstream configfile(path); + po::store(po::parse_config_file(configfile, options, true), vm); + configfile.close(); + + if (vm.count("help") != 0) + { + std::cout << options; + std::exit(0); // NOLINT(concurrency-mt-unsafe) + } + po::notify(vm); + + return vm["file"].as(); +} + +std::string get_style(const float temperature) +{ + if (temperature > 85.0) + { + return R"(background="red" color="black" weight="heavy")"; + } + if (temperature > 75.0) + { + return R"(color="#ff4400")"; + } + if (temperature > 65.0) + { + return R"(color="#ffff00")"; + } + return {}; +} + +int main(int argc, char *argv[]) +{ + using fmt::format; + using namespace std::chrono_literals; + + std::locale::global(std::locale("")); + + try + { + const std::string filepath{get_file_path(argc, argv)}; + std::ifstream file; + std::string line; + float temperature{-273.15}; + + while (true) + { + file.open(filepath); + std::getline(file, line); + file.close(); + temperature = static_cast(std::stof(line) / 1000.0); + std::cout << format("{:.1Lf}°C", + get_style(temperature), temperature) + << std::endl; + std::this_thread::sleep_for(5s); + } + } + catch (std::exception &e) + { + std::cout << R"()" << e.what() << "" + << std::endl; + return 33; + } +}