Skip to content

Instantly share code, notes, and snippets.

@mdmitry1
Last active May 4, 2024 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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")
#!/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 strict;
use warnings;
use Data::Dumper qw(Dumper);
use Math::Trig;
print "hello world\n"; # if $foo;
printf("running %s v(%vd)\n", $^X, $^V);
print Dumper \@ARGV;
print $ENV{MYVAR},"\n";
my $pi = acos(-1);
my $sum = 0;
my $n=$ARGV[0];
for(my $i=1; $i < $n; $i++) {
$sum += 1.0 / $i**2;
}
print sqrt($sum*6)/$pi-1,"\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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment