File: interaction.py

package info (click to toggle)
git-cola 4.13.0-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 6,480 kB
  • sloc: python: 36,938; sh: 304; makefile: 223; xml: 100; tcl: 62
file content (149 lines) | stat: -rw-r--r-- 4,367 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os
import sys

from . import core
from .i18n import N_


class Interaction:
    """Prompts the user and answers questions"""

    VERBOSE = bool(os.getenv('GIT_COLA_VERBOSE'))

    @classmethod
    def command(cls, title, cmd, status, out, err):
        """Log a command and display error messages on failure"""
        cls.log_status(status, out, err)
        if status != 0:
            cls.command_error(title, cmd, status, out, err)

    @classmethod
    def command_error(cls, title, cmd, status, out, err):
        """Display an error message for a failed command"""
        core.print_stderr(title)
        core.print_stderr('-' * len(title))
        core.print_stderr(cls.format_command_status(cmd, status))
        core.print_stdout('')
        if out:
            core.print_stdout(out)
        if err:
            core.print_stderr(err)

    @staticmethod
    def format_command_status(cmd, status):
        return N_('"%(command)s" returned exit status %(status)d') % {
            'command': cmd,
            'status': status,
        }

    @staticmethod
    def format_out_err(out, err):
        """Format stdout and stderr into a single string"""
        details = out or ''
        if err:
            if details and not details.endswith('\n'):
                details += '\n'
            details += err
        return details

    @staticmethod
    def information(title, message=None, details=None, informative_text=None):
        if message is None:
            message = title
        scope = {}
        scope['title'] = title
        scope['title_dashes'] = '-' * len(title)
        scope['message'] = message
        scope['details'] = ('\n' + details) if details else ''
        scope['informative_text'] = (
            ('\n' + informative_text) if informative_text else ''
        )
        sys.stdout.write(
            """
%(title)s
%(title_dashes)s
%(message)s%(informative_text)s%(details)s\n"""
            % scope
        )
        sys.stdout.flush()

    @classmethod
    def critical(cls, title, message=None, details=None):
        """Show a warning with the provided title and message."""
        cls.information(title, message=message, details=details)

    @classmethod
    def confirm(
        cls,
        title,
        text,
        informative_text,
        ok_text,
        icon=None,
        default=True,
        cancel_text=None,
    ):
        cancel_text = cancel_text or 'Cancel'
        icon = icon or '?'

        cls.information(title, message=text, informative_text=informative_text)
        if default:
            prompt = '%s? [Y/n] ' % ok_text
        else:
            prompt = '%s? [y/N] ' % ok_text
        sys.stdout.write(prompt)
        sys.stdout.flush()
        answer = sys.stdin.readline().strip()
        if answer:
            result = answer.lower().startswith('y')
        else:
            result = default
        return result

    @classmethod
    def question(cls, title, message, default=True):
        return cls.confirm(title, message, '', ok_text=N_('Continue'), default=default)

    @classmethod
    def run_command(cls, title, cmd):
        cls.log('# ' + title)
        cls.log('$ ' + core.list2cmdline(cmd))
        status, out, err = core.run_command(cmd)
        cls.log_status(status, out, err)
        return status, out, err

    @classmethod
    def confirm_config_action(cls, _context, name, _opts):
        return cls.confirm(
            N_('Run %s?') % name,
            N_('Run the "%s" command?') % name,
            '',
            ok_text=N_('Run'),
        )

    @classmethod
    def log_status(cls, status, out, err=None):
        msg = ((out + '\n') if out else '') + ((err + '\n') if err else '')
        cls.log(msg)
        cls.log('exit status %s' % status)

    @classmethod
    def log(cls, message):
        if cls.VERBOSE:
            core.print_stdout(message)

    @classmethod
    def save_as(cls, filename, title):
        if cls.confirm(title, 'Save as %s?' % filename, '', ok_text='Save'):
            return filename
        return None

    @staticmethod
    def async_command(title, command, runtask):
        pass

    @classmethod
    def choose_ref(cls, _context, title, button_text, default=None, icon=None):
        icon = icon or '?'
        cls.information(title, button_text)
        return sys.stdin.readline().strip() or default