cmds: simplify ToggleDiffType
[git-cola.git] / cola / interaction.py
blob993994d8e036ae96010311553569700370490637
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') % dict(
40 command=cmd, status=status
43 @staticmethod
44 def format_out_err(out, err):
45 """Format stdout and stderr into a single string"""
46 details = out or ''
47 if err:
48 if details and not details.endswith('\n'):
49 details += '\n'
50 details += err
51 return details
53 @staticmethod
54 def information(title, 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 ''
65 sys.stdout.write(
66 """
67 %(title)s
68 %(title_dashes)s
69 %(message)s%(informative_text)s%(details)s\n"""
70 % scope
73 @classmethod
74 def critical(cls, title, message=None, details=None):
75 """Show a warning with the provided title and message."""
76 cls.information(title, message=message, details=details)
78 @classmethod
79 def confirm(
80 cls,
81 title,
82 text,
83 informative_text,
84 ok_text,
85 icon=None,
86 default=True,
87 cancel_text=None,
90 cancel_text = cancel_text or 'Cancel'
91 icon = icon or '?'
93 cls.information(title, message=text, informative_text=informative_text)
94 if default:
95 prompt = '%s? [Y/n] ' % ok_text
96 else:
97 prompt = '%s? [y/N] ' % ok_text
98 sys.stdout.write(prompt)
99 answer = sys.stdin.readline().strip()
100 if answer:
101 result = answer.lower().startswith('y')
102 else:
103 result = default
104 return result
106 @classmethod
107 def question(cls, title, message, default=True):
108 return cls.confirm(title, message, '', ok_text=N_('Continue'), default=default)
110 @classmethod
111 def run_command(cls, title, cmd):
112 cls.log('# ' + title)
113 cls.log('$ ' + core.list2cmdline(cmd))
114 status, out, err = core.run_command(cmd)
115 cls.log_status(status, out, err)
116 return status, out, err
118 @classmethod
119 def confirm_config_action(cls, _context, name, _opts):
120 return cls.confirm(
121 N_('Run %s?') % name,
122 N_('Run the "%s" command?') % name,
124 ok_text=N_('Run'),
127 @classmethod
128 def log_status(cls, status, out, err=None):
129 msg = ((out + '\n') if out else '') + ((err + '\n') if err else '')
130 cls.log(msg)
131 cls.log('exit status %s' % status)
133 @classmethod
134 def log(cls, message):
135 if cls.VERBOSE:
136 core.print_stdout(message)
138 @classmethod
139 def save_as(cls, filename, title):
140 if cls.confirm(title, 'Save as %s?' % filename, '', ok_text='Save'):
141 return filename
142 return None
144 @staticmethod
145 def async_command(title, command, runtask):
146 pass
148 @classmethod
149 def choose_ref(cls, _context, title, button_text, default=None, icon=None):
150 icon = icon or '?'
151 cls.information(title, button_text)
152 return sys.stdin.readline().strip() or default