1 from __future__
import absolute_import
, division
, print_function
, unicode_literals
9 # The dependency injection calls to install() in ColaApplication's constructor
10 # triggers method-already-defined pylint warnings. Silence that warning.
12 # pylint: disable=function-redefined
13 class Interaction(object):
14 """Prompts the user and answers questions"""
16 VERBOSE
= bool(os
.getenv('GIT_COLA_VERBOSE'))
19 def command(cls
, title
, cmd
, status
, out
, err
):
20 """Log a command and display error messages on failure"""
21 cls
.log_status(status
, out
, err
)
23 cls
.command_error(title
, cmd
, status
, out
, err
)
26 def command_error(cls
, title
, cmd
, status
, out
, err
):
27 """Display an error message for a failed command"""
28 core
.print_stderr(title
)
29 core
.print_stderr('-' * len(title
))
30 core
.print_stderr(cls
.format_command_status(cmd
, status
))
33 core
.print_stdout(out
)
35 core
.print_stderr(err
)
38 def format_command_status(cmd
, status
):
39 return N_('"%(command)s" returned exit status %(status)d') % dict(
40 command
=cmd
, status
=status
44 def format_out_err(out
, err
):
45 """Format stdout and stderr into a single string"""
48 if details
and not details
.endswith('\n'):
54 def information(title
, message
=None, details
=None, informative_text
=None):
58 scope
['title'] = title
59 scope
['title_dashes'] = '-' * len(title
)
60 scope
['message'] = message
61 scope
['details'] = ('\n' + details
) if details
else ''
62 scope
['informative_text'] = (
63 ('\n' + informative_text
) if informative_text
else ''
69 %(message)s%(informative_text)s%(details)s\n"""
75 def critical(cls
, title
, message
=None, details
=None):
76 """Show a warning with the provided title and message."""
77 cls
.information(title
, message
=message
, details
=details
)
91 cancel_text
= cancel_text
or 'Cancel'
94 cls
.information(title
, message
=text
, informative_text
=informative_text
)
96 prompt
= '%s? [Y/n] ' % ok_text
98 prompt
= '%s? [y/N] ' % ok_text
99 sys
.stdout
.write(prompt
)
101 answer
= sys
.stdin
.readline().strip()
103 result
= answer
.lower().startswith('y')
109 def question(cls
, title
, message
, default
=True):
110 return cls
.confirm(title
, message
, '', ok_text
=N_('Continue'), default
=default
)
113 def run_command(cls
, title
, cmd
):
114 cls
.log('# ' + title
)
115 cls
.log('$ ' + core
.list2cmdline(cmd
))
116 status
, out
, err
= core
.run_command(cmd
)
117 cls
.log_status(status
, out
, err
)
118 return status
, out
, err
121 def confirm_config_action(cls
, _context
, name
, _opts
):
123 N_('Run %s?') % name
,
124 N_('Run the "%s" command?') % name
,
130 def log_status(cls
, status
, out
, err
=None):
131 msg
= ((out
+ '\n') if out
else '') + ((err
+ '\n') if err
else '')
133 cls
.log('exit status %s' % status
)
136 def log(cls
, message
):
138 core
.print_stdout(message
)
141 def save_as(cls
, filename
, title
):
142 if cls
.confirm(title
, 'Save as %s?' % filename
, '', ok_text
='Save'):
147 def async_command(title
, command
, runtask
):
151 def choose_ref(cls
, _context
, title
, button_text
, default
=None, icon
=None):
153 cls
.information(title
, button_text
)
154 return sys
.stdin
.readline().strip() or default