Allow running polybar without BAR argument (#2526)

* Allow running polybar without BAR argument

The configuration file must declare a single bar to be able to not name
it.

* Check barname not in config and improve error message

* Revert README changes

* Add no bar in config file error detection
This commit is contained in:
dvermd 2021-10-07 14:48:47 +02:00 committed by GitHub
parent 282b0f4e73
commit 47b5910eda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 48 additions and 10 deletions

View File

@ -159,6 +159,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Increased the double click interval from 150ms to 400ms. - Increased the double click interval from 150ms to 400ms.
- Stop ignoring actions if they arrive while the previous one hasn't been processed yet. - Stop ignoring actions if they arrive while the previous one hasn't been processed yet.
([`#2469`](https://github.com/polybar/polybar/issues/2469)) ([`#2469`](https://github.com/polybar/polybar/issues/2469))
- Polybar can now be run without passing the bar name as argument given that
the configuration file only defines one bar
([`#2525`](https://github.com/polybar/polybar/issues/2525))
### Fixed ### Fixed
- `custom/script`: Concurrency issues with fast-updating tailed scripts. - `custom/script`: Concurrency issues with fast-updating tailed scripts.

View File

@ -3,11 +3,12 @@ polybar(1)
SYNOPSIS SYNOPSIS
-------- --------
**polybar** [*OPTIONS*]... *BAR* **polybar** [*OPTIONS*]... [*BAR*]
DESCRIPTION DESCRIPTION
----------- -----------
Polybar aims to help users build beautiful and highly customizable status bars for their desktop environment, without the need of having a black belt in shell scripting. Polybar aims to help users build beautiful and highly customizable status bars for their desktop environment, without the need of having a black belt in shell scripting.
If the *BAR* argument is not provided and the configuration file only contains one bar definition, Polybar will display this bar.
OPTIONS OPTIONS
------- -------

View File

@ -33,6 +33,8 @@ class config {
const string& filepath() const; const string& filepath() const;
string section() const; string section() const;
static constexpr const char* BAR_PREFIX = "bar/";
/** /**
* \brief Instruct the config to connect to the xresource manager * \brief Instruct the config to connect to the xresource manager
*/ */

View File

@ -215,6 +215,8 @@ class config_parser {
*/ */
bool is_valid_name(const string& name); bool is_valid_name(const string& name);
vector<string> get_bars(const sectionmap_t& sections) const;
/** /**
* \brief Whether or not an xresource manager should be used * \brief Whether or not an xresource manager should be used
* *

View File

@ -9,7 +9,7 @@ namespace command_line {
* Create instance * Create instance
*/ */
parser::make_type parser::make(string&& scriptname, const options&& opts) { parser::make_type parser::make(string&& scriptname, const options&& opts) {
return std::make_unique<parser>("Usage: " + scriptname + " [OPTION]... BAR", forward<decltype(opts)>(opts)); return std::make_unique<parser>("Usage: " + scriptname + " [OPTION]... [BAR]", forward<decltype(opts)>(opts));
} }
/** /**

View File

@ -31,7 +31,7 @@ const string& config::filepath() const {
* Get the section name of the bar in use * Get the section name of the bar in use
*/ */
string config::section() const { string config::section() const {
return "bar/" + m_barname; return BAR_PREFIX + m_barname;
} }
void config::use_xrm() { void config::use_xrm() {

View File

@ -20,8 +20,23 @@ config::make_type config_parser::parse() {
sectionmap_t sections = create_sectionmap(); sectionmap_t sections = create_sectionmap();
if (sections.find("bar/" + m_barname) == sections.end()) { vector<string> bars = get_bars(sections);
throw application_error("Undefined bar: " + m_barname); if (m_barname.empty()) {
if (bars.size() == 1) {
m_barname = bars[0];
} else if (bars.empty()) {
throw application_error("The config file contains no bar.");
} else {
throw application_error("The config file contains multiple bars, but no bar name was given. Available bars: " +
string_util::join(bars, ", "));
}
} else if (sections.find("bar/" + m_barname) == sections.end()) {
if (bars.empty()) {
throw application_error("Undefined bar: " + m_barname + ". The config file contains no bar.");
} else {
throw application_error(
"Undefined bar: " + m_barname + ". Available bars: " + string_util::join(get_bars(sections), ", "));
}
} }
/* /*
@ -80,6 +95,19 @@ sectionmap_t config_parser::create_sectionmap() {
return sections; return sections;
} }
/**
* Get the bars declared
*/
vector<string> config_parser::get_bars(const sectionmap_t& sections) const {
vector<string> bars;
for (const auto& it : sections) {
if (it.first.find(config::BAR_PREFIX) == 0) {
bars.push_back(it.first.substr(strlen(config::BAR_PREFIX)));
}
}
return bars;
}
void config_parser::parse_file(const string& file, file_list path) { void config_parser::parse_file(const string& file, file_list path) {
if (std::find(path.begin(), path.end(), file) != path.end()) { if (std::find(path.begin(), path.end(), file) != path.end()) {
string path_str{}; string path_str{};

View File

@ -98,10 +98,7 @@ int main(int argc, char** argv) {
string confpath; string confpath;
// Make sure a bar name is passed in // Make sure a bar name is passed in
if (!cli->has(0)) { if (cli->has(1)) {
cli->usage();
return EXIT_FAILURE;
} else if (cli->has(1)) {
fprintf(stderr, "Unrecognized argument \"%s\"\n", cli->get(1).c_str()); fprintf(stderr, "Unrecognized argument \"%s\"\n", cli->get(1).c_str());
cli->usage(); cli->usage();
return EXIT_FAILURE; return EXIT_FAILURE;
@ -123,7 +120,12 @@ int main(int argc, char** argv) {
throw application_error("Define configuration using --config=PATH"); throw application_error("Define configuration using --config=PATH");
} }
config_parser parser{logger, move(confpath), cli->get(0)}; string barname;
if (cli->has(0)) {
barname = cli->get(0);
}
config_parser parser{logger, move(confpath), move(barname)};
config::make_type conf = parser.parse(); config::make_type conf = parser.parse();
//================================================== //==================================================