~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to hunk_selector.py

  • Committer: Aaron Bentley
  • Date: 2006-11-23 00:05:45 UTC
  • mfrom: (0.8.7 show_paths)
  • Revision ID: aaron.bentley@utoronto.ca-20061123000545-vk3id8fxuzzclvsh
Add show-paths command from Alexander Belchenko

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
                else:
 
18
                    if color is True:
 
19
                        raise NoColor()
 
20
                    self.diff_stream = sys.stdout
 
21
            except ImportError:
 
22
                if color is True:
 
23
                    raise NoBzrtoolsColor()
 
24
                self.diff_stream = sys.stdout
 
25
        else:
 
26
            self.diff_stream = sys.stdout
 
27
            
10
28
        self.standard_options = [
11
29
            UserOption('y', self._selected, self.strings['select_desc'],
12
30
                default=True),
65
83
    # Called once for each hunk
66
84
    def _hunk_callback(self, hunk, count):
67
85
        if self.last_printed != count:
68
 
            print hunk.patch.get_header(), hunk
 
86
            self.diff_stream.write(str(hunk.patch.get_header()))
 
87
            self.diff_stream.write(str(hunk))
69
88
            self.last_printed = count
70
89
 
71
90
        if hunk.selected:
153
172
        return (selected_patches, unselected_patches)
154
173
 
155
174
class ShelveHunkSelector(HunkSelector):
156
 
    def __init__(self, patches):
 
175
    def __init__(self, patches, color=None):
157
176
        self.strings = {}
158
177
        self.strings['status_selected'] = '%d hunks to be shelved'
159
178
        self.strings['status_unselected'] = '%d hunks to be kept'
162
181
        self.strings['finish_desc'] = 'shelve selected changes.'
163
182
        self.strings['prompt'] = 'Shelve this change? (%(count)d of %(total)d)'
164
183
        self.strings['end_prompt'] = 'Shelve these changes?'
165
 
        HunkSelector.__init__(self, patches)
 
184
        HunkSelector.__init__(self, patches, color)
166
185
 
167
186
class UnshelveHunkSelector(HunkSelector):
168
 
    def __init__(self, patches):
 
187
    def __init__(self, patches, color=None):
169
188
        self.strings = {}
170
189
        self.strings['status_selected'] = '%d hunks to be unshelved'
171
190
        self.strings['status_unselected'] = '%d hunks left on shelf'
175
194
        self.strings['prompt'] = 'Unshelve this change? ' \
176
195
            '(%(count)d of %(total)d)'
177
196
        self.strings['end_prompt'] = 'Unshelve these changes?'
178
 
        HunkSelector.__init__(self, patches)
 
197
        HunkSelector.__init__(self, patches, color)