| 1 |
""" |
| 2 |
A very simple `ls` clone. |
| 3 |
|
| 4 |
If your terminal supports hyperlinks you should be able to launch files by clicking the filename |
| 5 |
(usually with cmd / ctrl). |
| 6 |
|
| 7 |
""" |
| 8 |
|
| 9 |
import os |
| 10 |
import sys |
| 11 |
|
| 12 |
from rich import print |
| 13 |
from rich.columns import Columns |
| 14 |
from rich.text import Text |
| 15 |
|
| 16 |
try: |
| 17 |
root_path = sys.argv[1] |
| 18 |
except IndexError: |
| 19 |
print("Usage: python listdir.py DIRECTORY") |
| 20 |
else: |
| 21 |
|
| 22 |
def make_filename_text(filename): |
| 23 |
path = os.path.abspath(os.path.join(root_path, filename)) |
| 24 |
text = Text(filename, style="bold blue" if os.path.isdir(path) else "default") |
| 25 |
text.stylize(f"link file://{path}") |
| 26 |
text.highlight_regex(r"\..*?$", "bold") |
| 27 |
return text |
| 28 |
|
| 29 |
filenames = [ |
| 30 |
filename for filename in os.listdir(root_path) if not filename.startswith(".") |
| 31 |
] |
| 32 |
filenames.sort(key=lambda filename: filename.lower()) |
| 33 |
filename_text = [make_filename_text(filename) for filename in filenames] |
| 34 |
columns = Columns(filename_text, equal=True, column_first=True) |
| 35 |
print(columns) |