8 # The dependency injection calls to install() in ColaApplication's constructor
9 # triggers method-already-defined pylint warnings. Silence that warning.
11 # pylint: disable=function-redefined
13 """Prompts the user and answers questions"""
15 VERBOSE
= bool(os
.getenv('GIT_COLA_VERBOSE'))
18 def command(cls
, title
, cmd
, status
, out
, err
):
19 """Log a command and display error messages on failure"""
20 cls
.log_status(status
, out
, err
)
22 cls
.command_error(title
, cmd
, status
, out
, err
)
25 def command_error(cls
, title
, cmd
, status
, out
, err
):
26 """Display an error message for a failed command"""
27 core
.print_stderr(title
)
28 core
.print_stderr('-' * len(title
))
29 core
.print_stderr(cls
.format_command_status(cmd
, status
))
32 core
.print_stdout(out
)
34 core
.print_stderr(err
)
37 def format_command_status(cmd
, status
):
38 return N_('"%(command)s" returned exit status %(status)d') % {
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
)
90 cancel_text
= cancel_text
or 'Cancel'
93 cls
.information(title
, message
=text
, informative_text
=informative_text
)
95 prompt
= '%s? [Y/n] ' % ok_text
97 prompt
= '%s? [y/N] ' % ok_text
98 sys
.stdout
.write(prompt
)
100 answer
= sys
.stdin
.readline().strip()
102 result
= answer
.lower().startswith('y')
108 def question(cls
, title
, message
, default
=True):
109 return cls
.confirm(title
, message
, '', ok_text
=N_('Continue'), default
=default
)
112 def run_command(cls
, title
, cmd
):
113 cls
.log('# ' + title
)
114 cls
.log('$ ' + core
.list2cmdline(cmd
))
115 status
, out
, err
= core
.run_command(cmd
)
116 cls
.log_status(status
, out
, err
)
117 return status
, out
, err
120 def confirm_config_action(cls
, _context
, name
, _opts
):
122 N_('Run %s?') % name
,
123 N_('Run the "%s" command?') % name
,
129 def log_status(cls
, status
, out
, err
=None):
130 msg
= ((out
+ '\n') if out
else '') + ((err
+ '\n') if err
else '')
132 cls
.log('exit status %s' % status
)
135 def log(cls
, message
):
137 core
.print_stdout(message
)
140 def save_as(cls
, filename
, title
):
141 if cls
.confirm(title
, 'Save as %s?' % filename
, '', ok_text
='Save'):
146 def async_command(title
, command
, runtask
):
150 def choose_ref(cls
, _context
, title
, button_text
, default
=None, icon
=None):
152 cls
.information(title
, button_text
)
153 return sys
.stdin
.readline().strip() or default