Use function to return header instead of stringformatting a variable

This commit is contained in:
Robert Maikher 2018-08-24 09:21:01 +02:00
parent 24201e8a69
commit ab0a218f8e
2 changed files with 13 additions and 17 deletions

View File

@ -120,12 +120,7 @@ def safe_str(obj):
Call this on any object to turn it into a string which is Call this on any object to turn it into a string which is
(hopefully) safe for printing. (hopefully) safe for printing.
''' '''
if isinstance(obj, str):
return obj return obj
if isinstance(obj, str):
return obj.encode('utf8')
else:
return safe_str(str(obj))
def less(string): def less(string):
''' '''

View File

@ -32,7 +32,6 @@ class Menu(object):
self.exit_disallowed_msg = exit_disallowed_msg self.exit_disallowed_msg = exit_disallowed_msg
self.help_text = help_text self.help_text = help_text
self.context = None self.context = None
self.header_format = '[%s]'
self.uses_db = uses_db self.uses_db = uses_db
self.session = None self.session = None
@ -65,7 +64,7 @@ class Menu(object):
self.context += '\n' + string self.context += '\n' + string
def show_context(self): def show_context(self):
print(self.header_format % self.name) print(self.header())
if self.context is not None: if self.context is not None:
print(self.context) 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): def input_int(self, prompt=None, allowed_range=(None, None), null_allowed=False, default=None):
if prompt is None: if prompt is None:
prompt = self.prompt prompt = self.prompt
if default is not None:
prompt += (f"[{default}] ")
while True: while True:
result = self.input_str(prompt) result = self.input_str(prompt)
if result == '': if result == '':
@ -380,9 +377,12 @@ class Menu(object):
def confirm(prompt, default=None, timeout=None): def confirm(prompt, default=None, timeout=None):
return ConfirmMenu(prompt, default, timeout).execute() return ConfirmMenu(prompt, default, timeout).execute()
def header(self):
return f"[{self.name}]"
def print_header(self): def print_header(self):
print("") print("")
print(self.header_format % self.name) print(self.header())
def pause(self): def pause(self):
self.input_str('.') self.input_str('.')
@ -417,8 +417,7 @@ class Menu(object):
print('no help here') print('no help here')
else: else:
print('') print('')
print('Help for %s:' % (self.header_format print(f'Help for {self.header()}:')
% self.name))
print(self.help_text) print(self.help_text)
def execute(self, **kwargs): def execute(self, **kwargs):
@ -478,7 +477,7 @@ class ConfirmMenu(Menu):
def _execute(self): def _execute(self):
options = {True: '[y]/n', False: 'y/[n]', None: 'y/n'}[self.default] options = {True: '[y]/n', False: 'y/[n]', None: 'y/n'}[self.default]
while True: 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() result = result.lower().strip()
if result in ['y', 'yes']: if result in ['y', 'yes']:
return True return True
@ -496,10 +495,12 @@ class Selector(Menu):
if items is None: if items is None:
items = [] items = []
Menu.__init__(self, name, items, prompt, return_index, exit_msg) Menu.__init__(self, name, items, prompt, return_index, exit_msg)
self.header_format = '%s'
def header(self):
return self.name
def print_header(self): def print_header(self):
print(self.header_format % self.name) print(self.header())
def local_help(self): def local_help(self):
if self.help_text is None: if self.help_text is None: