forked from WayfireWM/wf-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand-output.cpp
More file actions
195 lines (162 loc) · 5.04 KB
/
Copy pathcommand-output.cpp
File metadata and controls
195 lines (162 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <gtkmm/cssprovider.h>
#include <glibmm/main.h>
#include <glibmm/shell.h>
#include <glibmm/spawn.h>
#include <gtkmm/tooltip.h>
#include <array>
#include <ctime>
#include <gtk-utils.hpp>
#include <string>
#include "command-output.hpp"
static sigc::connection label_set_from_command(std::string command_line,
Gtk::Label& label, Gtk::Image *icon = nullptr,
const std::string& icon_name = "")
{
command_line = "/bin/sh -c \"" + command_line + "\"";
Glib::Pid pid;
int output_fd;
Glib::spawn_async_with_pipes("", Glib::shell_parse_argv(command_line),
Glib::SpawnFlags::DO_NOT_REAP_CHILD | Glib::SpawnFlags::SEARCH_PATH_FROM_ENVP,
Glib::SlotSpawnChildSetup{}, &pid, nullptr, &output_fd, nullptr);
return Glib::signal_child_watch().connect([=, &label] (Glib::Pid pid, int exit_status)
{
FILE *file = fdopen(output_fd, "r");
Glib::ustring output;
std::array<char, 16> buffer;
while (std::fgets(buffer.data(), buffer.size(), file))
{
output += buffer.data();
}
std::fclose(file);
Glib::spawn_close_pid(pid);
while (!output.empty() && std::isspace(*output.rbegin()))
{
output.erase(std::prev(output.end()));
}
label.set_markup(output);
if (icon &&
!icon_name.empty() &&
((icon_name[0] == '/') ||
(icon_name.rfind("~/", 0) == 0)))
{
IconProvider::image_set_icon(*icon, icon_name);
}
}, pid);
}
WfCommandOutputButtons::CommandOutput::CommandOutput(const std::string & name,
const std::string & command, const std::string & tooltip_command, int period,
const std::string & icon_name, int icon_size,
const std::string & icon_position)
{
this->tooltip_command = tooltip_command;
IconProvider::image_set_icon(icon, icon_name);
if (icon_size > 0)
{
css_provider = Gtk::CssProvider::create();
css_provider->load_from_string(".command-icon-" + name + "{-gtk-icon-size:" + std::to_string(
icon_size) + "px;}");
icon.add_css_class("command-icon-" + name);
icon.get_style_context()->add_provider(css_provider, GTK_STYLE_PROVIDER_PRIORITY_USER);
}
add_css_class("flat");
icon.add_css_class("widget-icon");
add_css_class("command-output");
add_css_class("icon-" + icon_position);
main_label.set_ellipsize(Pango::EllipsizeMode::END);
main_label.set_max_width_chars(max_chars_opt);
max_chars_opt.set_callback([this]
{
main_label.set_max_width_chars(max_chars_opt);
});
box.set_orientation(
icon_position == "bottom" ||
icon_position ==
"top" ? Gtk::Orientation::VERTICAL : Gtk::Orientation::HORIZONTAL);
if ((icon_position == "right") || (icon_position == "bottom"))
{
box.append(main_label);
box.append(icon);
} else
{
box.append(icon);
box.append(main_label);
}
if (icon_name.empty())
{
box.remove(icon);
}
set_child(box);
const auto update_output = [=] ()
{
command_sig.disconnect();
command_sig = label_set_from_command(command, main_label, &icon, icon_name);
};
signals.push_back(signal_clicked().connect(update_output));
if (period > 0)
{
timeout_connection = Glib::signal_timeout().connect_seconds([=] ()
{
update_output();
return true;
}, period);
}
if (period != -1)
{
update_output();
}
if (!tooltip_command.empty())
{
set_has_tooltip();
tooltip_label.show();
signals.push_back(signal_query_tooltip().connect(sigc::mem_fun(*this,
&WfCommandOutputButtons::CommandOutput::query_tooltip), false));
}
}
WfCommandOutputButtons::CommandOutput::~CommandOutput()
{
timeout_connection.disconnect();
for (auto signal : signals)
{
signal.disconnect();
}
}
bool WfCommandOutputButtons::CommandOutput::query_tooltip(int i, int j, bool k,
const std::shared_ptr<Gtk::Tooltip>& tooltip)
{
this->update_tooltip();
tooltip->set_custom(tooltip_label);
return true;
}
void WfCommandOutputButtons::CommandOutput::update_tooltip()
{
if (std::time(nullptr) - last_tooltip_update < 1)
{
return;
}
label_set_from_command(tooltip_command, tooltip_label);
}
void WfCommandOutputButtons::init(Gtk::Box *container)
{
box.add_css_class("command-output-box");
container->append(box);
update_buttons();
commands_list_opt.set_callback([=] { update_buttons(); });
}
void WfCommandOutputButtons::update_buttons()
{
const auto & opt_value = commands_list_opt.value();
for (auto child : box.get_children())
{
box.remove(*child);
}
buttons.clear();
buttons.reserve(opt_value.size());
for (const auto & command_info : opt_value)
{
buttons.push_back(std::apply([] (auto&&... args)
{
return std::make_unique<CommandOutput>(args...);
}, command_info));
box.append(*buttons.back());
}
}