core: add list2cmdline() wrapper
[git-cola.git] / cola / interaction.py
blobe380aa357f992042e5a9f4ace42a2160226982e4
1 from __future__ import division, absolute_import, unicode_literals
3 import os
4 from cola import core
5 from cola.i18n import N_
8 class Interaction(object):
9 """Prompts the user and answers questions"""
11 VERBOSE = bool(os.getenv('GIT_COLA_VERBOSE'))
13 @staticmethod
14 def information(title,
15 message=None, details=None, informative_text=None):
16 if message is None:
17 message = title
18 scope = {}
19 scope['title'] = title
20 scope['title_dashes'] = '-' * len(title)
21 scope['message'] = message
22 scope['details'] = details and '\n'+details or ''
23 scope['informative_text'] = (informative_text and
24 '\n'+informative_text or '')
25 print("""
26 %(title)s
27 %(title_dashes)s
28 %(message)s%(details)s%(informative_text)s""" % scope)
30 @classmethod
31 def critical(cls, title, message=None, details=None):
32 """Show a warning with the provided title and message."""
33 cls.information(title, message=message, details=details)
35 @classmethod
36 def confirm(cls, title, text, informative_text, ok_text,
37 icon=None, default=True):
39 cls.information(title, message=text,
40 informative_text=informative_text)
41 if default:
42 prompt = '%s? [Y/n]:' % ok_text
43 else:
44 prompt = '%s? [y/N]: ' % ok_text
45 answer = raw_input(prompt)
46 if answer == '':
47 return default
48 return answer.lower().startswith('y')
50 @classmethod
51 def question(cls, title, message, default=True):
52 return cls.confirm(title, message, '',
53 ok_text=N_('Continue'), default=default)
55 @classmethod
56 def run_command(cls, title, cmd):
57 cls.log('$ ' + core.list2cmdline(cmd))
58 status, out, err = core.run_command(cmd)
59 cls.log_status(status, out, err)
60 return status, out, err
62 @classmethod
63 def confirm_config_action(cls, name, opts):
64 return cls.confirm(N_('Run %s?') % name,
65 N_('Run the "%s" command?') % name,
66 '',
67 ok_text=N_('Run'))
69 @classmethod
70 def log_status(cls, status, out, err=None):
71 msg = (
72 (out and ((N_('Output: %s') % out) + '\n') or '') +
73 (err and ((N_('Errors: %s') % err) + '\n') or '') +
74 N_('Exit code: %s') % status
76 cls.log(msg)
78 @classmethod
79 def log(cls, message):
80 if cls.VERBOSE:
81 core.stdout(message)
83 safe_log = log