cmds: apply patches in a single shot
[git-cola.git] / cola / interaction.py
bloba464300724b3f0a0a18c82da889d67ba7958f8f4
1 from __future__ import division, absolute_import, unicode_literals
2 import os
3 import sys
5 from . import core
6 from .i18n import N_
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'))
18 @classmethod
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)
22 if status != 0:
23 cls.command_error(title, cmd, status, out, err)
25 @classmethod
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))
31 core.print_stdout('')
32 if out:
33 core.print_stdout(out)
34 if err:
35 core.print_stderr(err)
37 @staticmethod
38 def format_command_status(cmd, status):
39 return (N_('"%(command)s" returned exit status %(status)d') %
40 dict(command=cmd, status=status))
42 @staticmethod
43 def format_out_err(out, err):
44 """Format stdout and stderr into a single string"""
45 details = out or ''
46 if err:
47 if details and not details.endswith('\n'):
48 details += '\n'
49 details += err
50 return details
52 @staticmethod
53 def information(title,
54 message=None, details=None, informative_text=None):
55 if message is None:
56 message = title
57 scope = {}
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 '')
64 sys.stdout.write("""
65 %(title)s
66 %(title_dashes)s
67 %(message)s%(informative_text)s%(details)s\n""" % scope)
69 @classmethod
70 def critical(cls, title, message=None, details=None):
71 """Show a warning with the provided title and message."""
72 cls.information(title, message=message, details=details)
74 @classmethod
75 def confirm(cls, title, text, informative_text, ok_text,
76 icon=None, default=True, cancel_text=None):
78 cancel_text = cancel_text or 'Cancel'
79 icon = icon or '?'
81 cls.information(title, message=text,
82 informative_text=informative_text)
83 if default:
84 prompt = '%s? [Y/n] ' % ok_text
85 else:
86 prompt = '%s? [y/N] ' % ok_text
87 sys.stdout.write(prompt)
88 answer = sys.stdin.readline().strip()
89 if answer:
90 result = answer.lower().startswith('y')
91 else:
92 result = default
93 return result
95 @classmethod
96 def question(cls, title, message, default=True):
97 return cls.confirm(title, message, '',
98 ok_text=N_('Continue'), default=default)
100 @classmethod
101 def run_command(cls, title, cmd):
102 cls.log('# ' + title)
103 cls.log('$ ' + core.list2cmdline(cmd))
104 status, out, err = core.run_command(cmd)
105 cls.log_status(status, out, err)
106 return status, out, err
108 @classmethod
109 def confirm_config_action(cls, _context, name, _opts):
110 return cls.confirm(
111 N_('Run %s?') % name,
112 N_('Run the "%s" command?') % name, '',
113 ok_text=N_('Run'))
115 @classmethod
116 def log_status(cls, status, out, err=None):
117 msg = (((out + '\n') if out else '') +
118 ((err + '\n') if err else ''))
119 cls.log(msg)
120 cls.log('exit status %s' % status)
122 @classmethod
123 def log(cls, message):
124 if cls.VERBOSE:
125 core.print_stdout(message)
127 @classmethod
128 def save_as(cls, filename, title):
129 if cls.confirm(title, 'Save as %s?' % filename, '', ok_text='Save'):
130 return filename
131 return None
133 @staticmethod
134 def async_command(title, command, runtask):
135 pass
137 @classmethod
138 def choose_ref(cls, _context, title, button_text, default=None, icon=None):
139 icon = icon or '?'
140 cls.information(title, button_text)
141 return sys.stdin.readline().strip() or default