Last active
February 4, 2020 12:24
-
-
Save lilydjwg/9891346 to your computer and use it in GitHub Desktop.
Revisions
-
lilydjwg revised this gist
Nov 11, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -64,7 +64,7 @@ def rainbow(freq, i): def colorline(line, lineno=0, *, width=0): global init_width term_w = get_terminal_size(2)[1] if not width: if not init_width: init_width = term_w - 1 -
lilydjwg revised this gist
Nov 11, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
Empty file. -
lilydjwg revised this gist
Nov 11, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -59,7 +59,7 @@ def get_terminal_size(fd=1): def rainbow(freq, i): h = i * freq r, g, b = (int(round(x * 255)) for x in hsv_to_rgb(h, 1, 1)) return "#%02X%02X%02X" % (r, g, b) def colorline(line, lineno=0, *, width=0): @@ -111,4 +111,4 @@ def colored(ch, color): inc = colorline(line, i, width=args.width) i += inc except KeyboardInterrupt: print() -
lilydjwg revised this gist
Mar 31, 2014 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import sys import re import os from math import ceil from colorsys import hsv_to_rgb from unicodedata import east_asian_width -
lilydjwg revised this gist
Mar 31, 2014 . 1 changed file with 32 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -79,4 +79,35 @@ def colorline(line, lineno=0, *, width=0): else: col += 1 sys.stdout.write('\x1b[0m') # reset return ceil(col / term_w) def colored(ch, color): c = hex2term(color) return '\x1b[01;0;38;5;{c}m{ch}'.format(c=c, ch=ch) if __name__ == '__main__': import argparse import signal parser = argparse.ArgumentParser(description='Okay, no unicorns. But rainbows! In Python.') parser.add_argument('-w', '--width', type=int, metavar='N', help='rainbow width. default: half of terminal width-1') parser.add_argument('-i', '--ignore-interrupts', action='store_true', help='ignore interrupt signals') parser.add_argument('files', nargs='*', metavar='FILE', type=argparse.FileType('r'), help='files to cat. default: stdin') args = parser.parse_args() if args.ignore_interrupts: signal.signal(signal.SIGINT, signal.SIG_IGN) files = args.files or [sys.stdin] try: i = 0 for f in files: for line in f: inc = colorline(line, i, width=args.width) i += inc except KeyboardInterrupt: print() -
lilydjwg revised this gist
Mar 31, 2014 . 1 changed file with 1 addition and 33 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -70,7 +70,6 @@ def colorline(line, lineno=0, *, width=0): width = init_width / 2 freq = 1 / width col = 0 line = COLOR_CODE_RE.sub('', line) for c in line: @@ -80,35 +79,4 @@ def colorline(line, lineno=0, *, width=0): else: col += 1 sys.stdout.write('\x1b[0m') # reset -
lilydjwg revised this gist
Mar 31, 2014 . 1 changed file with 7 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -54,15 +54,20 @@ def get_terminal_size(fd=1): return hw COLOR_CODE_RE = re.compile(r'\x1b\[(?:\d*)(?:;\d+)*[mK]') init_width = 0 def rainbow(freq, i): h = i * freq r, g, b = (x * 255 for x in hsv_to_rgb(h, 1, 1)) return "#%02X%02X%02X" % (r, g, b) def colorline(line, lineno=0, *, width=0): global init_width term_w = get_terminal_size()[1] if not width: if not init_width: init_width = term_w - 1 width = init_width / 2 freq = 1 / width out = sys.stdout @@ -75,7 +80,7 @@ def colorline(line, lineno=0, *, width=0): else: col += 1 sys.stdout.write('\x1b[0m') # reset return ceil(col / term_w) def colored(ch, color): c = hex2term(color) -
lilydjwg revised this gist
Mar 31, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -87,7 +87,7 @@ def colored(ch, color): parser = argparse.ArgumentParser(description='Okay, no unicorns. But rainbows! In Python.') parser.add_argument('-w', '--width', type=int, metavar='N', help='rainbow width. default: half of terminal width-1') parser.add_argument('-i', '--ignore-interrupts', action='store_true', help='ignore interrupt signals') parser.add_argument('files', nargs='*', metavar='FILE', -
lilydjwg revised this gist
Mar 31, 2014 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import sys import re from math import ceil from colorsys import hsv_to_rgb from unicodedata import east_asian_width @@ -61,7 +62,7 @@ def rainbow(freq, i): def colorline(line, lineno=0, *, width=0): if not width: width = (get_terminal_size()[1] - 1) / 2 freq = 1 / width out = sys.stdout @@ -74,6 +75,7 @@ def colorline(line, lineno=0, *, width=0): else: col += 1 sys.stdout.write('\x1b[0m') # reset return ceil(col / width) def colored(ch, color): c = hex2term(color) @@ -101,7 +103,7 @@ def colored(ch, color): i = 0 for f in files: for line in f: inc = colorline(line, i, width=args.width) i += inc except KeyboardInterrupt: print() -
lilydjwg revised this gist
Mar 31, 2014 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,7 @@ #!/usr/bin/env python3 # inspired by https://github.com/busyloop/lolcat import sys import re from colorsys import hsv_to_rgb @@ -82,7 +84,7 @@ def colored(ch, color): import signal parser = argparse.ArgumentParser(description='Okay, no unicorns. But rainbows! In Python.') parser.add_argument('-w', '--width', type=int, metavar='N', help='rainbow width. default: half of terminal width') parser.add_argument('-i', '--ignore-interrupts', action='store_true', help='ignore interrupt signals') -
lilydjwg created this gist
Mar 31, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,105 @@ #!/usr/bin/env python3 import sys import re from colorsys import hsv_to_rgb from unicodedata import east_asian_width try: # in https://github.com/lilydjwg/winterpy from colorfinder import hex2term_accurate as hex2term except ImportError: def hex2term(c): red, green, blue = (int(x, 16) for x in (c[1:3], c[3:5], c[5:7])) # from ruby-paint gray_possible = True sep = 42.5 gray = False while gray_possible: if red < sep or green < sep or blue < sep: gray = red < sep and green < sep and blue < sep gray_possible = False sep += 42.5 if gray: return 232 + (red + green + blue) // 33 else: return 16 + sum(6 * x // 256 * 6 ** i for i, x in enumerate((blue, green, red))) def get_terminal_size(fd=1): """ Returns height and width of current terminal. First tries to get size via termios.TIOCGWINSZ, then from environment. Defaults to 25 lines x 80 columns if both methods fail. :param fd: file descriptor (default: 1=stdout) from: http://blog.taz.net.au/2012/04/09/getting-the-terminal-size-in-python/ """ try: import fcntl, termios, struct hw = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')) except Exception: try: hw = (os.environ['LINES'], os.environ['COLUMNS']) except Exception: hw = (25, 80) return hw COLOR_CODE_RE = re.compile(r'\x1b\[(?:\d*)(?:;\d+)*[mK]') def rainbow(freq, i): h = i * freq r, g, b = (x * 255 for x in hsv_to_rgb(h, 1, 1)) return "#%02X%02X%02X" % (r, g, b) def colorline(line, lineno=0, *, width=0): if not width: width = get_terminal_size()[1] / 2 freq = 1 / width out = sys.stdout col = 0 line = COLOR_CODE_RE.sub('', line) for c in line: sys.stdout.write(colored(c, rainbow(freq, col+lineno))) if east_asian_width(c) in 'WF': col += 2 else: col += 1 sys.stdout.write('\x1b[0m') # reset def colored(ch, color): c = hex2term(color) return '\x1b[01;0;38;5;{c}m{ch}'.format(c=c, ch=ch) if __name__ == '__main__': import argparse import signal parser = argparse.ArgumentParser(description='Okay, no unicorns. But rainbows! In Python.') parser.add_argument('-w', '--width', type=int, help='rainbow width. default: half of terminal width') parser.add_argument('-i', '--ignore-interrupts', action='store_true', help='ignore interrupt signals') parser.add_argument('files', nargs='*', metavar='FILE', type=argparse.FileType('r'), help='files to cat. default: stdin') args = parser.parse_args() if args.ignore_interrupts: signal.signal(signal.SIGINT, signal.SIG_IGN) files = args.files or [sys.stdin] try: i = 0 for f in files: for line in f: colorline(line, i, width=args.width) i += 1 except KeyboardInterrupt: print()