xaizek / d2if (License: GPLv2+) (since 2018-12-07)
Simple dzen2 input formatter, which is supposed to not waste CPU time for nothing and provides a number of builtin "widgets" for displaying system information.
Commit e8a3ce47f3d45015eb7404bc5ed144dded280db9

Use unique_ptr instead of shared_ptr in StatusBar
Author: xaizek
Author date (UTC): 2016-03-17 14:49
Committer name: xaizek
Committer date (UTC): 2016-03-17 14:49
Parent(s): b883204a0376308b2250c4817b46a12ec8dfc5a4
Signing key:
Tree: 2332b480c70b6fdf9d4b1a88cb4e1fef05a42a59
File Lines added Lines deleted
src/StatusBar.cpp 3 8
src/StatusBar.hpp 2 4
src/d2if.cpp 53 23
File src/StatusBar.cpp changed (mode: 100644) (index d171b97..1d1ef22)
28 28 #include "DelimOstreamIterator.hpp" #include "DelimOstreamIterator.hpp"
29 29 #include "Field.hpp" #include "Field.hpp"
30 30
31 StatusBar::StatusBar(std::initializer_list<std::shared_ptr<Field>> fields)
32 :fields(fields.begin(), fields.end())
31 StatusBar::StatusBar(std::vector<std::unique_ptr<Field>> fields)
32 : fields(std::move(fields))
33 33 { {
34 34 } }
35 35
36 void StatusBar::addField(std::shared_ptr<Field> field)
37 {
38 fields.push_back(field);
39 }
40
41 36 void StatusBar::setFieldDelimiter(const std::string &delimiter) void StatusBar::setFieldDelimiter(const std::string &delimiter)
42 37 { {
43 38 fieldDelimiter = delimiter; fieldDelimiter = delimiter;
 
... ... std::string StatusBar::getText() const
58 53 std::vector<Field*> visibleFields; std::vector<Field*> visibleFields;
59 54 visibleFields.reserve(fields.size()); visibleFields.reserve(fields.size());
60 55
61 for (const std::shared_ptr<Field> &field : fields) {
56 for (const std::unique_ptr<Field> &field : fields) {
62 57 field->update(); field->update();
63 58 if (field->isVisible()) { if (field->isVisible()) {
64 59 visibleFields.push_back(field.get()); visibleFields.push_back(field.get());
File src/StatusBar.hpp changed (mode: 100644) (index dc6044f..5f30492)
28 28 class StatusBar class StatusBar
29 29 { {
30 30 public: public:
31 StatusBar(std::initializer_list<std::shared_ptr<Field>> fields);
31 StatusBar(std::vector<std::unique_ptr<Field>> fields);
32 32
33 33 // These operations are forbidden. // These operations are forbidden.
34 34 StatusBar(StatusBar &rhs) = delete; StatusBar(StatusBar &rhs) = delete;
35 35 StatusBar & operator=(StatusBar &rhs) = delete; StatusBar & operator=(StatusBar &rhs) = delete;
36 36
37 void addField(std::shared_ptr<Field> field);
38
39 37 void setFieldDelimiter(const std::string &delimiter); void setFieldDelimiter(const std::string &delimiter);
40 38 void setFieldDelimiterColor(const std::string &delimiterColor); void setFieldDelimiterColor(const std::string &delimiterColor);
41 39
42 40 std::string getText() const; std::string getText() const;
43 41
44 42 private: private:
45 std::vector<std::shared_ptr<Field>> fields;
43 const std::vector<std::unique_ptr<Field>> fields;
46 44 std::string fieldDelimiter; std::string fieldDelimiter;
47 45 std::string fieldDelimiterColor; std::string fieldDelimiterColor;
48 46 std::string colorCache { "^fg()" }; std::string colorCache { "^fg()" };
File src/d2if.cpp changed (mode: 100644) (index f25990b..3e137d2)
19 19
20 20 #include <chrono> #include <chrono>
21 21 #include <iostream> #include <iostream>
22 #include <utility>
22 23
23 24 #include "Battery.hpp" #include "Battery.hpp"
24 25 #include "ColorScheme.hpp" #include "ColorScheme.hpp"
 
34 35 #include "Timer.hpp" #include "Timer.hpp"
35 36 #include "Volume.hpp" #include "Volume.hpp"
36 37
38 template <typename T, typename... Args>
39 inline std::unique_ptr<T>
40 make_unique(Args &&...args)
41 {
42 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
43 }
44
45 template <typename T, typename... Args>
46 inline void
47 make_unique_vector(std::vector<std::unique_ptr<T>> &)
48 {
49 }
50
51 template <typename T, typename... Args>
52 inline void
53 make_unique_vector(std::vector<std::unique_ptr<T>> &v, std::unique_ptr<T> &&x,
54 Args &&...args)
55 {
56 v.emplace_back(std::move(x));
57 make_unique_vector<T>(v, std::forward<Args>(args)...);
58 }
59
60 template <typename T, typename... Args>
61 inline std::vector<std::unique_ptr<T>>
62 make_unique_vector(std::unique_ptr<T> &&x, Args &&...args)
63 {
64 std::vector<std::unique_ptr<T>> v;
65 make_unique_vector<T>(v, std::move(x), std::forward<Args>(args)...);
66 return v;
67 }
68
37 69 int main(void) int main(void)
38 70 { {
39 const ColorScheme colorScheme = {
40 {
41 { "bg", "black" },
42 { "fg", "white" },
43 { "fg-bad", "black" },
44 { "fg-bar", "#A6F09D" },
45 { "fg-bar-good", "#65A765" },
46 { "fg-bar-bad", "#FF0000" },
47 }
48 };
71 const ColorScheme colorScheme = {{
72 { "bg", "black" },
73 { "fg", "white" },
74 { "fg-bad", "black" },
75 { "fg-bar", "#A6F09D" },
76 { "fg-bar-good", "#65A765" },
77 { "fg-bar-bad", "#FF0000" },
78 }};
49 79
50 StatusBar statusBar = {
51 std::make_shared<Time>(colorScheme),
52 std::make_shared<Desktop>(colorScheme),
53 std::make_shared<Layout>(colorScheme),
54 std::make_shared<Memory>(colorScheme),
55 std::make_shared<Cpu>(colorScheme),
56 std::make_shared<Display>(colorScheme),
57 std::make_shared<Volume>(colorScheme, "default"),
58 std::make_shared<Battery>(colorScheme),
59 std::make_shared<Network>(colorScheme),
60 std::make_shared<Player>(colorScheme, "127.0.0.1", 6600),
61 };
80 StatusBar statusBar(make_unique_vector<Field>(
81 make_unique<Time>(colorScheme),
82 make_unique<Desktop>(colorScheme),
83 make_unique<Layout>(colorScheme),
84 make_unique<Memory>(colorScheme),
85 make_unique<Cpu>(colorScheme),
86 make_unique<Display>(colorScheme),
87 make_unique<Volume>(colorScheme, "default"),
88 make_unique<Battery>(colorScheme),
89 make_unique<Network>(colorScheme),
90 make_unique<Player>(colorScheme, "127.0.0.1", 6600)
91 ));
62 92
63 93 statusBar.setFieldDelimiter(" | "); statusBar.setFieldDelimiter(" | ");
64 94 statusBar.setFieldDelimiterColor(colorScheme.getColor("fg")); statusBar.setFieldDelimiterColor(colorScheme.getColor("fg"));
65 95
66 Timer timer {
96 Timer timer = {
67 97 [&]() { [&]() {
68 98 std::cout << statusBar.getText() << std::endl; std::cout << statusBar.getText() << std::endl;
69 99 } }
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/d2if

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/d2if

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master