1 from __future__
import division
, absolute_import
, unicode_literals
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'))
14 def information(title
,
15 message
=None, details
=None, informative_text
=None):
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 '')
28 %(message)s%(details)s%(informative_text)s""" % scope
)
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
)
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
)
42 prompt
= '%s? [Y/n]:' % ok_text
44 prompt
= '%s? [y/N]: ' % ok_text
45 answer
= raw_input(prompt
)
48 return answer
.lower().startswith('y')
51 def question(cls
, title
, message
, default
=True):
52 return cls
.confirm(title
, message
, '',
53 ok_text
=N_('Continue'), default
=default
)
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
63 def confirm_config_action(cls
, name
, opts
):
64 return cls
.confirm(N_('Run %s?') % name
,
65 N_('Run the "%s" command?') % name
,
70 def log_status(cls
, status
, out
, err
=None):
72 (out
and ((N_('Output: %s') % out
) + '\n') or '') +
73 (err
and ((N_('Errors: %s') % err
) + '\n') or '') +
74 N_('Exit code: %s') % status
79 def log(cls
, message
):