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 build(builder* builder, const string& tag) const;
string get_format() const;
private:
void update_label(int);
int get_number_of_notification();
string request();
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_offline{};
string m_api_url;
string m_accesstoken{};
unique_ptr<http_downloader> m_http{};
bool m_empty_notifications{false};
std::atomic<bool> m_offline{false};
};
}

View File

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