From ab0a218f8e08c6b650fa4d292762159512348535 Mon Sep 17 00:00:00 2001 From: Robert Maikher Date: Fri, 24 Aug 2018 09:21:01 +0200 Subject: [PATCH] Use function to return header instead of stringformatting a variable --- helpers.py | 7 +------ text_interface/helpermenus.py | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/helpers.py b/helpers.py index 0783fcc..9ef0bd8 100644 --- a/helpers.py +++ b/helpers.py @@ -120,12 +120,7 @@ def safe_str(obj): Call this on any object to turn it into a string which is (hopefully) safe for printing. ''' - if isinstance(obj, str): - return obj - if isinstance(obj, str): - return obj.encode('utf8') - else: - return safe_str(str(obj)) + return obj def less(string): ''' diff --git a/text_interface/helpermenus.py b/text_interface/helpermenus.py index 04aaccd..3e0a7d1 100644 --- a/text_interface/helpermenus.py +++ b/text_interface/helpermenus.py @@ -32,7 +32,6 @@ class Menu(object): self.exit_disallowed_msg = exit_disallowed_msg self.help_text = help_text self.context = None - self.header_format = '[%s]' self.uses_db = uses_db self.session = None @@ -65,7 +64,7 @@ class Menu(object): self.context += '\n' + string def show_context(self): - print(self.header_format % self.name) + print(self.header()) if self.context is not None: print(self.context) @@ -210,8 +209,6 @@ class Menu(object): def input_int(self, prompt=None, allowed_range=(None, None), null_allowed=False, default=None): if prompt is None: prompt = self.prompt - if default is not None: - prompt += (f"[{default}] ") while True: result = self.input_str(prompt) if result == '': @@ -361,7 +358,7 @@ class Menu(object): print(f'No {thing}s matching "{search_str}"') return None if len(result) == 1: - msg = f'One {thing} matching "{search_str}": {str(result[0])}. Use this?' + msg = f'One {thing} matching "{search_str}": {str(result[0])}. Use this?' if self.confirm(msg, default=True): return result[0] return None @@ -380,9 +377,12 @@ class Menu(object): def confirm(prompt, default=None, timeout=None): return ConfirmMenu(prompt, default, timeout).execute() + def header(self): + return f"[{self.name}]" + def print_header(self): print("") - print(self.header_format % self.name) + print(self.header()) def pause(self): self.input_str('.') @@ -417,8 +417,7 @@ class Menu(object): print('no help here') else: print('') - print('Help for %s:' % (self.header_format - % self.name)) + print(f'Help for {self.header()}:') print(self.help_text) def execute(self, **kwargs): @@ -478,7 +477,7 @@ class ConfirmMenu(Menu): def _execute(self): options = {True: '[y]/n', False: 'y/[n]', None: 'y/n'}[self.default] while True: - result = self.input_str(f'{self.prompt} ({options}) ', timeout=self.timeout) + result = self.input_str(f'{self.prompt} ({options}): ', timeout=self.timeout) result = result.lower().strip() if result in ['y', 'yes']: return True @@ -496,10 +495,12 @@ class Selector(Menu): if items is None: items = [] Menu.__init__(self, name, items, prompt, return_index, exit_msg) - self.header_format = '%s' + + def header(self): + return self.name def print_header(self): - print(self.header_format % self.name) + print(self.header()) def local_help(self): if self.help_text is None: