9 """Prompts the user and answers questions"""
11 VERBOSE
= bool(os
.getenv('GIT_COLA_VERBOSE'))
14 def command(cls
, title
, cmd
, status
, out
, err
):
15 """Log a command and display error messages on failure"""
16 cls
.log_status(status
, out
, err
)
18 cls
.command_error(title
, cmd
, status
, out
, err
)
21 def command_error(cls
, title
, cmd
, status
, out
, err
):
22 """Display an error message for a failed command"""
23 core
.print_stderr(title
)
24 core
.print_stderr('-' * len(title
))
25 core
.print_stderr(cls
.format_command_status(cmd
, status
))
28 core
.print_stdout(out
)
30 core
.print_stderr(err
)
33 def format_command_status(cmd
, status
):
34 return N_('"%(command)s" returned exit status %(status)d') % {
40 def format_out_err(out
, err
):
41 """Format stdout and stderr into a single string"""
44 if details
and not details
.endswith('\n'):
50 def information(title
, message
=None, details
=None, informative_text
=None):
54 scope
['title'] = title
55 scope
['title_dashes'] = '-' * len(title
)
56 scope
['message'] = message
57 scope
['details'] = ('\n' + details
) if details
else ''
58 scope
['informative_text'] = (
59 ('\n' + informative_text
) if informative_text
else ''
65 %(message)s%(informative_text)s%(details)s\n"""
71 def critical(cls
, title
, message
=None, details
=None):
72 """Show a warning with the provided title and message."""
73 cls
.information(title
, message
=message
, details
=details
)
86 cancel_text
= cancel_text
or 'Cancel'
89 cls
.information(title
, message
=text
, informative_text
=informative_text
)
91 prompt
= '%s? [Y/n] ' % ok_text
93 prompt
= '%s? [y/N] ' % ok_text
94 sys
.stdout
.write(prompt
)
96 answer
= sys
.stdin
.readline().strip()
98 result
= answer
.lower().startswith('y')
104 def question(cls
, title
, message
, default
=True):
105 return cls
.confirm(title
, message
, '', ok_text
=N_('Continue'), default
=default
)
108 def run_command(cls
, title
, cmd
):
109 cls
.log('# ' + title
)
110 cls
.log('$ ' + core
.list2cmdline(cmd
))
111 status
, out
, err
= core
.run_command(cmd
)
112 cls
.log_status(status
, out
, err
)
113 return status
, out
, err
116 def confirm_config_action(cls
, _context
, name
, _opts
):
118 N_('Run %s?') % name
,
119 N_('Run the "%s" command?') % name
,
125 def log_status(cls
, status
, out
, err
=None):
126 msg
= ((out
+ '\n') if out
else '') + ((err
+ '\n') if err
else '')
128 cls
.log('exit status %s' % status
)
131 def log(cls
, message
):
133 core
.print_stdout(message
)
136 def save_as(cls
, filename
, title
):
137 if cls
.confirm(title
, 'Save as %s?' % filename
, '', ok_text
='Save'):
142 def async_command(title
, command
, runtask
):
146 def choose_ref(cls
, _context
, title
, button_text
, default
=None, icon
=None):
148 cls
.information(title
, button_text
)
149 return sys
.stdin
.readline().strip() or default