Skip to content

Commit

Permalink
Use different format strings for each auto-detected network interface (
Browse files Browse the repository at this point in the history
…enkore#565)

* network - Use different format strings for each autodetected interface

Introduce format_active_up dictionary. Each key can be a valid globbing pattern
for fnmatch.fnmatch().

Fall back to format_up if no match could be found.

* network - Always honor disabled dynamic_color

Disable the calculation of color values if detect_active is enabled
and dynamic_color is disabled.

This resulted in "#000000" for some reason.
  • Loading branch information
rscholer authored and facetoe committed Apr 23, 2017
1 parent c6464ae commit 811ade8
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions i3pystatus/network.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from fnmatch import fnmatch

import netifaces

from i3pystatus import IntervalModule, formatp
Expand Down Expand Up @@ -284,6 +286,10 @@ class Network(IntervalModule, ColorRangeModule):

settings = (
("format_up", "format string"),
("format_active_up", "Dictionary containing format strings for auto-detected interfaces. "
"Each key can be either a full interface name, or a pattern matching "
"a interface, eg 'e*' for ethernet interfaces. "
"Fallback to format_up if no pattern could be matched."),
("format_down", "format string"),
"color_up",
"color_down",
Expand Down Expand Up @@ -314,6 +320,7 @@ class Network(IntervalModule, ColorRangeModule):
interface = 'eth0'

format_up = "{interface} {network_graph}{kbs}KB/s"
format_active_up = {}
format_down = "{interface}: DOWN"
color_up = "#00FF00"
color_down = "#FF0000"
Expand Down Expand Up @@ -410,31 +417,39 @@ def run(self):
format_values['network_graph'] = self.get_network_graph(kbs, limit)
format_values['kbs'] = "{0:.1f}".format(round(kbs, 2))

if self.separate_color and self.pango_enabled:
color = self.color_up
color_template = "<span color=\"{}\">{}</span>"
per_recv = network_usage["bytes_recv"] * self.divisor / (self.recv_limit * 1024)
per_sent = network_usage["bytes_sent"] * self.divisor / (self.sent_limit * 1024)
c_recv = self.get_gradient(int(per_recv * 100), self.colors, 100)
c_sent = self.get_gradient(int(per_sent * 100), self.colors, 100)
format_values["bytes_recv"] = color_template.format(c_recv, network_usage["bytes_recv"])
format_values["bytes_sent"] = color_template.format(c_sent, network_usage["bytes_sent"])
if self.graph_type == 'output':
c_kbs = c_sent
if self.dynamic_color:
if self.separate_color and self.pango_enabled:
color = self.color_up
color_template = "<span color=\"{}\">{}</span>"
per_recv = network_usage["bytes_recv"] * self.divisor / (self.recv_limit * 1024)
per_sent = network_usage["bytes_sent"] * self.divisor / (self.sent_limit * 1024)
c_recv = self.get_gradient(int(per_recv * 100), self.colors, 100)
c_sent = self.get_gradient(int(per_sent * 100), self.colors, 100)
format_values["bytes_recv"] = color_template.format(c_recv, network_usage["bytes_recv"])
format_values["bytes_sent"] = color_template.format(c_sent, network_usage["bytes_sent"])
if self.graph_type == 'output':
c_kbs = c_sent
else:
c_kbs = c_recv
format_values['network_graph'] = color_template.format(c_kbs, format_values["network_graph"])
format_values['kbs'] = color_template.format(c_kbs, format_values["kbs"])
else:
c_kbs = c_recv
format_values['network_graph'] = color_template.format(c_kbs, format_values["network_graph"])
format_values['kbs'] = color_template.format(c_kbs, format_values["kbs"])
percent = int(kbs * 100 / limit)
color = self.get_gradient(percent, self.colors, 100)
else:
percent = int(kbs * 100 / limit)
color = self.get_gradient(percent, self.colors, 100)
color = None
else:
color = None

if sysfs_interface_up(self.interface, self.unknown_up):
if not color:
color = self.color_up
format_str = self.format_up

if self.detect_active:
for pattern in self.format_active_up:
if fnmatch(self.interface, pattern):
format_str = self.format_active_up.get(pattern, self.format_up)
else:
color = self.color_down
format_str = self.format_down
Expand Down

0 comments on commit 811ade8

Please sign in to comment.