1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-25 13:55:47 -05:00

refactor: Cleanup

This commit is contained in:
Michael Carlberg 2017-01-11 03:07:28 +01:00
parent c1aa3319a1
commit 8cc885c316
42 changed files with 353 additions and 244 deletions

View file

@ -3,6 +3,7 @@
#include <mutex>
#include "common.hpp"
#include "settings.hpp"
// fwd
struct _snd_ctl;

View file

@ -39,6 +39,7 @@
#endif
#include "common.hpp"
#include "settings.hpp"
#include "errors.hpp"
POLYBAR_NS

View file

@ -3,6 +3,7 @@
#include <mutex>
#include "common.hpp"
#include "settings.hpp"
// fwd
struct _snd_mixer;

View file

@ -11,14 +11,12 @@
#endif
#include "common.hpp"
#include "config.hpp"
#include "settings.hpp"
#include "errors.hpp"
#include "utils/math.hpp"
POLYBAR_NS
namespace chrono = std::chrono;
class file_descriptor;
namespace net {
@ -45,7 +43,7 @@ namespace net {
struct link_activity {
bytes_t transmitted{0};
bytes_t received{0};
chrono::system_clock::time_point time;
std::chrono::system_clock::time_point time;
};
struct link_status {

View file

@ -2,8 +2,7 @@
#include <memory>
#include <string>
#include "config.hpp"
#include <vector>
#define POLYBAR_NS \
namespace polybar { \

View file

@ -4,6 +4,7 @@
#include <mutex>
#include "common.hpp"
#include "settings.hpp"
#include "components/types.hpp"
#include "errors.hpp"
#include "events/signal_fwd.hpp"

View file

@ -4,7 +4,6 @@
#include "common.hpp"
#include "errors.hpp"
#include "utils/factory.hpp"
POLYBAR_NS
@ -27,8 +26,12 @@ namespace command_line {
string token;
const choices values;
explicit option(string flag, string flag_long, string desc, string token = "", const choices c = {})
: flag(flag), flag_long(flag_long), desc(desc), token(token), values(c) {}
explicit option(string&& flag, string&& flag_long, string&& desc, string&& token = "", const choices&& c = {})
: flag(forward<string>(flag))
, flag_long(forward<string>(flag_long))
, desc(forward<string>(desc))
, token(forward<string>(token))
, values(forward<const choices>(c)) {}
};
// }}}
@ -67,8 +70,4 @@ namespace command_line {
// }}}
}
using cliparser = command_line::parser;
using clioption = command_line::option;
using clioptions = command_line::options;
POLYBAR_NS_END

View file

@ -1,4 +1,5 @@
#pragma once
#include <unordered_map>
#include "common.hpp"

View file

@ -4,6 +4,7 @@
#include <thread>
#include "common.hpp"
#include "settings.hpp"
#include "events/signal_fwd.hpp"
#include "events/signal_receiver.hpp"
#include "events/types.hpp"

View file

@ -1,14 +1,15 @@
#pragma once
#include "common.hpp"
#include "components/logger.hpp"
#include "events/signal_emitter.hpp"
#include "settings.hpp"
#include "utils/concurrency.hpp"
#include "utils/functional.hpp"
#include "utils/file.hpp"
POLYBAR_NS
class file_descriptor;
class logger;
class signal_emitter;
/**
* Message types
*/

View file

@ -5,7 +5,7 @@
#endif
#include <chrono>
#include <iostream>
#include <cstdio>
#include "common.hpp"
@ -23,7 +23,7 @@ namespace debug_util {
explicit scope_timer() : m_start(clock_t::now()) {}
~scope_timer() {
std::cout << std::chrono::duration_cast<duration_t>(clock_t::now() - m_start).count() << "ms" << std::endl;
printf("%lums\n", std::chrono::duration_cast<duration_t>(clock_t::now() - m_start).count());
}
private:
@ -39,13 +39,12 @@ namespace debug_util {
auto start = std::chrono::high_resolution_clock::now();
expr();
auto finish = std::chrono::high_resolution_clock::now();
std::cout << "execution speed: " << std::chrono::duration_cast<std::chrono::milliseconds>(finish - start).count()
<< "ms" << std::endl;
printf("execution speed: %lums\n", std::chrono::duration_cast<std::chrono::milliseconds>(finish - start).count());
}
template <class T>
void memory_usage(const T& object) noexcept {
std::cout << "memory usage: " << sizeof(object) << "b" << std::endl;
printf("memory usage: %lub\n", sizeof(object));
}
}

View file

@ -1,7 +1,7 @@
#pragma once
#include "components/config.hpp"
#include "config.hpp"
#include "settings.hpp"
#include "modules/meta/inotify_module.hpp"
POLYBAR_NS

View file

@ -1,6 +1,6 @@
#pragma once
#include "config.hpp"
#include "settings.hpp"
#include "modules/meta/timer_module.hpp"
POLYBAR_NS

View file

@ -1,7 +1,7 @@
#pragma once
#include "components/config.hpp"
#include "config.hpp"
#include "settings.hpp"
#include "modules/meta/timer_module.hpp"
POLYBAR_NS

View file

@ -1,6 +1,6 @@
#pragma once
#include "config.hpp"
#include "settings.hpp"
#include "modules/meta/timer_module.hpp"
#include "utils/http.hpp"

View file

@ -3,7 +3,7 @@
#include <i3ipc++/ipc.hpp>
#include "components/config.hpp"
#include "config.hpp"
#include "settings.hpp"
#include "modules/meta/event_module.hpp"
#include "modules/meta/input_handler.hpp"
#include "utils/i3.hpp"

View file

@ -1,6 +1,6 @@
#pragma once
#include "config.hpp"
#include "settings.hpp"
#include "modules/meta/timer_module.hpp"
POLYBAR_NS

View file

@ -2,7 +2,7 @@
#include <istream>
#include "config.hpp"
#include "settings.hpp"
#include "modules/meta/timer_module.hpp"
POLYBAR_NS

View file

@ -1,6 +1,6 @@
#pragma once
#include "config.hpp"
#include "settings.hpp"
#include "modules/meta/event_module.hpp"
#include "modules/meta/input_handler.hpp"

View file

@ -1,7 +1,7 @@
#pragma once
#include "components/config.hpp"
#include "config.hpp"
#include "settings.hpp"
#include "modules/meta/event_handler.hpp"
#include "modules/meta/input_handler.hpp"
#include "modules/meta/static_module.hpp"

107
include/settings.hpp Normal file
View file

@ -0,0 +1,107 @@
#pragma once
#include <cstdio>
#include <string>
#include <vector>
#include "version.hpp"
#define APP_NAME "polybar"
#define APP_VERSION "2.4.8-9-gc1aa331-git"
#ifndef APP_VERSION
#define APP_VERSION GIT_TAG
#endif
#define BASE_PATH "/home/jaagr/.local/var/github/jaagr/polybar"
#define ENABLE_ALSA 1
#define ENABLE_MPD 1
#define ENABLE_NETWORK 1
#define ENABLE_I3 1
#define ENABLE_CURL 1
#define WITH_XRANDR 1
#define WITH_XRENDER 0
#define WITH_XDAMAGE 0
#define WITH_XSYNC 0
#define WITH_XCOMPOSITE 0
#define WITH_XKB 1
#define XPP_EXTENSION_LIST xpp::randr::extension, xpp::xkb::extension
/* #undef DEBUG_LOGGER */
/* #undef VERBOSE_TRACELOG */
/* #undef DEBUG_HINTS */
static const size_t EVENT_SIZE{64UL};
static const int SIGN_PRIORITY_CONTROLLER{1};
static const int SIGN_PRIORITY_SCREEN{2};
static const int SIGN_PRIORITY_BAR{3};
static const int SIGN_PRIORITY_RENDERER{4};
static const int SIGN_PRIORITY_TRAY{5};
static const int SINK_PRIORITY_BAR{1};
static const int SINK_PRIORITY_SCREEN{2};
static const int SINK_PRIORITY_TRAY{3};
static const int SINK_PRIORITY_MODULE{4};
#ifdef DEBUG_HINTS
static const int DEBUG_HINTS_OFFSET_X{0};
static const int DEBUG_HINTS_OFFSET_Y{0};
#endif
static constexpr const char* ALSA_SOUNDCARD{"default"};
static constexpr const char* BSPWM_SOCKET_PATH{"/tmp/bspwm_0_0-socket"};
static constexpr const char* BSPWM_STATUS_PREFIX{"W"};
static constexpr const char* CONNECTION_TEST_IP{"8.8.8.8"};
static constexpr const char* PATH_ADAPTER{"/sys/class/power_supply/%adapter%"};
static constexpr const char* PATH_BACKLIGHT_MAX{"/sys/class/backlight/%card%/max_brightness"};
static constexpr const char* PATH_BACKLIGHT_VAL{"/sys/class/backlight/%card%/brightness"};
static constexpr const char* PATH_BATTERY{"/sys/class/power_supply/%battery%"};
static constexpr const char* PATH_CPU_INFO{"/proc/stat"};
static constexpr const char* PATH_MEMORY_INFO{"/proc/meminfo"};
static constexpr const char* PATH_MESSAGING_FIFO{"/tmp/polybar_mqueue.%pid%"};
static constexpr const char* PATH_TEMPERATURE_INFO{"/sys/class/thermal/thermal_zone%zone%/temp"};
static constexpr const char* BUILDER_SPACE_TOKEN{"%__"};
const auto version_details = [](const std::vector<std::string>& args) {
for (auto&& arg : args) {
if (arg.compare(0, 3, "-vv") == 0)
return true;
}
return false;
};
// clang-format off
const auto print_build_info = [](bool extended = false) {
printf("%s %s\n\n", APP_NAME, APP_VERSION);
printf("Features: %calsa %ccurl %ci3 %cmpd %cnetwork\n",
(ENABLE_ALSA ? '+' : '-'),
(ENABLE_CURL ? '+' : '-'),
(ENABLE_I3 ? '+' : '-'),
(ENABLE_MPD ? '+' : '-'),
(ENABLE_NETWORK ? '+' : '-'));
if (extended) {
printf("\n");
printf("Build type: RelWithDebInfo\n");
printf("Compiler: /usr/bin/c++\n");
printf("Compiler flags: -Wall -Wextra -Werror -O2 -pedantic -pedantic-errors\n");
printf("Linker flags: \n");
printf("\n");
printf("ALSA_SOUNDCARD %s\n", ALSA_SOUNDCARD);
printf("BSPWM_SOCKET_PATH %s\n", BSPWM_SOCKET_PATH);
printf("BSPWM_STATUS_PREFIX %s\n", BSPWM_STATUS_PREFIX);
printf("BUILDER_SPACE_TOKEN %s\n", BUILDER_SPACE_TOKEN);
printf("CONNECTION_TEST_IP %s\n", CONNECTION_TEST_IP);
printf("PATH_ADAPTER %s\n", PATH_ADAPTER);
printf("PATH_BACKLIGHT_MAX %s\n", PATH_BACKLIGHT_MAX);
printf("PATH_BACKLIGHT_VAL %s\n", PATH_BACKLIGHT_VAL);
printf("PATH_BATTERY %s\n", PATH_BATTERY);
printf("PATH_CPU_INFO %s\n", PATH_CPU_INFO);
printf("PATH_MEMORY_INFO %s\n", PATH_MEMORY_INFO);
printf("PATH_TEMPERATURE_INFO %s\n", PATH_TEMPERATURE_INFO);
}
};
// clang-format on
// vim:ft=cpp

View file

@ -1,6 +1,7 @@
#pragma once
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include "version.hpp"
@ -30,7 +31,7 @@
#cmakedefine VERBOSE_TRACELOG
#cmakedefine DEBUG_HINTS
static const size_t EVENT_SIZE{64U};
static const size_t EVENT_SIZE{64UL};
static const int SIGN_PRIORITY_CONTROLLER{1};
static const int SIGN_PRIORITY_SCREEN{2};
@ -44,12 +45,8 @@ static const int SINK_PRIORITY_TRAY{3};
static const int SINK_PRIORITY_MODULE{4};
#ifdef DEBUG_HINTS
static const int DEBUG_HINTS_OFFSET_X {
@DEBUG_HINTS_OFFSET_X@
};
static const int DEBUG_HINTS_OFFSET_Y {
@DEBUG_HINTS_OFFSET_Y@
};
static const int DEBUG_HINTS_OFFSET_X{@DEBUG_HINTS_OFFSET_X@};
static const int DEBUG_HINTS_OFFSET_Y{@DEBUG_HINTS_OFFSET_Y@};
#endif
static constexpr const char* ALSA_SOUNDCARD{"@SETTING_ALSA_SOUNDCARD@"};
@ -67,7 +64,7 @@ static constexpr const char* PATH_TEMPERATURE_INFO{"@SETTING_PATH_TEMPERATURE_IN
static constexpr const char* BUILDER_SPACE_TOKEN{"%__"};
auto version_details = [](const std::vector<std::string>& args) {
const auto version_details = [](const std::vector<std::string>& args) {
for (auto&& arg : args) {
if (arg.compare(0, 3, "-vv") == 0)
return true;
@ -76,36 +73,34 @@ auto version_details = [](const std::vector<std::string>& args) {
};
// clang-format off
auto print_build_info = [](bool extended = false) {
std::cout << APP_NAME << " " << APP_VERSION << " " << "\n"
<< "\n"
<< "Features: "
<< (ENABLE_ALSA ? "+" : "-") << "alsa "
<< (ENABLE_CURL ? "+" : "-") << "curl "
<< (ENABLE_I3 ? "+" : "-") << "i3 "
<< (ENABLE_MPD ? "+" : "-") << "mpd "
<< (ENABLE_NETWORK ? "+" : "-") << "network "
<< "\n";
if (!extended)
return;
std::cout << "\n"
<< "Build type: @CMAKE_BUILD_TYPE@" << "\n"
<< "Compiler: @CMAKE_CXX_COMPILER@" << "\n"
<< "Compiler flags: @CMAKE_CXX_FLAGS@" << "\n"
<< "Linker flags: @CMAKE_EXE_LINKER_FLAGS@" << "\n"
<< "\n"
<< "ALSA_SOUNDCARD " << ALSA_SOUNDCARD << "\n"
<< "BSPWM_SOCKET_PATH " << BSPWM_SOCKET_PATH << "\n"
<< "BSPWM_STATUS_PREFIX " << BSPWM_STATUS_PREFIX << "\n"
<< "BUILDER_SPACE_TOKEN " << BUILDER_SPACE_TOKEN << "\n"
<< "CONNECTION_TEST_IP " << CONNECTION_TEST_IP << "\n"
<< "PATH_ADAPTER " << PATH_ADAPTER << "\n"
<< "PATH_BACKLIGHT_MAX " << PATH_BACKLIGHT_MAX << "\n"
<< "PATH_BACKLIGHT_VAL " << PATH_BACKLIGHT_VAL << "\n"
<< "PATH_BATTERY " << PATH_BATTERY << "\n"
<< "PATH_CPU_INFO " << PATH_CPU_INFO << "\n"
<< "PATH_MEMORY_INFO " << PATH_MEMORY_INFO << "\n"
<< "PATH_TEMPERATURE_INFO " << PATH_TEMPERATURE_INFO << "\n";
const auto print_build_info = [](bool extended = false) {
printf("%s %s\n\n", APP_NAME, APP_VERSION);
printf("Features: %calsa %ccurl %ci3 %cmpd %cnetwork\n",
(ENABLE_ALSA ? '+' : '-'),
(ENABLE_CURL ? '+' : '-'),
(ENABLE_I3 ? '+' : '-'),
(ENABLE_MPD ? '+' : '-'),
(ENABLE_NETWORK ? '+' : '-'));
if (extended) {
printf("\n");
printf("Build type: @CMAKE_BUILD_TYPE@\n");
printf("Compiler: @CMAKE_CXX_COMPILER@\n");
printf("Compiler flags: @CMAKE_CXX_FLAGS@\n");
printf("Linker flags: @CMAKE_EXE_LINKER_FLAGS@\n");
printf("\n");
printf("ALSA_SOUNDCARD %s\n", ALSA_SOUNDCARD);
printf("BSPWM_SOCKET_PATH %s\n", BSPWM_SOCKET_PATH);
printf("BSPWM_STATUS_PREFIX %s\n", BSPWM_STATUS_PREFIX);
printf("BUILDER_SPACE_TOKEN %s\n", BUILDER_SPACE_TOKEN);
printf("CONNECTION_TEST_IP %s\n", CONNECTION_TEST_IP);
printf("PATH_ADAPTER %s\n", PATH_ADAPTER);
printf("PATH_BACKLIGHT_MAX %s\n", PATH_BACKLIGHT_MAX);
printf("PATH_BACKLIGHT_VAL %s\n", PATH_BACKLIGHT_VAL);
printf("PATH_BATTERY %s\n", PATH_BATTERY);
printf("PATH_CPU_INFO %s\n", PATH_CPU_INFO);
printf("PATH_MEMORY_INFO %s\n", PATH_MEMORY_INFO);
printf("PATH_TEMPERATURE_INFO %s\n", PATH_TEMPERATURE_INFO);
}
};
// clang-format on

View file

@ -4,7 +4,7 @@
#include <xcb/xcb_icccm.h>
#include "common.hpp"
#include "config.hpp"
#include "settings.hpp"
#include "utils/socket.hpp"
#include "utils/string.hpp"
#include "x11/extensions/randr.hpp"

View file

@ -1,5 +1,7 @@
#pragma once
#include <streambuf>
#include "common.hpp"
#include "utils/factory.hpp"
@ -75,7 +77,7 @@ class fd_streambuf : public std::streambuf {
char m_in[BUFSIZ - 1]{};
};
template <typename StreamType = std::ostream>
template <typename StreamType>
class fd_stream : public StreamType {
public:
using type = fd_stream<StreamType>;

View file

@ -1,6 +1,6 @@
#pragma once
#include "config.hpp"
#include "settings.hpp"
#if WITH_XDAMAGE
#include "x11/extensions/damage.hpp"

View file

@ -1,6 +1,6 @@
#pragma once
#include "config.hpp"
#include "settings.hpp"
#if not WITH_XCOMPOSITE
#error "X Composite extension is disabled..."

View file

@ -1,6 +1,6 @@
#pragma once
#include "config.hpp"
#include "settings.hpp"
#if not WITH_XDAMAGE
#error "X Damage extension is disabled..."

View file

@ -1,6 +1,6 @@
#pragma once
#include "config.hpp"
#include "settings.hpp"
namespace xpp {
#if WITH_XDAMAGE

View file

@ -1,6 +1,6 @@
#pragma once
#include "config.hpp"
#include "settings.hpp"
#if not WITH_XRANDR
#error "X RandR extension is disabled..."

View file

@ -1,6 +1,6 @@
#pragma once
#include "config.hpp"
#include "settings.hpp"
#if not WITH_XRENDER
#error "X Render extension is disabled..."

View file

@ -1,6 +1,6 @@
#pragma once
#include "config.hpp"
#include "settings.hpp"
#if not WITH_XSYNC
#error "X Sync extension is disabled..."

View file

@ -2,7 +2,7 @@
#include <map>
#include "config.hpp"
#include "settings.hpp"
#if not WITH_XKB
#error "X xkb extension is disabled..."

View file

@ -161,6 +161,6 @@ execute_process(COMMAND git describe --tags --dirty=-git
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
configure_file(
${PROJECT_SOURCE_DIR}/include/config.hpp.cmake
${CMAKE_SOURCE_DIR}/include/config.hpp
${PROJECT_SOURCE_DIR}/include/settings.hpp.cmake
${CMAKE_SOURCE_DIR}/include/settings.hpp
ESCAPE_QUOTES @ONLY)

View file

@ -22,7 +22,7 @@
#endif
#include "common.hpp"
#include "config.hpp"
#include "settings.hpp"
#include "utils/command.hpp"
#include "utils/file.hpp"
#include "utils/string.hpp"
@ -67,7 +67,7 @@ namespace net {
m_status.previous = m_status.current;
m_status.current.transmitted = 0;
m_status.current.received = 0;
m_status.current.time = chrono::system_clock::now();
m_status.current.time = std::chrono::system_clock::now();
for (auto ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == nullptr) {
@ -183,7 +183,7 @@ namespace net {
*/
string network::format_speedrate(float bytes_diff, int minwidth) const {
const auto duration = m_status.current.time - m_status.previous.time;
float time_diff = chrono::duration_cast<chrono::seconds>(duration).count();
float time_diff = std::chrono::duration_cast<std::chrono::seconds>(duration).count();
float speedrate = bytes_diff / (time_diff ? time_diff : 1);
vector<string> suffixes{"GB", "MB"};

View file

@ -1,179 +1,179 @@
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <utility>
#include "components/command_line.hpp"
#include "utils/factory.hpp"
POLYBAR_NS
/**
* Create instance
*/
cliparser::make_type cliparser::make(string&& scriptname, const clioptions&& opts) {
return factory_util::unique<cliparser>(
"Usage: " + scriptname + " bar_name [OPTION...]", forward<decltype(opts)>(opts));
}
/**
* Construct parser
*/
cliparser::parser(string&& synopsis, const options&& opts)
: m_synopsis(forward<decltype(synopsis)>(synopsis)), m_opts(forward<decltype(opts)>(opts)) {}
/**
* Print application usage message
*/
void cliparser::usage() const {
std::cout << m_synopsis << "\n\n";
// get the length of the longest string in the flag column
// which is used to align the description fields
size_t maxlen{0};
for (const auto& m_opt : m_opts) {
size_t len{m_opt.flag_long.length() + m_opt.flag.length() + 4};
maxlen = len > maxlen ? len : maxlen;
namespace command_line {
/**
* Create instance
*/
parser::make_type parser::make(string&& scriptname, const options&& opts) {
return factory_util::unique<parser>(
"Usage: " + scriptname + " bar_name [OPTION...]", forward<decltype(opts)>(opts));
}
for (auto& opt : m_opts) {
size_t pad = maxlen - opt.flag_long.length() - opt.token.length();
/**
* Construct parser
*/
parser::parser(string&& synopsis, const options&& opts)
: m_synopsis(forward<decltype(synopsis)>(synopsis)), m_opts(forward<decltype(opts)>(opts)) {}
std::cout << " " << opt.flag << ", " << opt.flag_long;
/**
* Print application usage message
*/
void parser::usage() const {
printf("%s\n\n", m_synopsis.c_str());
if (!opt.token.empty()) {
std::cout << "=" << opt.token;
pad--;
// get the length of the longest string in the flag column
// which is used to align the description fields
size_t maxlen{0};
for (const auto& m_opt : m_opts) {
size_t len{m_opt.flag_long.length() + m_opt.flag.length() + 4};
maxlen = len > maxlen ? len : maxlen;
}
// output the list with accepted values
if (!opt.values.empty()) {
std::cout << std::setw(pad + opt.desc.length()) << std::setfill(' ') << opt.desc << '\n';
for (auto& opt : m_opts) {
size_t pad = maxlen - opt.flag_long.length() - opt.token.length();
pad = pad + opt.flag_long.length() + opt.token.length() + 7;
printf(" %s, %s", opt.flag.c_str(), opt.flag_long.c_str());
std::cout << string(pad, ' ') << opt.token << " is one of: ";
for (auto& v : opt.values) {
std::cout << v << (v != opt.values.back() ? ", " : "");
if (!opt.token.empty()) {
printf("=%s", opt.token.c_str());
pad--;
}
} else {
std::cout << std::setw(pad + opt.desc.length()) << std::setfill(' ') << opt.desc;
}
std::cout << '\n';
}
// output the list with accepted values
if (!opt.values.empty()) {
printf("%*s\n", static_cast<int>(pad + opt.desc.length()), opt.desc.c_str());
std::cout << std::endl;
}
pad = pad + opt.flag_long.length() + opt.token.length() + 7;
/**
* Process input values
*/
void cliparser::process_input(const vector<string>& values) {
for (size_t i = 0; i < values.size(); i++) {
parse(values[i], values.size() > i + 1 ? values[i + 1] : "");
}
}
printf("%*c%s is one of: ", static_cast<int>(pad), ' ', opt.token.c_str());
/**
* Test if the passed option was provided
*/
bool cliparser::has(const string& option) const {
return m_optvalues.find(option) != m_optvalues.end();
}
/**
* Gets the value defined for given option
*/
string cliparser::get(string opt) const {
if (has(forward<string>(opt))) {
return m_optvalues.find(opt)->second;
}
return "";
}
/**
* Compare option value with given string
*/
bool cliparser::compare(string opt, const string& val) const {
return get(move(opt)) == val;
}
/**
* Compare option with its short version
*/
auto cliparser::is_short(const string& option, const string& opt_short) const {
return option.compare(0, opt_short.length(), opt_short) == 0;
}
/**
* Compare option with its long version
*/
auto cliparser::is_long(const string& option, const string& opt_long) const {
return option.compare(0, opt_long.length(), opt_long) == 0;
}
/**
* Compare option with both versions
*/
auto cliparser::is(const string& option, string opt_short, string opt_long) const {
return is_short(option, move(opt_short)) || is_long(option, move(opt_long));
}
/**
* Parse option value
*/
auto cliparser::parse_value(string input, const string& input_next, choices values) const {
string opt = move(input);
size_t pos;
string value;
if (input_next.empty() && opt.compare(0, 2, "--") != 0) {
throw value_error("Missing value for " + opt);
} else if ((pos = opt.find('=')) == string::npos && opt.compare(0, 2, "--") == 0) {
throw value_error("Missing value for " + opt);
} else if (pos == string::npos && !input_next.empty()) {
value = input_next;
} else {
value = opt.substr(pos + 1);
opt = opt.substr(0, pos);
}
if (!values.empty() && std::find(values.begin(), values.end(), value) == values.end()) {
throw value_error("Invalid value '" + value + "' for argument " + string{opt});
}
return value;
}
/**
* Parse and validate passed arguments and flags
*/
void cliparser::parse(const string& input, const string& input_next) {
if (m_skipnext) {
m_skipnext = false;
if (!input_next.empty()) {
return;
}
}
for (auto&& opt : m_opts) {
if (is(input, opt.flag, opt.flag_long)) {
if (opt.token.empty()) {
m_optvalues.insert(make_pair(opt.flag_long.substr(2), ""));
for (auto& v : opt.values) {
printf("%s%s", v.c_str(), v != opt.values.back() ? ", " : "");
}
} else {
auto value = parse_value(input, input_next, opt.values);
m_skipnext = (value == input_next);
m_optvalues.insert(make_pair(opt.flag_long.substr(2), value));
printf("%*s", static_cast<int>(pad + opt.desc.length()), opt.desc.c_str());
}
return;
printf("\n");
}
printf("\n");
}
/**
* Process input values
*/
void parser::process_input(const vector<string>& values) {
for (size_t i = 0; i < values.size(); i++) {
parse(values[i], values.size() > i + 1 ? values[i + 1] : "");
}
}
if (input.compare(0, 1, "-") == 0) {
throw argument_error("Unrecognized option " + input);
/**
* Test if the passed option was provided
*/
bool parser::has(const string& option) const {
return m_optvalues.find(option) != m_optvalues.end();
}
/**
* Gets the value defined for given option
*/
string parser::get(string opt) const {
if (has(forward<string>(opt))) {
return m_optvalues.find(opt)->second;
}
return "";
}
/**
* Compare option value with given string
*/
bool parser::compare(string opt, const string& val) const {
return get(move(opt)) == val;
}
/**
* Compare option with its short version
*/
auto parser::is_short(const string& option, const string& opt_short) const {
return option.compare(0, opt_short.length(), opt_short) == 0;
}
/**
* Compare option with its long version
*/
auto parser::is_long(const string& option, const string& opt_long) const {
return option.compare(0, opt_long.length(), opt_long) == 0;
}
/**
* Compare option with both versions
*/
auto parser::is(const string& option, string opt_short, string opt_long) const {
return is_short(option, move(opt_short)) || is_long(option, move(opt_long));
}
/**
* Parse option value
*/
auto parser::parse_value(string input, const string& input_next, choices values) const {
string opt = move(input);
size_t pos;
string value;
if (input_next.empty() && opt.compare(0, 2, "--") != 0) {
throw value_error("Undefined argument for option " + opt);
} else if ((pos = opt.find('=')) == string::npos && opt.compare(0, 2, "--") == 0) {
throw value_error("Undefined argument for option " + opt);
} else if (pos == string::npos && !input_next.empty()) {
value = input_next;
} else {
value = opt.substr(pos + 1);
opt = opt.substr(0, pos);
}
if (!values.empty() && std::find(values.begin(), values.end(), value) == values.end()) {
throw value_error("Invalid argument for option " + opt);
}
return value;
}
/**
* Parse and validate passed arguments and flags
*/
void parser::parse(const string& input, const string& input_next) {
if (m_skipnext) {
m_skipnext = false;
if (!input_next.empty()) {
return;
}
}
for (auto&& opt : m_opts) {
if (is(input, opt.flag, opt.flag_long)) {
if (opt.token.empty()) {
m_optvalues.insert(make_pair(opt.flag_long.substr(2), ""));
} else {
auto value = parse_value(input, input_next, opt.values);
m_skipnext = (value == input_next);
m_optvalues.insert(make_pair(opt.flag_long.substr(2), value));
}
return;
}
}
if (input.compare(0, 1, "-") == 0) {
throw argument_error("Unrecognized option " + input);
}
}
}

View file

@ -1,14 +1,12 @@
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "components/ipc.hpp"
#include "config.hpp"
#include "components/logger.hpp"
#include "events/signal.hpp"
#include "events/signal_emitter.hpp"
#include "utils/factory.hpp"
#include "utils/file.hpp"
#include "utils/io.hpp"
#include "utils/string.hpp"
POLYBAR_NS

View file

@ -5,6 +5,8 @@
#include "events/signal.hpp"
#include "events/signal_emitter.hpp"
#include "utils/color.hpp"
#include "utils/factory.hpp"
#include "utils/file.hpp"
#include "utils/math.hpp"
#include "utils/memory.hpp"
#include "utils/string.hpp"

View file

@ -4,6 +4,8 @@
#include "events/signal.hpp"
#include "events/signal_emitter.hpp"
#include "events/signal_receiver.hpp"
#include "utils/factory.hpp"
#include "utils/file.hpp"
#include "x11/connection.hpp"
#include "x11/draw.hpp"
#include "x11/extensions/all.hpp"

View file

@ -9,7 +9,7 @@
#include "components/logger.hpp"
#include "components/parser.hpp"
#include "components/renderer.hpp"
#include "config.hpp"
#include "settings.hpp"
#include "utils/env.hpp"
#include "utils/file.hpp"
#include "utils/inotify.hpp"
@ -47,7 +47,7 @@ int main(int argc, char** argv) {
string scriptname{argv[0]};
vector<string> args{argv + 1, argv + argc};
cliparser::make_type cli{cliparser::make(move(scriptname), move(opts))};
command_line::parser::make_type cli{command_line::parser::make(move(scriptname), move(opts))};
cli->process_input(args);
if (cli->has("quiet")) {

View file

@ -36,7 +36,8 @@ namespace modules {
if (file_util::exists((m_fstate = path_adapter + "online"))) {
m_state_reader = make_unique<state_reader>([=] { return file_util::contents(m_fstate).compare(0, 1, "1") == 0; });
} else if (file_util::exists((m_fstate = path_battery + "status"))) {
m_state_reader = make_unique<state_reader>([=] { return file_util::contents(m_fstate).compare(0, 8, "Charging") == 0; });
m_state_reader =
make_unique<state_reader>([=] { return file_util::contents(m_fstate).compare(0, 8, "Charging") == 0; });
} else {
throw module_error("No suitable way to get current charge state");
}

View file

@ -1,10 +1,10 @@
#include <curl/curl.h>
#include <curl/curlbuild.h>
#include <curl/easy.h>
#include <iostream>
#include <sstream>
#include "errors.hpp"
#include "settings.hpp"
#include "utils/http.hpp"
POLYBAR_NS

View file

@ -2,7 +2,7 @@
#include <i3ipc++/ipc.hpp>
#include "common.hpp"
#include "config.hpp"
#include "settings.hpp"
#include "utils/i3.hpp"
#include "utils/socket.hpp"
#include "utils/string.hpp"