1 from __future__
import division
, absolute_import
, unicode_literals
10 class Interaction(object):
11 """Prompts the user and answers questions"""
13 VERBOSE
= bool(os
.getenv('GIT_COLA_VERBOSE'))
16 def information(title
,
17 message
=None, details
=None, informative_text
=None):
21 scope
['title'] = title
22 scope
['title_dashes'] = '-' * len(title
)
23 scope
['message'] = message
24 scope
['details'] = details
and '\n'+details
or ''
25 scope
['informative_text'] = (
26 informative_text
and ('\n' + informative_text
) or '')
30 %(message)s%(details)s%(informative_text)s\n""" % scope
)
33 def critical(cls
, title
, message
=None, details
=None):
34 """Show a warning with the provided title and message."""
35 cls
.information(title
, message
=message
, details
=details
)
38 def confirm(cls
, title
, text
, informative_text
, ok_text
,
39 icon
=None, default
=True):
41 cls
.information(title
, message
=text
,
42 informative_text
=informative_text
)
44 prompt
= '%s? [Y/n] ' % ok_text
46 prompt
= '%s? [y/N] ' % ok_text
47 sys
.stdout
.write(prompt
)
48 answer
= sys
.stdin
.readline().strip()
50 result
= answer
.lower().startswith('y')
56 def question(cls
, title
, message
, default
=True):
57 return cls
.confirm(title
, message
, '',
58 ok_text
=N_('Continue'), default
=default
)
61 def run_command(cls
, title
, cmd
):
62 cls
.log('$ ' + core
.list2cmdline(cmd
))
63 status
, out
, err
= core
.run_command(cmd
)
64 cls
.log_status(status
, out
, err
)
65 return status
, out
, err
68 def confirm_config_action(cls
, name
, opts
):
69 return cls
.confirm(N_('Run %s?') % name
,
70 N_('Run the "%s" command?') % name
,
75 def log_status(cls
, status
, out
, err
=None):
77 (out
and ((N_('Output: %s') % out
) + '\n') or '') +
78 (err
and ((N_('Errors: %s') % err
) + '\n') or '') +
79 N_('Exit code: %s') % status
84 def log(cls
, message
):