Custom vertical bar glyphs (#738)

* refactor make_vertical_bar

refactor to use make_glyph under the hood (de-duping code)

* Allow pulseaudio module to accept custom vertical bar arrays

also added config example
This commit is contained in:
chestm007 2019-07-17 05:22:54 +10:00 committed by GitHub
parent 2016addf81
commit af6d98cb8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 15 deletions

View File

@ -500,24 +500,19 @@ def make_graph(values, lower_limit=0.0, upper_limit=100.0, style="blocks"):
return graph
def make_vertical_bar(percentage, width=1):
def make_vertical_bar(percentage, width=1, glyphs=None):
"""
Draws a vertical bar made of unicode characters.
:param value: A value between 0 and 100
:param percentage: A value between 0 and 100
:param width: How many characters wide the bar should be.
:returns: Bar as a String
"""
bar = ' _▁▂▃▄▅▆▇█'
percentage //= 10
percentage = int(percentage)
if percentage < 0:
output = bar[0]
elif percentage >= len(bar):
output = bar[-1]
if glyphs is not None:
bar = make_glyph(percentage, lower_bound=0, upper_bound=100, glyphs=glyphs)
else:
output = bar[percentage]
return output * width
bar = make_glyph(percentage, lower_bound=0, upper_bound=100)
return bar * width
def make_bar(percentage):
@ -532,13 +527,13 @@ def make_bar(percentage):
tens = int(percentage / 10)
ones = int(percentage) - tens * 10
result = tens * ''
if(ones >= 1):
if ones >= 1:
result = result + bars[ones]
result = result + (10 - len(result)) * ' '
return result
def make_glyph(number, glyphs="▁▂▃▄▅▆▇█", lower_bound=0, upper_bound=100, enable_boundary_glyphs=False):
def make_glyph(number, glyphs=" _▁▂▃▄▅▆▇█", lower_bound=0, upper_bound=100, enable_boundary_glyphs=False):
"""
Returns a single glyph from the list of glyphs provided relative to where
the number is in the range (by default a percentage value is expected).

View File

@ -15,6 +15,22 @@ class PulseAudio(Module, ColorRangeModule):
- Requires amixer for toggling mute and incrementing/decrementing volume on scroll.
- Depends on the PyPI colour module - https://pypi.python.org/pypi/colour/0.0.5
.. rubric:: Example configuration
The example configuration below uses only unicode to display the volume (tested with otf-font-awesome)
.. code-block:: python
status.register(
"pulseaudio",
color_unmuted='#aa3300,
color_muted='#aa0500',
format_muted='\uf6a9',
format='{volume_bar}',
vertical_bar_width=1,
vertical_bar_glyphs=['\uf026 ', '\uf027 ', '\uf028']
)
.. rubric:: Available formatters
* `{volume}` volume in percent (0...100)
@ -37,7 +53,8 @@ class PulseAudio(Module, ColorRangeModule):
("bar_type", "type of volume bar. Allowed values are 'vertical' or 'horizontal'"),
("multi_colors", "whether or not to change the color from "
"'color_muted' to 'color_unmuted' based on volume percentage"),
("vertical_bar_width", "how many characters wide the vertical volume_bar should be")
("vertical_bar_width", "how many characters wide the vertical volume_bar should be"),
('vertical_bar_glyphs', 'custom array output as vertical bar instead of unicode bars')
)
muted = "M"
@ -49,6 +66,7 @@ class PulseAudio(Module, ColorRangeModule):
has_amixer = False
color_muted = "#FF0000"
color_unmuted = "#FFFFFF"
vertical_bar_glyphs = None
sink = None
move_sink_inputs = True
@ -173,7 +191,7 @@ class PulseAudio(Module, ColorRangeModule):
output_format = self.format
if self.bar_type == 'vertical':
volume_bar = make_vertical_bar(volume_percent, self.vertical_bar_width)
volume_bar = make_vertical_bar(volume_percent, self.vertical_bar_width, glyphs=self.vertical_bar_glyphs)
elif self.bar_type == 'horizontal':
volume_bar = make_bar(volume_percent)
else: