Skip to content

Instantly share code, notes, and snippets.

@Athesto
Last active May 9, 2021 00:59
Show Gist options
  • Save Athesto/191e6f70f1626c03f187b58d3d0da507 to your computer and use it in GitHub Desktop.
Save Athesto/191e6f70f1626c03f187b58d3d0da507 to your computer and use it in GitHub Desktop.
pretty dir, changes the visualization for the dir command
#!/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)
@Athesto
Copy link
Author

Athesto commented May 7, 2021

README

pretty dir

pretty dir is a function which change the print format for the dir function

Install

foo@bar~:$ wget https://gist.github.com/Athesto/191e6f70f1626c03f187b58d3d0da507/raw/pdir.py
...

Usage

>>> from pdir import pdir
>>> pdir(set)
add                  isdisjoint                   update
clear                issubset
copy                 issuperset
difference           pop
difference_update    remove
discard              symmetric_difference
intersection         symmetric_difference_update
intersection_update  union
>>>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment