refactor(bspwm): Cleanup

This commit is contained in:
Michael Carlberg 2016-12-14 07:45:29 +01:00
parent 18597f8e1d
commit b156d1bbf4
4 changed files with 30 additions and 31 deletions

View File

@ -33,7 +33,7 @@ namespace modules {
vector<label_t> modes;
label_t label;
string name;
bool focused = false;
bool focused{false};
};
using event_module::event_module;
@ -72,13 +72,13 @@ namespace modules {
label_t m_monitorlabel;
iconset_t m_icons;
bool m_click = true;
bool m_scroll = true;
bool m_pinworkspaces = true;
unsigned long m_hash;
bool m_click{true};
bool m_scroll{true};
bool m_pinworkspaces{true};
string_util::hash_type m_hash{0U};
// used while formatting output
size_t m_index = 0;
size_t m_index{0U};
};
}

View File

@ -19,6 +19,7 @@ namespace socket_util {
ssize_t send(const void* data, size_t len, int flags = 0);
ssize_t send(const string& data, int flags = 0);
string receive(const ssize_t receive_bytes, int flags = 0);
string receive(const ssize_t receive_bytes, ssize_t* bytes_received, int flags = 0);
bool peek(const size_t peek_bytes);

View File

@ -3,8 +3,8 @@
#include "drawtypes/iconset.hpp"
#include "drawtypes/label.hpp"
#include "modules/bspwm.hpp"
#include "utils/file.hpp"
#include "utils/factory.hpp"
#include "utils/file.hpp"
#include "modules/meta/base.inl"
#include "modules/meta/event_module.inl"
@ -152,41 +152,31 @@ namespace modules {
return false;
}
ssize_t bytes{0};
string data = m_subscriber->receive(BUFSIZ - 1, &bytes, 0);
if (bytes == 0) {
return false;
}
data = string_util::strip_trailing_newline(data);
unsigned long pos;
while ((pos = data.find('\n')) != string::npos) {
data.erase(pos);
}
string data{string_util::replace_all(m_subscriber->receive(BUFSIZ), "\n", "")};
if (data.empty()) {
return false;
}
const auto prefix = string{BSPWM_STATUS_PREFIX};
size_t pos;
// If there were more than 1 row available in the channel
// we'll strip out the old updates
if ((pos = data.find_last_of(prefix)) > 0) {
data = data.substr(pos);
if ((pos = data.find_last_of(BSPWM_STATUS_PREFIX)) > 0) {
data.erase(0, pos);
}
if (data.compare(0, prefix.length(), prefix) != 0) {
size_t prefix_len{strlen(BSPWM_STATUS_PREFIX)};
if (data.compare(0, prefix_len, BSPWM_STATUS_PREFIX) != 0) {
m_log.err("%s: Unknown status '%s'", name(), data);
return false;
}
unsigned long hash;
string_util::hash_type hash;
if ((hash = string_util::hash(data)) == m_hash) {
return false;
}
m_hash = hash;
// Extract the string for the defined monitor
@ -194,8 +184,8 @@ namespace modules {
const auto needle_active = ":M" + m_bar.monitor->name + ":";
const auto needle_inactive = ":m" + m_bar.monitor->name + ":";
if ((pos = data.find(prefix)) != string::npos) {
data = data.replace(pos, prefix.length(), ":");
if ((pos = data.find(BSPWM_STATUS_PREFIX)) != string::npos) {
data = data.replace(pos, prefix_len, ":");
}
if ((pos = data.find(needle_active)) != string::npos) {
data.erase(0, pos + 1);
@ -209,16 +199,16 @@ namespace modules {
if ((pos = data.find(":M", 1)) != string::npos) {
data.erase(pos);
}
} else if ((pos = data.find(prefix)) != string::npos) {
data = data.replace(pos, prefix.length(), ":");
} else if ((pos = data.find(BSPWM_STATUS_PREFIX)) != string::npos) {
data = data.replace(pos, prefix_len, ":");
} else {
return false;
}
size_t workspace_n{0U};
m_monitors.clear();
size_t workspace_n{0U};
for (auto&& tag : string_util::split(data, ':')) {
if (tag.empty()) {
continue;

View File

@ -84,6 +84,14 @@ namespace socket_util {
return string{buffer};
}
/**
* @see receive
*/
string unix_connection::receive(const ssize_t receive_bytes, int flags) {
ssize_t bytes{0};
return receive(receive_bytes, &bytes, flags);
}
/**
* Peek at the specified number of bytes
*/