stash: pylint updates
[git-cola.git] / cola / interaction.py
blob60f920748989db3a82ca87871ebf0d327ccb2c84
1 from __future__ import division, absolute_import, unicode_literals
3 import os
4 import sys
6 from . import core
7 from .i18n import N_
10 class Interaction(object):
11 """Prompts the user and answers questions"""
13 VERBOSE = bool(os.getenv('GIT_COLA_VERBOSE'))
15 @classmethod
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)
19 if status != 0:
20 cls.command_error(title, cmd, status, out, err)
22 @classmethod
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))
28 core.print_stdout('')
29 if out:
30 core.print_stdout(out)
31 if err:
32 core.print_stderr(err)
34 @staticmethod
35 def format_command_status(cmd, status):
36 return (N_('"%(command)s" returned exit status %(status)d') %
37 dict(command=cmd, status=status))
39 @staticmethod
40 def format_out_err(out, err):
41 """Format stdout and stderr into a single string"""
42 details = out or ''
43 if err:
44 if details and not details.endswith('\n'):
45 details += '\n'
46 details += err
47 return details
49 @staticmethod
50 def information(title,
51 message=None, details=None, informative_text=None):
52 if message is None:
53 message = title
54 scope = {}
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 '')
61 sys.stdout.write("""
62 %(title)s
63 %(title_dashes)s
64 %(message)s%(informative_text)s%(details)s\n""" % scope)
66 @classmethod
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)
71 @classmethod
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'
76 icon = icon or '?'
78 cls.information(title, message=text,
79 informative_text=informative_text)
80 if default:
81 prompt = '%s? [Y/n] ' % ok_text
82 else:
83 prompt = '%s? [y/N] ' % ok_text
84 sys.stdout.write(prompt)
85 answer = sys.stdin.readline().strip()
86 if answer:
87 result = answer.lower().startswith('y')
88 else:
89 result = default
90 return result
92 @classmethod
93 def question(cls, title, message, default=True):
94 return cls.confirm(title, message, '',
95 ok_text=N_('Continue'), default=default)
97 @classmethod
98 def run_command(cls, title, cmd):
99 cls.log('# ' + title)
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
105 @classmethod
106 def confirm_config_action(cls, _context, name, _opts):
107 return cls.confirm(
108 N_('Run %s?') % name,
109 N_('Run the "%s" command?') % name, '',
110 ok_text=N_('Run'))
112 @classmethod
113 def log_status(cls, status, out, err=None):
114 msg = (((out + '\n') if out else '') +
115 ((err + '\n') if err else ''))
116 cls.log(msg)
117 cls.log('exit status %s' % status)
119 @classmethod
120 def log(cls, message):
121 if cls.VERBOSE:
122 core.print_stdout(message)
124 @classmethod
125 def save_as(cls, filename, title):
126 if cls.confirm(title, 'Save as %s?' % filename, '', ok_text='Save'):
127 return filename
128 return None
130 @staticmethod
131 def async_command(title, command, runtask):
132 pass
134 @classmethod
135 def choose_ref(cls, _context, title, button_text, default=None, icon=None):
136 icon = icon or '?'
137 cls.information(title, button_text)
138 return sys.stdin.readline().strip() or default