dag: update column allocation algorithm description
[git-cola.git] / cola / interaction.py
blob0caa84cdd45efa6582a42c003a290c4dd3dfa211
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 @staticmethod
16 def information(title,
17 message=None, details=None, informative_text=None):
18 if message is None:
19 message = title
20 scope = {}
21 scope['title'] = title
22 scope['title_dashes'] = '-' * len(title)
23 scope['message'] = message
24 scope['details'] = details and '\n'+details or ''
25 scope['informative_text'] = (
26 informative_text and ('\n' + informative_text) or '')
27 sys.stdout.write("""
28 %(title)s
29 %(title_dashes)s
30 %(message)s%(details)s%(informative_text)s\n""" % scope)
32 @classmethod
33 def critical(cls, title, message=None, details=None):
34 """Show a warning with the provided title and message."""
35 cls.information(title, message=message, details=details)
37 @classmethod
38 def confirm(cls, title, text, informative_text, ok_text,
39 icon=None, default=True):
41 cls.information(title, message=text,
42 informative_text=informative_text)
43 if default:
44 prompt = '%s? [Y/n] ' % ok_text
45 else:
46 prompt = '%s? [y/N] ' % ok_text
47 sys.stdout.write(prompt)
48 answer = sys.stdin.readline().strip()
49 if answer:
50 result = answer.lower().startswith('y')
51 else:
52 result = default
53 return result
55 @classmethod
56 def question(cls, title, message, default=True):
57 return cls.confirm(title, message, '',
58 ok_text=N_('Continue'), default=default)
60 @classmethod
61 def run_command(cls, title, cmd):
62 cls.log('$ ' + core.list2cmdline(cmd))
63 status, out, err = core.run_command(cmd)
64 cls.log_status(status, out, err)
65 return status, out, err
67 @classmethod
68 def confirm_config_action(cls, name, opts):
69 return cls.confirm(N_('Run %s?') % name,
70 N_('Run the "%s" command?') % name,
71 '',
72 ok_text=N_('Run'))
74 @classmethod
75 def log_status(cls, status, out, err=None):
76 msg = (
77 (out and ((N_('Output: %s') % out) + '\n') or '') +
78 (err and ((N_('Errors: %s') % err) + '\n') or '') +
79 N_('Exit code: %s') % status
81 cls.log(msg)
83 @classmethod
84 def log(cls, message):
85 if cls.VERBOSE:
86 core.stdout(message)
88 safe_log = log