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 command(cls
, title
, cmd
, status
, out
, err
):
17 """Log a command and display error messages on failure"""
18 cls
.log_status(status
, out
, err
)
20 cls
.command_error(title
, cmd
, status
, out
, err
)
23 def command_error(cls
, title
, cmd
, status
, out
, err
):
24 """Display an error message for a failed command"""
25 core
.print_stderr(title
)
26 core
.print_stderr('-' * len(title
))
27 core
.print_stderr(cls
.format_command_status(cmd
, status
))
30 core
.print_stdout(out
)
32 core
.print_stderr(err
)
35 def format_command_status(cmd
, status
):
36 return (N_('"%(command)s" returned exit status %(status)d') %
37 dict(command
=cmd
, status
=status
))
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
,
51 message
=None, details
=None, informative_text
=None):
55 scope
['title'] = title
56 scope
['title_dashes'] = '-' * len(title
)
57 scope
['message'] = message
58 scope
['details'] = ('\n' + details
) if details
else ''
59 scope
['informative_text'] = (
60 ('\n' + informative_text
) if informative_text
else '')
64 %(message)s%(informative_text)s%(details)s\n""" % scope
)
67 def critical(cls
, title
, message
=None, details
=None):
68 """Show a warning with the provided title and message."""
69 cls
.information(title
, message
=message
, details
=details
)
72 def confirm(cls
, title
, text
, informative_text
, ok_text
,
73 icon
=None, default
=True, cancel_text
=None):
75 cancel_text
= cancel_text
or 'Cancel'
78 cls
.information(title
, message
=text
,
79 informative_text
=informative_text
)
81 prompt
= '%s? [Y/n] ' % ok_text
83 prompt
= '%s? [y/N] ' % ok_text
84 sys
.stdout
.write(prompt
)
85 answer
= sys
.stdin
.readline().strip()
87 result
= answer
.lower().startswith('y')
93 def question(cls
, title
, message
, default
=True):
94 return cls
.confirm(title
, message
, '',
95 ok_text
=N_('Continue'), default
=default
)
98 def run_command(cls
, title
, cmd
):
100 cls
.log('$ ' + core
.list2cmdline(cmd
))
101 status
, out
, err
= core
.run_command(cmd
)
102 cls
.log_status(status
, out
, err
)
103 return status
, out
, err
106 def confirm_config_action(cls
, _context
, name
, _opts
):
108 N_('Run %s?') % name
,
109 N_('Run the "%s" command?') % name
, '',
113 def log_status(cls
, status
, out
, err
=None):
114 msg
= (((out
+ '\n') if out
else '') +
115 ((err
+ '\n') if err
else ''))
117 cls
.log('exit status %s' % status
)
120 def log(cls
, message
):
122 core
.print_stdout(message
)
125 def save_as(cls
, filename
, title
):
126 if cls
.confirm(title
, 'Save as %s?' % filename
, '', ok_text
='Save'):
131 def async_command(title
, command
, runtask
):
135 def choose_ref(cls
, _context
, title
, button_text
, default
=None, icon
=None):
137 cls
.information(title
, button_text
)
138 return sys
.stdin
.readline().strip() or default