feat(network): Add mac address token (#2569)

* Add mac address option

* Add error handling

* Address changes

* Move outside of for loop

* Update changelog
This commit is contained in:
Cameron 2022-01-15 17:39:55 -08:00 committed by GitHub
parent 6e34265d7b
commit 195a0d94df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 0 deletions

View File

@ -73,6 +73,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `DEBUG_SHADED` cmake variable and its associated functionality. - `DEBUG_SHADED` cmake variable and its associated functionality.
### Added ### Added
- `internal/network`: New token `%mac%`
([2569](https://github.com/polybar/polybar/pull/2569))
- Polybar can now read config files from stdin: `polybar -c /dev/stdin`. - Polybar can now read config files from stdin: `polybar -c /dev/stdin`.
- `custom/script`: Implement `env-*` config option. - `custom/script`: Implement `env-*` config option.
([2090](https://github.com/polybar/polybar/issues/2090)) ([2090](https://github.com/polybar/polybar/issues/2090))

View File

@ -68,6 +68,7 @@ namespace net {
struct link_status { struct link_status {
string ip; string ip;
string ip6; string ip6;
string mac;
link_activity previous{}; link_activity previous{};
link_activity current{}; link_activity current{};
}; };
@ -86,6 +87,7 @@ namespace net {
string ip() const; string ip() const;
string ip6() const; string ip6() const;
string mac() const;
string downspeed(int minwidth = 3, const string& unit = "B/s") const; string downspeed(int minwidth = 3, const string& unit = "B/s") const;
string upspeed(int minwidth = 3, const string& unit = "B/s") const; string upspeed(int minwidth = 3, const string& unit = "B/s") const;
void set_unknown_up(bool unknown = true); void set_unknown_up(bool unknown = true);

View File

@ -31,6 +31,7 @@ namespace net {
}; };
static const string NO_IP = string("N/A"); static const string NO_IP = string("N/A");
static const string NO_MAC = string("N/A");
static const string NET_PATH = "/sys/class/net/"; static const string NET_PATH = "/sys/class/net/";
static const string VIRTUAL_PATH = "/sys/devices/virtual/"; static const string VIRTUAL_PATH = "/sys/devices/virtual/";
@ -150,6 +151,11 @@ namespace net {
return false; return false;
} }
m_status.mac = string_util::trim(file_util::contents(NET_PATH + m_interface + "/address"), isspace);
if (m_status.mac == "") {
m_status.mac = NO_MAC;
}
for (auto ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) { for (auto ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == nullptr) { if (ifa->ifa_addr == nullptr) {
continue; continue;
@ -228,6 +234,12 @@ namespace net {
string network::ip() const { string network::ip() const {
return m_status.ip; return m_status.ip;
} }
/**
* Get interface mac address
*/
string network::mac() const {
return m_status.mac;
}
/** /**
* Get interface ipv6 address * Get interface ipv6 address

View File

@ -154,6 +154,7 @@ namespace modules {
label->reset_tokens(); label->reset_tokens();
label->replace_token("%ifname%", m_interface); label->replace_token("%ifname%", m_interface);
label->replace_token("%local_ip%", network->ip()); label->replace_token("%local_ip%", network->ip());
label->replace_token("%mac%", network->mac());
label->replace_token("%local_ip6%", network->ip6()); label->replace_token("%local_ip6%", network->ip6());
label->replace_token("%upspeed%", upspeed); label->replace_token("%upspeed%", upspeed);
label->replace_token("%downspeed%", downspeed); label->replace_token("%downspeed%", downspeed);