Ejemplo n.º 1
0
def main():
    (options, args) = parser.parse_args()
    if len(args) == 2:
        search_dir, string = args
        search_dir = py.path.local(search_dir)
    else:
        search_dir = py.path.local()
        string = args[0]
    if options.ignorecase:
        string = string.lower()

    for x in search_dir.visit(options.glob, rec):
        # match filename directly
        if x.check(dir=1):
            continue
        s = x.relto(search_dir)
        if options.ignorecase:
            s = s.lower()
        if s.find(string) != -1:
            sys.stdout.write("%s: filename matches %r" %(x, string) + "\n")

        try:
            with x.open("rb") as f:
                bytecontent = f.read()
        except py.error.ENOENT:
            continue # whatever, probably broken link (ie emacs lock)
        for encoding in ("UTF-8", "latin1"):
            try:
                s = py.builtin._totext(bytecontent, encoding)
            except UnicodeDecodeError:
                continue
            else:
                break
        searchs = s
        if options.ignorecase:
            searchs = s.lower()
        if s.find(string) != -1:
            lines = s.splitlines()
            if options.ignorecase:
                searchlines = s.lower().splitlines()
            else:
                searchlines = lines
            for i, (line, searchline) in enumerate(zip(lines, searchlines)):
                indexes = find_indexes(searchline, string)
                if not indexes:
                    continue
                if not options.context:
                    sys.stdout.write("%s:%d: " %(x.relto(search_dir), i+1))
                    last_index = 0
                    for index in indexes:
                        sys.stdout.write(line[last_index: index])
                        ansi_print(line[index: index+len(string)],
                                   file=sys.stdout, esc=31, newline=False)
                        last_index = index + len(string)
                    sys.stdout.write(line[last_index:] + "\n")
                else:
                    context = (options.context)/2
                    for count in range(max(0, i-context), min(len(lines) - 1, i+context+1)):
                        print("%s:%d:  %s" %(x.relto(search_dir), count+1, lines[count].rstrip()))
                    print("-" * terminal_width)
Ejemplo n.º 2
0
def print_pixel(colour, value_range, invert=1):
    global colour_range
    chars = [".", ".", "+", "*", "%", "#"]
    idx = lambda chars: (colour + 1) * (len(chars) - 1) / value_range
    if invert:
        idx = lambda chars, idx=idx: len(chars) - 1 - idx(chars)
    char = chars[idx(chars)]
    ansi_colour = palette[idx(palette)]
    ansi_print(char, ansi_colour, newline=False, flush=True)
Ejemplo n.º 3
0
def print_pixel(colour, value_range, invert=1):
    global colour_range
    chars = [".", ".", "+", "*", "%", "#"]
    idx = lambda chars: (colour + 1) * (len(chars) - 1) / value_range
    if invert:
        idx = lambda chars, idx=idx: len(chars) - 1 - idx(chars)
    char = chars[idx(chars)]
    ansi_colour = palette[idx(palette)]
    ansi_print(char, ansi_colour, newline=False, flush=True)
Ejemplo n.º 4
0
 def logger_method(self, text):
     global wrote_dot
     if self.output_disabled:
         return
     text = "[%s%s] %s" % (self.name, subname, text)
     if isatty():
         col = colors
     else:
         col = ()
     if wrote_dot:
         text = '\n' + text
     ansi_print(text, col)
     wrote_dot = False
Ejemplo n.º 5
0
 def logger_method(self, text):
     global wrote_dot
     if self.output_disabled:
         return
     text = "[%s%s] %s" % (self.name, subname, text)
     if isatty():
         col = colors
     else:
         col = ()
     if wrote_dot:
         text = '\n' + text
     ansi_print(text, col)
     wrote_dot = False
