Last active
May 9, 2021 00:59
-
-
Save Athesto/191e6f70f1626c03f187b58d3d0da507 to your computer and use it in GitHub Desktop.
pretty dir, changes the visualization for the dir command
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/python3 | |
""" | |
pdir is a pretty print function for the dir() command | |
""" | |
from cmd import Cmd | |
from re import match | |
import os | |
__version__ = "0.0.1" | |
def pdir(data=None, opt=None): | |
''' | |
NAME: | |
pdir - a pretty print for dir() command | |
SYNOPSIS: | |
pdir(data, opt) - pretty dirk | |
ARGS: | |
@data: is any list | |
@opt: it could be None, 'public', 'privated', 'dunder'/'magic' | |
or 'protected'. it set a blue color for dunder function and | |
a light blue for protected function | |
RETURN: | |
It will print the dir command as ls in the shell | |
''' | |
re_public = r"^[^_].*" | |
re_protec = r"^[_][^_].*" | |
re_dunder = r"^[_][_].*[_][_]$" | |
re_privat = r"^[_][_].*[^_][^_]$" | |
list_dir = dir(data) | |
public, private, protected, dunder = \ | |
[x for x in list_dir if match(re_public, x)], \ | |
[x for x in list_dir if match(re_privat, x)], \ | |
[x for x in list_dir if match(re_protec, x)], \ | |
[x for x in list_dir if match(re_dunder, x)] | |
a = (list_dir) | |
b = (public + protected + private + dunder) | |
if len(a) != len(b): | |
print("regex error") | |
return | |
public = ["\033[{}m{}\033[m".format(00, x) for x in public] | |
private = ["\033[{}m{}\033[m".format(00, x) for x in private] | |
protected = ["\033[{}m{}\033[m".format(94, x) for x in protected] | |
dunder = ["\033[{}m{}\033[m".format(34, x) for x in dunder] | |
if opt == 'public': | |
out = public | |
elif opt == 'private': | |
out = private | |
elif opt == 'protected': | |
out = protected | |
elif opt in ('dunder', 'magic'): | |
out = dunder | |
else: | |
out = dunder + private + protected + public | |
rows, columns = os.popen('stty size', 'r').read().split() | |
columns = int(columns) - 2 | |
Cmd().columnize(out, displaywidth=columns) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
README
pretty dir is a function which change the print format for the dir function
Install
Usage