Skip to content

Instantly share code, notes, and snippets.

@mdmitry1
Last active June 1, 2024 14:31
Show Gist options
  • Save mdmitry1/464819af55a87be0dc200d7cd1ef0a56 to your computer and use it in GitHub Desktop.
Save mdmitry1/464819af55a87be0dc200d7cd1ef0a56 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3.11
'''
https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
https://www.codequoi.com/en/coloring-terminal-text-tput-and-ansi-escape-sequences
https://web.archive.org/web/20210226122732/http://ascii-table.com/ansi-escape-sequences.php
'''
from sys import argv
from argparse import ArgumentParser
def print_color(fg_bg_code, color):
print(fg_bg_code + color + "m " + color.ljust(4),end="")
def print_colors(fg_bg_code,palette):
for i in range(0, 256):
print_color(fg_bg_code, palette[i]);
if( i % 16 == 15): print(u"\033[0m")
def print_color_pair(c1, c2):
combined_code=u"\033[38;5;" + c1 + "m" + \
u"\033[48;5;" + c2 + "m "
c1c2=c1 + ":" + c2
print(combined_code + c1c2.ljust(7),end="")
def print_color_combinations(palette, r1, r2, c1, c2):
for i in range(r1, r2):
for j in range(c1, c2):
print_color_pair(palette[i],palette[j]);
if( j % 16 == 15 or j == c2 - 1): print(u"\033[0m")
colors=[]
[colors.append(str(i)) for i in range(0, 256)]
print_colors(u"\033[38;5;", colors)
print_colors(u"\033[48;5;", colors)
if len(argv) > 1:
parser = ArgumentParser()
parser.add_argument('--row1', '-r1', type=int, default=0)
parser.add_argument('--row2', '-r2', type=int, default=256)
parser.add_argument('--column1', '-c1', type=int, default=0)
parser.add_argument('--column2', '-c2', type=int, default=256)
args=parser.parse_args()
print_color_combinations(colors,args.row1, \
args.row2, \
args.column1, \
args.column2 )
combined_code=u"\033[38;5;200m" + u"\033[48;5;14m"
print(combined_code + u"\033[1m BOLD \033[0m")
print(combined_code + u"\033[2m Regular \033[0m")
print(combined_code + u"\033[3m Italic \033[0m")
print(combined_code + u"\033[4m Underline \033[0m")
print(combined_code + u"\033[5m Blink \033[0m")
print(combined_code + u"\033[7m Reversed \033[0m")
print(combined_code + u"\033[8m Concealed \033[0m")
print(combined_code + u"\033[9m Strikethrough \033[0m")
#!/usr/bin/bash -f
export MYVAR="MYVAL"
perlbin=/usr/bin/perl
if [ $# -ge 2 ]; then perlbin=$2; fi
#! -*-perl-*-
eval 'exec $perlbin -x -S $0 ${1+"$@"};'
if $running_under_some_shell;
use Data::Dumper;
use Math::Trig;
printf("running %s v(%vd)\n", $^X, $^V);
print Dumper \@ARGV;
print $ENV{MYVAR},"\n";
my $sum=0;
my $n=1e6;
$#ARGV >=0 ? $n = $ARGV[0] : print "n = $n\n";
for(my $i=1; $i < $n; $i++) {$sum += 6 / $i**2; }
print "\033[38;5;4m\033[48;5;7m", sqrt($sum)/pi-1, "\033[0m\n";
#!/usr/bin/tcsh -f
"/usr/bin/true" '''\'
if($#argv < 2 ) then
exec /usr/bin/python3.11 $0
else
exec $argv[1] $0 $*
endif
'''
from os import name as osname
from os.path import basename, realpath
from numpy import float64
from ctypes import *
from sys import argv, version
from rich import print as rprint
from re import sub
script_name=basename(realpath(argv[0]))
n = "1" if len(argv) < 3 else argv[2]
libc=cdll.msvcrt if 'nt' == osname else cdll.LoadLibrary("libc.so.6")
printf=libc.printf
print(sub('\n','',version))
printf(b"n = %s\n", n.encode('utf-8'))
libm=cdll.msvcrt if 'nt' == osname else cdll.LoadLibrary("libm.so.6")
sin=libm.sin
sin.argtypes=[c_double]
sin.restype=c_double
printf.argtypes = [c_char_p, c_double]
try:
printf(b"\033[38;5;4m\033[48;5;7m" + b"sin(n) = %18.16lf\033[0m\n", sin(float64(n)))
except Exception as err:
rprint("\n[magenta] " + script_name + ":", "[red] ERROR: [/red]", "[red] " + str(err), "\n")
exit(1)
#!/usr/bin/python3.11
from argparse import ArgumentParser
from schwifty import IBAN
import sys
from os import _exit
from rich.console import Console
console_256=Console(color_system="256")
parser = ArgumentParser()
parser.add_argument('--bank', '-bnk', required=True)
parser.add_argument('--branch', '-brn', required=True)
parser.add_argument('--account', '-n', required=True)
args=parser.parse_args()
if not args.bank.isdigit():
console_256.print("[red on cornsilk1]" + "\nERROR: bank code should contain digits only\n")
_exit(-1)
if not args.branch.isdigit():
console_256.print("[red on cornsilk1]" + "\nERROR: branch code should contain digits only\n")
_exit(-1)
if not args.account.isdigit():
console_256.print("[red on cornsilk1]" + "\nERROR: account number should contain digits only\n")
_exit(-1)
if len(args.bank) > 2:
console_256.print("[red on cornsilk1]" + "\nERROR: bank code exceeds maximum size of 2\n")
_exit(-1)
if len(args.branch) > 3:
console_256.print("[red on cornsilk1]" + "\nERROR: branch code exceeds maximum size of 3\n")
_exit(-1)
if len(args.account) > 8:
console_256.print("[red on cornsilk1]" + "\nERROR: account number exceeds maximum size of 8\n")
_exit(-1)
iban=IBAN.generate("IL", bank_code=args.bank, branch_code=args.branch, account_code=args.account)
try:
console_256.print("[dark_blue on white]" + iban[0:4] + " " + iban[4:8] + " " + iban[8:12] + " " + iban[12:16] + " " + iban[16:20] + " " + iban[20:23])
except:
print("Unexpected error:", sys.exc_info()[0])
#!/usr/bin/python3.11
from argparse import ArgumentParser, RawDescriptionHelpFormatter
from sys import argv
from os.path import basename
from rich.console import Console
console_256=Console(color_system="256")
usage = "\n " + basename(argv[0])
usage = usage + " <-bnk|--bank> <bank code>"
usage = usage + " <-brn|--branch> <branch number> "
usage = usage + " <-n|--account> <account_number>"
usage = usage + "\n Example:"
usage = usage + "\n " + basename(argv[0])
usage = usage + " -bnk 10 -brn 800 -n 99999999"
usage = usage + "\n Result: \033[38;5;4m\033[48;5;7mIL62 0108 0000 0009 9999 999\033[0m"
parser = ArgumentParser( prog=argv[0], usage=usage, description = "Calculates IBAN for Israel bank accounts",
formatter_class=RawDescriptionHelpFormatter,
epilog=(' <bank code>: 2 digits' + '\n' +
' <branch number>: 2 or 3 digits' + '\n' +
' <account number>: not more than 8 digits'''))
if len(argv)==1:
parser.print_help()
exit(1)
parser.add_argument('--bank', '-bnk', required=True )
parser.add_argument('--branch', '-brn', required=True )
parser.add_argument('--account', '-n', required=True )
args=parser.parse_args()
if not args.bank.isdigit():
console_256.print("[red on cornsilk1]" + "\nERROR: bank code should contain digits only\n")
exit(-1)
if not args.branch.isdigit():
console_256.print("[red on cornsilk1]" + "\nERROR: branch code should contain digits only\n")
exit(-1)
if not args.account.isdigit():
console_256.print("[red on cornsilk1]" + "\nERROR: account number should contain digits only\n")
exit(-1)
if len(args.bank) > 2:
console_256.print("[red on cornsilk1]" + "\nERROR: bank code exceeds maximum size of 2\n")
exit(-1)
if len(args.branch) > 3:
console_256.print("[red on cornsilk1]" + "\nERROR: branch code exceeds maximum size of 3\n")
exit(-1)
if len(args.account) > 8:
console_256.print("[red on cornsilk1]" + "\nERROR: account number exceeds maximum size of 8\n")
exit(-1)
full_account_number=args.bank + args.branch.zfill(3) + args.account.zfill(13);
control_number = 98 - int(full_account_number + "182100") % 97
iban="IL" + str(control_number).zfill(2) + full_account_number.zfill(19)
console_256.print("[dark_blue on white]" + iban[0:4] + " " + iban[4:8] + " " + iban[8:12] + " " + iban[12:16] + " " + iban[16:20] + " " + iban[20:23])
#!/usr/bin/tcsh -f
setenv MYVAR "MYVAL"
set perlbin=/usr/bin/perl
if($#argv > 1 ) set perlbin=$2
@ i = 0
set args=`echo '$'$i`
while($i < $#argv)
@ i = $i + 1
set args=`echo $args '$'$i`
end
#!perl
eval "exec $perlbin -x -S $args;"
if $running_under_some_shell;
use Data::Dumper;
use Math::Trig;
printf("running %s v(%vd)\n", $^X, $^V);
print Dumper \@ARGV;
print $ENV{MYVAR},"\n";
my $sum=0;
my $n=1e6;
$#ARGV >=0 ? $n = $ARGV[0] : print "n = $n\n";
for(my $i=1; $i < $n; $i++) {$sum += 6 / $i**2; }
print "\033[38;5;4m\033[48;5;7m", sqrt($sum)/pi-1, "\033[0m\n";
#!/usr/bin/tcsh -f
#\
setenv MYVAR MYVAL
#\
exec /usr/bin/tclsh $0 $*
proc setBit {var bit} {
upvar 1 $var v
# OR
set v [expr {$v | (1 << $bit)}]
}
proc lshift {listVar {count 1}} {
upvar 1 $listVar l
if {![info exists l]} {
# make the error message show the real variable name
error "can't read \"$listVar\": no such variable"
}
if {![llength $l]} {error Empty}
set r [lrange $l 0 [incr count -1]]
set l [lreplace $l [set l 0] $count]
return $r
}
if {![llength $argv]} {
set args {120 0 1 2}
} else {
set args [split $argv]
}
set v [lshift args]
puts "MYVAR=$env(MYVAR)"
puts "\033\[38;5;4m\033\[48;5;7m$v\033\[0m"
while { [llength $args] > 0 } {
setBit v [lshift args]
puts "\033\[38;5;4m\033\[48;5;7m$v\033\[0m"
}
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment