~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to hunk_selector.py

  • Committer: Aaron Bentley
  • Date: 2007-06-12 22:09:44 UTC
  • mfrom: (540.1.2 bzrtools-0.17)
  • Revision ID: aaron.bentley@utoronto.ca-20070612220944-5zw4hlzp1ctq6mkl
Merge fixes from 0.17

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
 
1
import sys
2
2
 
3
3
from userinteractor import UserInteractor, UserOption
 
4
from errors import NoColor
4
5
import copy
5
6
 
6
7
class HunkSelector:
7
8
    strings = {}
8
9
 
9
 
    def __init__(self, patches):
 
10
    def __init__(self, patches, color=None):
 
11
        if color is True or color is None:
 
12
            try:
 
13
                from colordiff import DiffWriter
 
14
                from terminal import has_ansi_colors
 
15
                if has_ansi_colors():
 
16
                    self.diff_stream = DiffWriter(sys.stdout,
 
17
                                                  check_style=False)
 
18
                else:
 
19
                    if color is True:
 
20
                        raise NoColor()
 
21
                    self.diff_stream = sys.stdout
 
22
            except ImportError:
 
23
                if color is True:
 
24
                    raise NoBzrtoolsColor()
 
25
                self.diff_stream = sys.stdout
 
26
        else:
 
27
            self.diff_stream = sys.stdout
 
28
 
10
29
        self.standard_options = [
11
30
            UserOption('y', self._selected, self.strings['select_desc'],
12
31
                default=True),
65
84
    # Called once for each hunk
66
85
    def _hunk_callback(self, hunk, count):
67
86
        if self.last_printed != count:
68
 
            print hunk.patch.get_header(), hunk
 
87
            self.diff_stream.write(str(hunk.patch.get_header()))
 
88
            self.diff_stream.write(str(hunk))
69
89
            self.last_printed = count
70
90
 
71
91
        if hunk.selected:
153
173
        return (selected_patches, unselected_patches)
154
174
 
155
175
class ShelveHunkSelector(HunkSelector):
156
 
    def __init__(self, patches):
 
176
    def __init__(self, patches, color=None):
157
177
        self.strings = {}
158
178
        self.strings['status_selected'] = '%d hunks to be shelved'
159
179
        self.strings['status_unselected'] = '%d hunks to be kept'
162
182
        self.strings['finish_desc'] = 'shelve selected changes.'
163
183
        self.strings['prompt'] = 'Shelve this change? (%(count)d of %(total)d)'
164
184
        self.strings['end_prompt'] = 'Shelve these changes?'
165
 
        HunkSelector.__init__(self, patches)
 
185
        HunkSelector.__init__(self, patches, color)
166
186
 
167
187
class UnshelveHunkSelector(HunkSelector):
168
 
    def __init__(self, patches):
 
188
    def __init__(self, patches, color=None):
169
189
        self.strings = {}
170
190
        self.strings['status_selected'] = '%d hunks to be unshelved'
171
191
        self.strings['status_unselected'] = '%d hunks left on shelf'
175
195
        self.strings['prompt'] = 'Unshelve this change? ' \
176
196
            '(%(count)d of %(total)d)'
177
197
        self.strings['end_prompt'] = 'Unshelve these changes?'
178
 
        HunkSelector.__init__(self, patches)
 
198
        HunkSelector.__init__(self, patches, color)