1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-11 13:50:56 -05:00
polybar/include/components/config.hpp

194 lines
5.7 KiB
C++
Raw Normal View History

2016-06-14 23:32:35 -04:00
#pragma once
#include <boost/lexical_cast.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include "common.hpp"
#include "components/logger.hpp"
#include "utils/string.hpp"
2016-11-02 15:22:45 -04:00
#include "x11/xresources.hpp"
2016-06-14 23:32:35 -04:00
LEMONBUDDY_NS
#define GET_CONFIG_VALUE(section, var, name) var = m_conf.get<decltype(var)>(section, name, var)
#define REQ_CONFIG_VALUE(section, var, name) var = m_conf.get<decltype(var)>(section, name)
2016-06-14 23:32:35 -04:00
using ptree = boost::property_tree::ptree;
DEFINE_ERROR(value_error);
DEFINE_ERROR(key_error);
class config {
public:
explicit config(const logger& logger, const xresource_manager& xrm)
: m_logger(logger), m_xrm(xrm) {}
2016-06-14 23:32:35 -04:00
2016-11-02 15:22:45 -04:00
void load(string file, string barname);
string filepath() const;
string bar_section() const;
vector<string> defined_bars() const;
string build_path(const string& section, const string& key) const;
2016-06-14 23:32:35 -04:00
/**
* Get parameter for the current bar by name
*/
template <typename T>
T get(string key) const {
return get<T>(bar_section(), key);
}
/**
* Get value of a variable by section and parameter name
*/
template <typename T>
T get(string section, string key) const {
auto val = m_ptree.get_optional<T>(build_path(section, key));
if (val == boost::none)
throw key_error("Missing parameter [" + section + "." + key + "]");
2016-06-14 23:32:35 -04:00
auto str_val = m_ptree.get<string>(build_path(section, key));
return dereference_var<T>(section, key, str_val, val.get());
}
/**
* Get value of a variable by section and parameter name
* with a default value in case the parameter isn't defined
*/
template <typename T>
T get(string section, string key, T default_value) const {
auto val = m_ptree.get_optional<T>(build_path(section, key));
auto str_val = m_ptree.get_optional<string>(build_path(section, key));
return dereference_var<T>(
section, key, str_val.get_value_or(""), val.get_value_or(default_value));
}
/**
* Get list of values for the current bar by name
*/
template <typename T>
vector<T> get_list(string key) const {
2016-06-14 23:32:35 -04:00
return get_list<T>(bar_section(), key);
}
/**
* Get list of values by section and parameter name
*/
template <typename T>
vector<T> get_list(string section, string key) const {
vector<T> vec;
optional<T> value;
while ((value = m_ptree.get_optional<T>(
build_path(section, key) + "-" + to_string(vec.size()))) != boost::none) {
auto str_val = m_ptree.get<string>(build_path(section, key) + "-" + to_string(vec.size()));
vec.emplace_back(dereference_var<T>(section, key, str_val, value.get()));
}
if (vec.empty())
throw key_error("Missing parameter [" + section + "." + key + "-0]");
2016-06-14 23:32:35 -04:00
return vec;
}
/**
* Get list of values by section and parameter name
* with a default list in case the list isn't defined
*/
template <typename T>
vector<T> get_list(string section, string key, vector<T> default_value) const {
vector<T> vec;
optional<T> value;
while ((value = m_ptree.get_optional<T>(
build_path(section, key) + "-" + to_string(vec.size()))) != boost::none) {
auto str_val = m_ptree.get<string>(build_path(section, key) + "-" + to_string(vec.size()));
vec.emplace_back(dereference_var<T>(section, key, str_val, value.get()));
}
if (vec.empty())
return default_value;
else
return vec;
}
protected:
/**
* Find value of a config parameter defined as a reference
* variable using ${section.param}
*
* ${BAR.key} may be used to reference the current bar section
* ${self.key} may be used to reference the current section
* ${env:key} may be used to reference an environment variable
* ${xrdb:key} may be used to reference a variable in the X resource db
2016-06-14 23:32:35 -04:00
*/
template <typename T>
T dereference_var(string ref_section, string ref_key, string var, const T ref_val) const {
auto n = var.find("${");
auto m = var.find("}");
if (n != 0 || m != var.length() - 1)
return ref_val;
auto path = var.substr(2, m - 2);
if (path.find("env:") == 0) {
if (has_env(path.substr(4).c_str()))
return boost::lexical_cast<T>(read_env(path.substr(4).c_str()));
return ref_val;
}
if (path.find("xrdb:") == 0) {
2016-10-19 04:06:29 -04:00
if (std::is_same<string, T>::value)
return boost::lexical_cast<T>(m_xrm.get_string(path.substr(5)));
2016-10-19 04:06:29 -04:00
else if (std::is_same<float, T>::value)
return boost::lexical_cast<T>(m_xrm.get_float(path.substr(5)));
2016-10-19 04:06:29 -04:00
else if (std::is_same<int, T>::value)
return boost::lexical_cast<T>(m_xrm.get_int(path.substr(5)));
2016-10-19 04:06:29 -04:00
return ref_val;
}
2016-06-14 23:32:35 -04:00
auto ref_path = build_path(ref_section, ref_key);
if ((n = path.find(".")) == string::npos)
throw value_error("Invalid reference defined at [" + ref_path + "]");
2016-06-14 23:32:35 -04:00
auto section = path.substr(0, n);
section = string_util::replace(section, "BAR", bar_section());
section = string_util::replace(section, "self", ref_section);
2016-06-14 23:32:35 -04:00
auto key = path.substr(n + 1, path.length() - n - 1);
auto val = m_ptree.get_optional<T>(build_path(section, key));
if (val == boost::none)
throw value_error("Unexisting reference defined at [" + ref_path + "]");
2016-06-14 23:32:35 -04:00
auto str_val = m_ptree.get<string>(build_path(section, key));
return dereference_var<T>(section, key, str_val, val.get());
}
private:
const logger& m_logger;
const xresource_manager& m_xrm;
2016-06-14 23:32:35 -04:00
ptree m_ptree;
string m_file;
string m_current_bar;
};
namespace {
/**
* Configure injection module
*/
template <typename T = const config&>
di::injector<T> configure_config() {
return di::make_injector(configure_logger(), configure_xresource_manager());
}
}
2016-06-14 23:32:35 -04:00
LEMONBUDDY_NS_END