Ejemplo n.º 6
0
 def print_pixel(self, colour, invert=1):
     chars = [".", ".", "+", "*", "%", "#"]
     idx = lambda chars: (colour+1) * (len(chars) - 1) / self.max_colour
     if invert:
         idx = lambda chars, idx=idx:len(chars) - 1 - idx(chars)
     char = chars[idx(chars)]
     ansi_colour = palette[idx(palette)]
     ansi_print(char, ansi_colour, newline=False, flush=True)
     if DEBUG:
         if self.colour_range is None:
             self.colour_range = [colour, colour]
         else:
             old_colour_range = self.colour_range
             self.colour_range = [min(self.colour_range[0], colour), max(self.colour_range[1], colour)]
             if old_colour_range[0] - colour > 3 or colour - old_colour_range[1] > 3:
                 return True
Ejemplo n.º 7
0
 def __call__(self, msg):
     tty = self.isatty()
     flush = False
     newline = True
     keywords = []
     esc = []
     for kw in msg.keywords:
         color, supress = self.kw_to_color.get(kw, (None, False))
         if color:
             esc.extend(color)
         if not supress:
             keywords.append(kw)
     if 'start' in keywords:
         if tty:
             newline = False
             flush = True
             keywords.remove('start')
     elif 'done' in keywords:
         if tty:
             print >> sys.stderr
             return
     elif 'dot' in keywords:
         if tty:
             if self.fancy:
                 if not AnsiLog.wrote_dot:
                     self.mandelbrot_driver.reset()
                 self.mandelbrot_driver.dot()
             else:
                 ansi_print(".",
                            tuple(esc),
                            file=self.file,
                            newline=False,
                            flush=flush)
             AnsiLog.wrote_dot = True
             return
     if AnsiLog.wrote_dot:
         AnsiLog.wrote_dot = False
         sys.stderr.write("\n")
     esc = tuple(esc)
     for line in msg.content().splitlines():
         ansi_print("[%s] %s" % (":".join(keywords), line),
                    esc,
                    file=self.file,
                    newline=newline,
                    flush=flush)
Ejemplo n.º 8
0
 def __call__(self, msg):
     tty = self.isatty()
     flush = False
     newline = True
     keywords = []
     esc = []
     for kw in msg.keywords:
         color, supress = self.kw_to_color.get(kw, (None, False))
         if color:
             esc.extend(color)
         if not supress:
             keywords.append(kw)
     if 'start' in keywords:
         if tty:
             newline = False
             flush = True
             keywords.remove('start')
     elif 'done' in keywords:
         if tty:
             print >> sys.stderr
             return
     elif 'dot' in keywords:
         if tty:
             if self.fancy:
                 if not AnsiLog.wrote_dot:
                     self.mandelbrot_driver.reset()
                 self.mandelbrot_driver.dot()
             else:
                 ansi_print(".", tuple(esc), file=self.file, newline=False, flush=flush)
             AnsiLog.wrote_dot = True
             return
     if AnsiLog.wrote_dot:
         AnsiLog.wrote_dot = False
         sys.stderr.write("\n")
     esc = tuple(esc)
     for line in msg.content().splitlines():
         ansi_print("[%s] %s" %(":".join(keywords), line), esc, 
                    file=self.file, newline=newline, flush=flush)
Ejemplo n.º 9
0
#! /usr/bin/env python
import colorsys

def hsv2ansi(h, s, v):
    # h: 0..1, s/v: 0..1
    if s < 0.1:
        return int(v * 23) + 232
    r, g, b = map(lambda x: int(x * 5), colorsys.hsv_to_rgb(h, s, v))
    return 16 + (r * 36) + (g * 6) + b

def ramp_idx(i, num):
    assert num > 0
    i0 = float(i) / num
    h = 0.57 + i0
    s = 1 - pow(i0,3)
    v = 1
    return hsv2ansi(h, s, v)

def ansi_ramp(num):
    return [ramp_idx(i, num) for i in range(num)]

ansi_ramp80 = ansi_ramp(80)

if __name__ == '__main__':
    import sys
    from py.io import ansi_print
    colors = int(sys.argv[1]) if len(sys.argv) > 1 else 80
    for col in range(colors):
        ansi_print('#', "38;5;%d" % ramp_idx(col, colors), newline=False, flush=True)