~lovesyao/bzr/windows-utf8

« back to all changes in this revision

Viewing changes to bzr

  • Committer: Nazo
  • Date: 2008-11-20 04:36:24 UTC
  • Revision ID: lovesyao@gmail.com-20081120043624-1chwwppwhow5rt8f
cleanup code

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
    os.unsetenv(REINVOKE)
53
53
 
54
54
 
55
 
import sys
56
55
if sys.platform == 'win32' and sys.stdout.isatty():
57
 
    from ctypes import windll, create_string_buffer, Structure, c_short, c_ushort, byref
 
56
    from ctypes import windll, Structure, c_short, c_ushort, byref
58
57
    import codecs
59
58
 
60
 
    class WindowsConsoleWriter():
 
59
    class COORD(Structure):
 
60
        _fields_ = [
 
61
            ("X", c_short),
 
62
            ("Y", c_short)
 
63
        ]
 
64
 
 
65
    class SMALL_RECT(Structure):
 
66
        _fields_ = [
 
67
            ("Left", c_short),
 
68
            ("Top", c_short),
 
69
            ("Right", c_short),
 
70
            ("Bottom", c_short)
 
71
        ]
 
72
 
 
73
    class CONSOLE_SCREEN_BUFFER_INFO(Structure):
 
74
        _fields_ = [
 
75
            ("dwSize", COORD),
 
76
            ("dwCursorPosition", COORD),
 
77
            ("wAttributes", c_ushort),
 
78
            ("srWindow", SMALL_RECT),
 
79
            ("dwMaximumWindowSize", COORD)
 
80
        ]
 
81
 
 
82
    STD_OUTPUT_HANDLE = -11
 
83
 
 
84
    def consolereplace_errors(exception):
 
85
        if not isinstance(exception, UnicodeDecodeError):
 
86
            raise TypeError("consolereplace() cannot handle %r" % exception)
 
87
        l = [u"\\%x" % ord(exception.object[pos]) for pos in xrange(exception.start, exception.end)]
 
88
        return (u"\ufffe%s\ufeff" % u"".join(l), exception.end)
 
89
 
 
90
    codecs.register_error("consolereplace", consolereplace_errors)
 
91
 
 
92
    FOREGROUND_RED = 4
 
93
 
 
94
    class WindowsConsoleWriter:
61
95
        def __init__(self, errors='strict'):
62
96
            self.errors = errors
63
97
            self.encoding = 'utf_16'
64
 
            self.handle = windll.kernel32.GetStdHandle(-11)
65
 
 
66
 
            class COORD(Structure):
67
 
                _fields_ = [
68
 
                    ("X", c_short),
69
 
                    ("Y", c_short)
70
 
                ]
71
 
 
72
 
            class SMALL_RECT(Structure):
73
 
                _fields_ = [
74
 
                    ("Left", c_short),
75
 
                    ("Top", c_short),
76
 
                    ("Right", c_short),
77
 
                    ("Bottom", c_short)
78
 
                ]
79
 
 
80
 
            class CONSOLE_SCREEN_BUFFER_INFO(Structure):
81
 
                _fields_ = [
82
 
                    ("dwSize", COORD),
83
 
                    ("dwCursorPosition", COORD),
84
 
                    ("wAttributes", c_ushort),
85
 
                    ("srWindow", SMALL_RECT),
86
 
                    ("dwMaximumWindowSize", COORD)
87
 
                ]
 
98
            self.handle = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
88
99
 
89
100
            csbi = CONSOLE_SCREEN_BUFFER_INFO()
90
101
            windll.kernel32.GetConsoleScreenBufferInfo(self.handle, byref(csbi))
95
106
            self.write(text + "\n")
96
107
 
97
108
        def _write(self, text):
98
 
            windll.kernel32.WriteConsoleW(self.handle
99
 
                , text, len(text), 0, 0)
 
109
            windll.kernel32.WriteConsoleW(self.handle, text, len(text), 0, 0)
100
110
 
101
111
        def _color(self, color):
102
112
            windll.kernel32.SetConsoleTextAttribute(self.handle, color)
103
113
 
104
114
        def write(self, text):
105
 
            def consolereplace_errors(exception):
106
 
                if not isinstance(exception, UnicodeDecodeError):
107
 
                    raise TypeError("consolereplace() cannot handle %r" % exception)
108
 
                l = [u"\ufffe\\%x\ufeff" % ord(exception.object[pos]) for pos in xrange(exception.start, exception.end)]
109
 
                return (u"%s" % u"".join(l), exception.end)
110
 
 
111
 
            codecs.register_error("consolereplace", consolereplace_errors)
112
 
 
113
115
            text = unicode(text, 'utf_8', errors='consolereplace')
114
 
            i = False
115
 
            for x in text.split(u"\ufffe"):
116
 
                if i:
117
 
                    self._color(0x4)
118
 
                else:
119
 
                    i = True
120
 
                b = x.split(u"\ufeff")
121
 
                j = False
122
 
                for y in b:
123
 
                    if j:
124
 
                        self._color(self.defattr)
125
 
                    else:
126
 
                        j = True
127
 
                    self._write(y)
 
116
            i = 0
 
117
            j = 0
 
118
            for c in text:
 
119
                if c == u'\ufffe':
 
120
                    self._write(text[i:j])
 
121
                    i = j+1
 
122
                    self._color(FOREGROUND_RED)
 
123
                elif c == u'\ufeff':
 
124
                    self._write(text[i:j])
 
125
                    i = j+1
 
126
                    self._color(self.defattr)
 
127
                j+=1
 
128
            self._write(text[i:j])
128
129
 
129
130
        def isatty(self): return True 
130
131