Skip to content

Commit

Permalink
handle i3status 'short_text'.
Browse files Browse the repository at this point in the history
simple way to transform with settings a 'max_size' globally for all module or per module.
short_text is used if returned text size is larger than monitor.
  • Loading branch information
cyrinux committed Sep 3, 2018
1 parent b74a6de commit 414c1da
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions py3status/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'color_separator': '#333333',
'colors': False,
'interval': 5,
'max_size': False,
'output_format': 'i3bar'
}

Expand Down
29 changes: 27 additions & 2 deletions py3status/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,7 @@ def create_output_modules(self):
output_modules[name]['module'] = self.modules[name]
output_modules[name]['type'] = 'py3status'
output_modules[name]['color'] = self.mappings_color.get(name)
output_modules[name]['max_size'] = self.mappings_max_size.get(name)
# i3status modules
for name in i3modules:
if name not in output_modules:
Expand All @@ -897,6 +898,7 @@ def create_output_modules(self):
output_modules[name]['module'] = i3modules[name]
output_modules[name]['type'] = 'i3status'
output_modules[name]['color'] = self.mappings_color.get(name)
output_modules[name]['max_size'] = self.mappings_max_size.get(name)

self.output_modules = output_modules

Expand All @@ -913,21 +915,44 @@ def create_mappings(self, config):
if hasattr(color, 'none_setting'):
color = None
mappings[name] = color

max_size = self.get_config_attribute(name, 'max_size')
if hasattr(max_size, 'none_setting'):
max_size = None
mappings[name] = max_size

# Store mappings for later use.
self.mappings_max_size = mappings
self.mappings_color = mappings

def process_module_output(self, module):
"""
Process the output for a module and return a json string representing it.
Color processing occurs here.
Shortened text 'short_text' processing occurs here.
"""
outputs = module['module'].get_latest()
color = module['color']
if color:
for output in outputs:
max_size = module['max_size']

for output in outputs:
if color:
# Color: substitute the config defined color
if 'color' not in output:
output['color'] = color

if 'short_text' not in output:
full_text = output['full_text']
if int(max_size) >= 1:
short = full_text[:max_size] if len(full_text) > int(max_size) else full_text
short = short + '..' if len(short + '..') < len(full_text) else short
output['short_text'] = short
elif max_size is True:
size = int((len(full_text) + 1) / 2)
short = full_text[:size]
short = short + '..' if len(short + '..') < len(full_text) else short
output['short_text'] = short

# Create the json string output.
return ','.join([dumps(x) for x in outputs])

Expand Down
9 changes: 9 additions & 0 deletions py3status/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,15 @@ def deprecation_log(item):
param = True
self.allow_urgent = param

# max_size
# get the value form the config or use the module default if
# supplied.
fn = self._py3_wrapper.get_config_attribute
param = fn(self.module_full_name, 'max_size')
if hasattr(param, 'none_setting'):
param = True
self.max_size = param

# get the available methods for execution
for method in sorted(dir(class_inst)):
if method.startswith('_'):
Expand Down

0 comments on commit 414c1da

Please sign in to comment.