polybar/src/components/config.cpp

93 lines
2.2 KiB
C++
Raw Normal View History

2016-11-20 22:04:31 +00:00
#include <algorithm>
2016-11-25 12:55:15 +00:00
#include <utility>
2016-11-20 22:04:31 +00:00
2016-11-02 19:22:45 +00:00
#include "components/config.hpp"
2016-11-20 22:04:31 +00:00
#include "utils/env.hpp"
2016-11-22 02:01:50 +00:00
#include "utils/file.hpp"
2016-11-02 19:22:45 +00:00
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-11-02 19:22:45 +00:00
/**
* Load configuration and validate bar section
*
* This is done outside the constructor due to boost::di noexcept
*/
void config::load(string file, string barname) {
m_file = file;
2016-11-25 12:55:15 +00:00
m_current_bar = move(barname);
2016-11-02 19:22:45 +00:00
2016-11-25 12:55:15 +00:00
if (!file_util::exists(file)) {
2016-11-02 19:22:45 +00:00
throw application_error("Could not find config file: " + file);
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
try {
boost::property_tree::read_ini(file, m_ptree);
} catch (const std::exception& e) {
throw application_error(e.what());
}
auto bars = defined_bars();
2016-11-25 12:55:15 +00:00
if (std::find(bars.begin(), bars.end(), m_current_bar) == bars.end()) {
2016-11-02 19:22:45 +00:00
throw application_error("Undefined bar: " + m_current_bar);
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
2016-11-25 12:55:15 +00:00
if (env_util::has("XDG_CONFIG_HOME")) {
2016-11-20 22:04:31 +00:00
file = string_util::replace(file, env_util::get("XDG_CONFIG_HOME"), "$XDG_CONFIG_HOME");
2016-11-25 12:55:15 +00:00
}
if (env_util::has("HOME")) {
2016-11-20 22:04:31 +00:00
file = string_util::replace(file, env_util::get("HOME"), "~");
2016-11-25 12:55:15 +00:00
}
2016-11-20 22:04:31 +00:00
2016-11-02 19:22:45 +00:00
m_logger.trace("config: Loaded %s", file);
m_logger.trace("config: Current bar section: [%s]", bar_section());
}
/**
* Get path of loaded file
*/
string config::filepath() const {
return m_file;
}
/**
* Get the section name of the bar in use
*/
string config::bar_section() const {
return "bar/" + m_current_bar;
}
/**
* Get a list of defined bar sections in the current config
*/
vector<string> config::defined_bars() const {
vector<string> bars;
for (auto&& p : m_ptree) {
2016-11-25 12:55:15 +00:00
if (p.first.compare(0, 4, "bar/") == 0) {
2016-11-02 19:22:45 +00:00
bars.emplace_back(p.first.substr(4));
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
}
return bars;
}
/**
* Build path used to find a parameter in the given section
*/
string config::build_path(const string& section, const string& key) const {
return section + "." + key;
}
2016-11-22 02:01:50 +00:00
/**
* Print a deprecation warning if the given parameter is set
*/
2016-11-25 12:55:15 +00:00
void config::warn_deprecated(const string& section, const string& key, string replacement) const {
2016-11-22 02:01:50 +00:00
try {
auto value = get<string>(section, key);
2016-11-25 12:55:15 +00:00
m_logger.warn("The config parameter `%s.%s` is deprecated, use `%s` instead.", section, key, move(replacement));
2016-11-22 02:01:50 +00:00
} catch (const key_error& err) {
}
}
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END