Skip to content

Instantly share code, notes, and snippets.

@lilydjwg
Last active February 4, 2020 12:24
Show Gist options
  • Select an option

  • Save lilydjwg/9891346 to your computer and use it in GitHub Desktop.

Select an option

Save lilydjwg/9891346 to your computer and use it in GitHub Desktop.

Revisions

  1. lilydjwg revised this gist Nov 11, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion lolcat.py
    Original 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()[1]
    term_w = get_terminal_size(2)[1]
    if not width:
    if not init_width:
    init_width = term_w - 1
  2. lilydjwg revised this gist Nov 11, 2016. 1 changed file with 0 additions and 0 deletions.
    Empty file modified lolcat.py
    100644 → 100755
    Empty file.
  3. lilydjwg revised this gist Nov 11, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions lolcat.py
    Original 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 = (x * 255 for x in hsv_to_rgb(h, 1, 1))
    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()
    print()
  4. lilydjwg revised this gist Mar 31, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions lolcat.py
    Original 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
  5. lilydjwg revised this gist Mar 31, 2014. 1 changed file with 32 additions and 1 deletion.
    33 changes: 32 additions & 1 deletion lolcat.py
    Original 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()
  6. lilydjwg revised this gist Mar 31, 2014. 1 changed file with 1 addition and 33 deletions.
    34 changes: 1 addition & 33 deletions lolcat.py
    Original 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

    out = sys.stdout
    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
    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()

  7. lilydjwg revised this gist Mar 31, 2014. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions lolcat.py
    Original 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:
    width = (get_terminal_size()[1] - 1) / 2
    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 / width)
    return ceil(col / term_w)

    def colored(ch, color):
    c = hex2term(color)
  8. lilydjwg revised this gist Mar 31, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion lolcat.py
    Original 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')
    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',
  9. lilydjwg revised this gist Mar 31, 2014. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions lolcat.py
    Original 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] / 2
    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:
    colorline(line, i, width=args.width)
    i += 1
    inc = colorline(line, i, width=args.width)
    i += inc
    except KeyboardInterrupt:
    print()
  10. lilydjwg revised this gist Mar 31, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion lolcat.py
    Original 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,
    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')
  11. lilydjwg created this gist Mar 31, 2014.
    105 changes: 105 additions & 0 deletions lolcat.py
    Original 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()