From b417c9f8120498c41a962a9c6be2a43c9123ef0f Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Mon, 19 Dec 2016 22:01:37 +0100 Subject: [PATCH] feat(github): New module Module used to query the GitHub API for information. Currently only supports notification count. Ref #84 --- cmake/build/options.cmake | 9 +++ cmake/build/summary.cmake | 1 + include/components/config.hpp | 18 ++++++ include/config.hpp.cmake | 2 + include/modules/github.hpp | 30 ++++++++++ include/modules/meta/base.inl | 23 ++++---- include/modules/meta/factory.hpp | 7 ++- include/modules/meta/timer_module.inl | 7 ++- include/modules/unsupported.hpp | 7 ++- include/utils/http.hpp | 30 ++++++++++ src/CMakeLists.txt | 13 +++++ src/modules/github.cpp | 79 +++++++++++++++++++++++++++ src/utils/http.cpp | 51 +++++++++++++++++ 13 files changed, 260 insertions(+), 17 deletions(-) create mode 100644 include/modules/github.hpp create mode 100644 include/utils/http.hpp create mode 100644 src/modules/github.cpp create mode 100644 src/utils/http.cpp diff --git a/cmake/build/options.cmake b/cmake/build/options.cmake index aedc759c..8a2c457b 100644 --- a/cmake/build/options.cmake +++ b/cmake/build/options.cmake @@ -33,6 +33,14 @@ if(NOT DEFINED ENABLE_I3 AND NOT I3_BINARY) set(ENABLE_I3 OFF CACHE STRING "Module support for i3wm") endif() +# }}} +# Default value for: ENABLE_CURL {{{ + +find_package(CURL QUIET) +if(NOT DEFINED ENABLE_CURL AND NOT CURL_FOUND) + set(ENABLE_CURL OFF CACHE STRING "Module support for libcurl") +endif() + # }}} # Define build options {{{ @@ -47,6 +55,7 @@ option(DEBUG_HINTS "Enable hints rendering" OFF) option(ENABLE_CCACHE "Enable ccache support" OFF) option(ENABLE_ALSA "Enable alsa support" ON) +option(ENABLE_CURL "Enable curl support" ON) option(ENABLE_I3 "Enable i3 support" ON) option(ENABLE_MPD "Enable mpd support" ON) option(ENABLE_NETWORK "Enable network support" ON) diff --git a/cmake/build/summary.cmake b/cmake/build/summary.cmake index 2e19976b..2a690e6f 100644 --- a/cmake/build/summary.cmake +++ b/cmake/build/summary.cmake @@ -51,6 +51,7 @@ colored_option(STATUS " Draw debug hints ${DEBUG_HINTS}" DEBUG_HINTS "32;1" colored_option(STATUS " Enable ccache ${ENABLE_CCACHE}" ENABLE_CCACHE "32;1" "37;2") message(STATUS "--------------------------") colored_option(STATUS " Enable alsa ${ENABLE_ALSA}" ENABLE_ALSA "32;1" "37;2") +colored_option(STATUS " Enable curl ${ENABLE_CURL}" ENABLE_CURL "32;1" "37;2") colored_option(STATUS " Enable i3 ${ENABLE_I3}" ENABLE_I3 "32;1" "37;2") colored_option(STATUS " Enable mpd ${ENABLE_MPD}" ENABLE_MPD "32;1" "37;2") colored_option(STATUS " Enable network ${ENABLE_NETWORK}" ENABLE_NETWORK "32;1" "37;2") diff --git a/include/components/config.hpp b/include/components/config.hpp index 0f3b4546..b91d252c 100644 --- a/include/components/config.hpp +++ b/include/components/config.hpp @@ -5,6 +5,7 @@ #include "components/logger.hpp" #include "errors.hpp" #include "utils/env.hpp" +#include "utils/file.hpp" #include "utils/string.hpp" #include "x11/xresources.hpp" @@ -196,6 +197,8 @@ class config { return dereference_env(path.substr(4), fallback); } else if (path.compare(0, 5, "xrdb:") == 0) { return dereference_xrdb(path.substr(5), fallback); + } else if (path.compare(0, 5, "file:") == 0) { + return dereference_file(path.substr(5), fallback); } else if ((pos = path.find(".")) != string::npos) { return dereference_local(path.substr(0, pos), path.substr(pos + 1), section); } else { @@ -274,6 +277,21 @@ class config { return str.empty() ? fallback : convert(move(str)); } + /** + * Dereference file reference by reading its contents + * ${file:/absolute/file/path} + */ + template + T dereference_file(string var, const T& fallback) const { + string filename{move(var)}; + + if (file_util::exists(filename)) { + return convert(string_util::trim(file_util::get_contents(filename), '\n')); + } + + return fallback; + } + private: static constexpr const char* KEY_INHERIT{"inherit"}; diff --git a/include/config.hpp.cmake b/include/config.hpp.cmake index 2779a748..6b94aa31 100644 --- a/include/config.hpp.cmake +++ b/include/config.hpp.cmake @@ -16,6 +16,7 @@ #cmakedefine01 ENABLE_MPD #cmakedefine01 ENABLE_NETWORK #cmakedefine01 ENABLE_I3 +#cmakedefine01 ENABLE_CURL #cmakedefine01 WITH_XRANDR #cmakedefine01 WITH_XRENDER @@ -78,6 +79,7 @@ auto print_build_info = [](bool extended = false) { << "\n" << "Features: " << (ENABLE_ALSA ? "+" : "-") << "alsa " + << (ENABLE_CURL ? "+" : "-") << "curl " << (ENABLE_I3 ? "+" : "-") << "i3 " << (ENABLE_MPD ? "+" : "-") << "mpd " << (ENABLE_NETWORK ? "+" : "-") << "network " diff --git a/include/modules/github.hpp b/include/modules/github.hpp new file mode 100644 index 00000000..79080cc8 --- /dev/null +++ b/include/modules/github.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include "config.hpp" +#include "modules/meta/timer_module.hpp" +#include "utils/http.hpp" + +POLYBAR_NS + +namespace modules { + /** + * Module used to query the GitHub API for notification count + */ + class github_module : public timer_module { + public: + explicit github_module(const bar_settings&, string); + + void setup(); + bool update(); + bool build(builder* builder, const string& tag) const; + + private: + static constexpr auto TAG_LABEL = "