Archive

Archive for January, 2012

trim trailing spaces from files

January 31st, 2012 No comments
1
2
find . -name "*.h" -print0 | xargs -0 perl -pi -e 's/ +$//'
find . -name "*.cpp" -print0 | xargs -0 perl -pi -e 's/ +$//'
Categories: coding Tags:

python and windows terminal colors: the lightweight version

January 3rd, 2012 No comments

its hard to get colored output on windows if you do not want to pull in any dependencies, so i wrote my own little class. The handy thing is, that it wraps around the stdout and parses the stream to colorize the output.

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
import sys
import ctypes

# Constants from the Windows API
class WTCW: #WindowsTerminalColourWrapper:
    WRAPPERS = ['{', '}']
   
    # windows api constants: DO NOT CHANGE
    STD_INPUT_HANDLE = -10 ; STD_OUTPUT_HANDLE= -11 ; STD_ERROR_HANDLE = -12
    FG_BLUE = 0x01 ; FG_GREEN= 0x02 ; FG_RED  = 0x04 ; FG_INTENSITY = 0x08 ; BG_BLUE = 0x10 ; BG_GREEN= 0x20 ; BG_RED  = 0x40 ; BG_INTENSITY = 0x80
    colors = {'r': FG_RED, 'g':FG_GREEN, 'b':FG_BLUE, 'R':BG_RED, 'G':BG_GREEN, 'B':BG_BLUE, 'i':FG_INTENSITY, 'I':BG_INTENSITY, 'w':FG_RED|FG_GREEN|FG_BLUE }
   
    def _get_csbi_attributes(self):
        import struct
        csbi = ctypes.create_string_buffer(22) ; res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(self.handle, csbi) ; assert res
        (bufx, bufy, curx, cury, wattr, left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
        return wattr
    def __init__(self, stream, defColor = ''):
        self.stream = stream ; self.defColor = self._parseColor(defColor)
        self.handle = ctypes.windll.kernel32.GetStdHandle(self.STD_OUTPUT_HANDLE)
        self.reset  = self._get_csbi_attributes()
    def __del__(self): self.resetColor()
    def _parseColor(self, str):
        colval = 0
        for c in str:
            if c.lower() == 'x':
                self.resetColor()
                break
            colval |= self.colors[c]
        return colval
   
    def write(self, data):
        if self.defColor != 0:
            self.setColour(defColor)
        parts = data.split(self.WRAPPERS[0])
        for part in parts:
            f = part.find(self.WRAPPERS[1])
            if f != -1:
                self.setColour(self._parseColor(part[0:f]))
                self.stream.write(part[f+len(self.WRAPPERS[1]):])
            else:
                self.stream.write(part)
            self.stream.flush()
        #self.resetColor() #uncomment this to reset the color after every invocation
    def __getattr__(self, attr): return getattr(self.stream, attr)
    def setColour(self, col): ctypes.windll.kernel32.SetConsoleTextAttribute(self.handle, col)
    def resetColor(self): ctypes.windll.kernel32.SetConsoleTextAttribute(self.handle, self.reset)

#how to use: wrap around streams
sys.stdout = WTCW(sys.stdout)
#sys.stderr = WTCW(sys.stderr)

# examples, syntax is {color}:
print '{r}red {g}green {b}blue {x}normal'
print '{rgb}white {rg}yellow {rb}purple {gb}turquois {x}normal'
print '{w}white {rg}yellow {rb}purple {gb}turquois {x}normal'
print '{ri}red {gi}green {bi}blue {x}normal'
print '{R}red {G}green {B}blue {x}normal'
print '{rG}red {gR}green {bR}blue {x}normal'
print '{rGI}red {gRI}green {bRI}blue {x}normal'

the output looks like this:

if you want a good solution when you do not care about dependencies, i would suggest reading this thread

Categories: coding, python Tags: