Last active
June 4, 2022 21:57
-
-
Save bougui505/dfb224ae757a49075414438bf1fe7cf8 to your computer and use it in GitHub Desktop.
Simple column ruler for shell
This file contains 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 characters
#!/usr/bin/env python3 | |
# -*- coding: UTF8 -*- | |
# Author: Guillaume Bouvier -- [email protected] | |
# https://research.pasteur.fr/en/member/guillaume-bouvier/ | |
# 2020-10-09 15:23:41 (UTC+0200) | |
import sys | |
import argparse | |
parser = argparse.ArgumentParser(description='Column ruler for the shell') | |
parser.add_argument('-g', '--glob', action='store_true', default=False, | |
help='Do not restart numbering at each line') | |
parser.add_argument('-c', '--color', action='store_true', default=False, | |
help='Print with color. termcolor is required') | |
args = parser.parse_args() | |
if args.color: | |
from termcolor import colored, cprint | |
def printer(instr): | |
if args.color: | |
cprint(instr, 'cyan') | |
else: | |
print(instr) | |
def get_rulers(strlen): | |
ruler1_ = '....^....|' | |
ruler2_ = '1234567890' | |
r1len = len(ruler1_) | |
ruler1 = '' | |
ruler2 = '' | |
for i in range((strlen + r1len) // r1len): | |
ruler1 += f'...{i:03d}...|' | |
ruler2 += ruler2_ | |
ruler1 = ruler1[:strlen] | |
ruler2 = ruler2[:strlen] | |
ruler1 += f'-> {len(ruler1)}' | |
return ruler1, ruler2 | |
with sys.stdin as inpipe: | |
lines = [] | |
linelen = [] | |
for line in inpipe: | |
line = line.strip() | |
if args.glob: | |
lines.append(line) | |
linelen.append(len(line)) | |
else: | |
sys.stdout.write(line) | |
sys.stdout.write("\n") | |
strlen = len(line) | |
ruler1, ruler2 = get_rulers(strlen) | |
printer(ruler1) | |
printer(ruler2) | |
if args.glob: | |
lines = ''.join(lines) | |
strlen = len(lines) | |
ruler1, ruler2 = get_rulers(strlen) | |
i = 0 | |
for n in linelen: | |
print(lines[i:i + n]) | |
ruler1__ = ruler1[i:i + n] + f'-> {i + n}' | |
printer(ruler1__) | |
printer(ruler2[i:i + n]) | |
print("") | |
i += n |
This file contains 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 characters
#!/usr/bin/env zsh | |
# -*- coding: UTF8 -*- | |
# Author: Guillaume Bouvier -- [email protected] | |
# https://research.pasteur.fr/en/member/guillaume-bouvier/ | |
# 2020-10-09 13:19:40 (UTC+0200) | |
ruler() { | |
# [ ! -t 0 ] && echo "stdin has data" || echo "stdin is empty" # (See: https://unix.stackexchange.com/a/388462/68794) | |
[ ! -t 0 ] && read PIPEIN && echo $PIPEIN | |
[ ! -t 0 ] && STRLEN=${#PIPEIN} || STRLEN=$COLUMNS | |
s='....^....|' | |
w=${#s}; str=$( for (( i=1; $i<=$(( ($STRLEN + $w) / $w )) ; i=$i+1 )); do | |
DEC=$(printf "%03d" $i-1) | |
echo -n "...$DEC...|" | |
done ) | |
str=$(echo $str | cut -c -$STRLEN) | |
echo $str | |
s='1234567890' | |
w=${#s}; str=$( for (( i=1; $i<=$(( ($STRLEN + $w) / $w )) ; i=$i+1 )); do echo -n $s; done ) | |
str=$(echo $str | cut -c -$STRLEN) | |
echo $str | |
[ ! -t 0 ] && OUTLEN="String length: $STRLEN" && printf "%*s\n" $(((${#title}+$COLUMNS)/2)) "$OUTLEN" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment