inotify: guard against non-existent paths
[git-cola.git] / cola / interaction.py
blob02e46f35d498c55a78f4f8049c0fd1232d2d3a9d
1 import os
2 import subprocess
3 from cola import core
4 from cola.i18n import N_
7 class Interaction(object):
8 """Prompts the user and answers questions"""
10 VERBOSE = bool(os.getenv('GIT_COLA_VERBOSE'))
12 @staticmethod
13 def information(title,
14 message=None, details=None, informative_text=None):
15 if message is None:
16 message = title
17 scope = {}
18 scope['title'] = title
19 scope['title_dashes'] = '-' * len(title)
20 scope['message'] = message
21 scope['details'] = details and '\n'+details or ''
22 scope['informative_text'] = (informative_text and
23 '\n'+informative_text or '')
24 print("""
25 %(title)s
26 %(title_dashes)s
27 %(message)s%(details)s%(informative_text)s""" % scope)
29 @classmethod
30 def critical(cls, title, message=None, details=None):
31 """Show a warning with the provided title and message."""
32 cls.information(title, message=message, details=details)
34 @classmethod
35 def confirm(cls, title, text, informative_text, ok_text,
36 icon=None, default=True):
38 cls.information(title, message=text,
39 informative_text=informative_text)
40 if default:
41 prompt = '%s? [Y/n]:' % ok_text
42 else:
43 prompt = '%s? [y/N]: ' % ok_text
44 answer = raw_input(prompt)
45 if answer == '':
46 return default
47 return answer.lower().startswith('y')
49 @classmethod
50 def question(cls, title, message, default=True):
51 return cls.confirm(title, message, '',
52 ok_text=N_('Continue'), default=default)
54 @classmethod
55 def run_command(cls, title, cmd):
56 cls.log('$ ' + subprocess.list2cmdline(cmd))
57 status, out, err = core.run_command(cmd)
58 cls.log_status(status, out, err)
60 @classmethod
61 def confirm_config_action(cls, name, opts):
62 return cls.confirm(N_('Run %s?') % name,
63 N_('Run the "%s" command?') % name,
64 ok_text=N_('Run'))
66 @classmethod
67 def log_status(cls, status, out, err=None):
68 msg = (
69 (out and ((N_('Output: %s') % out) + '\n') or '') +
70 (err and ((N_('Errors: %s') % err) + '\n') or '') +
71 N_('Exit code: %s') % status
73 cls.log(msg)
75 @classmethod
76 def log(cls, message):
77 if cls.VERBOSE:
78 core.stdout(message)