feat(github): offline label (#1825)

Adds `format-offline` and `label-offline`

* feat(github): offline label & fixes

* Clear label if there are no notifications and empty-notifications = false

* clang-format

Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
This commit is contained in:
Cooper Pierce 2020-02-21 07:58:23 -05:00 committed by GitHub
parent 8d2b0d2747
commit 683cfc0738
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 11 deletions

View File

@ -16,18 +16,24 @@ namespace modules {
bool update(); bool update();
bool build(builder* builder, const string& tag) const; bool build(builder* builder, const string& tag) const;
string get_format() const;
private: private:
void update_label(int); void update_label(int);
int get_number_of_notification(); int get_number_of_notification();
string request(); string request();
static constexpr auto TAG_LABEL = "<label>"; static constexpr auto TAG_LABEL = "<label>";
static constexpr auto TAG_LABEL_OFFLINE = "<label-offline>";
static constexpr auto FORMAT_OFFLINE = "format-offline";
label_t m_label{}; label_t m_label{};
label_t m_label_offline{};
string m_api_url; string m_api_url;
string m_accesstoken{}; string m_accesstoken{};
unique_ptr<http_downloader> m_http{}; unique_ptr<http_downloader> m_http{};
bool m_empty_notifications{false}; bool m_empty_notifications{false};
std::atomic<bool> m_offline{false};
}; };
} }

View File

@ -1,10 +1,10 @@
#include "modules/github.hpp"
#include <cassert> #include <cassert>
#include "drawtypes/label.hpp" #include "drawtypes/label.hpp"
#include "modules/github.hpp"
#include "utils/concurrency.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "utils/concurrency.hpp"
POLYBAR_NS POLYBAR_NS
@ -29,10 +29,16 @@ namespace modules {
if (m_formatter->has(TAG_LABEL)) { if (m_formatter->has(TAG_LABEL)) {
m_label = load_optional_label(m_conf, name(), TAG_LABEL, "Notifications: %notifications%"); m_label = load_optional_label(m_conf, name(), TAG_LABEL, "Notifications: %notifications%");
}
m_formatter->add(FORMAT_OFFLINE, TAG_LABEL_OFFLINE, {TAG_LABEL_OFFLINE});
if (m_formatter->has(TAG_LABEL_OFFLINE)) {
m_label_offline = load_optional_label(m_conf, name(), TAG_LABEL_OFFLINE, "Offline");
}
update_label(0); update_label(0);
} }
}
/** /**
* Update module contents * Update module contents
@ -68,10 +74,15 @@ namespace modules {
try { try {
content = request(); content = request();
} catch (application_error& e) { } catch (application_error& e) {
m_log.warn("%s: cannot complete the request to github: %s", name(), e.what()); if (!m_offline) {
m_log.info("%s: cannot complete the request to github: %s", name(), e.what());
}
m_offline = true;
return -1; return -1;
} }
m_offline = false;
size_t pos{0}; size_t pos{0};
size_t notifications{0}; size_t notifications{0};
@ -82,7 +93,15 @@ namespace modules {
return notifications; return notifications;
} }
string github_module::get_format() const {
return m_offline ? FORMAT_OFFLINE : DEFAULT_FORMAT;
}
void github_module::update_label(const int notifications) { void github_module::update_label(const int notifications) {
if (!m_label) {
return;
}
if (0 != notifications || m_empty_notifications) { if (0 != notifications || m_empty_notifications) {
m_label->reset_tokens(); m_label->reset_tokens();
m_label->replace_token("%notifications%", to_string(notifications)); m_label->replace_token("%notifications%", to_string(notifications));
@ -95,12 +114,16 @@ namespace modules {
* Build module content * Build module content
*/ */
bool github_module::build(builder* builder, const string& tag) const { bool github_module::build(builder* builder, const string& tag) const {
if (tag != TAG_LABEL) if (tag == TAG_LABEL) {
return false;
builder->node(m_label); builder->node(m_label);
return true; return true;
} else if (tag == TAG_LABEL_OFFLINE) {
builder->node(m_label_offline);
return true;
} }
}
return false;
}
} // namespace modules
POLYBAR_NS_END POLYBAR_NS_END