A CLI DIRectory MANagement tool written in Python 3.
The objective is to provide a simple and intuitive command line interface, similar to a bash shell.
The prompt_toolkit library is used to manage the CLI experience, with features like history and selection through up/down arrows.
For visualization, the library rich was used to render the tree and drawing tables.
Commands strings are processed using an handler function, which given a function mapping, will call the function with the supplied arguments.
The internal data structure used to represent directories and files is an N-ary tree. Nodes (see dirman/tree/TreeNode.py
) can have multiple children, which, in turn, also can have many children.
Nodes are added recursively from the root, following the directory path by traversing the tree.
The motivation to use a tree structure is to have a convenient way of storing and navigating the directories to access files. Tree are common and well known in computer science, allowing us to use algorithms like depth-first search (DFS) or breadth-first search (BFS). Another advantage is the ability to reuse nodes for nested directories.
An additional Trie data structure stores a copy of all file names. The main purpose of the Trie is to quickly filter files by their name, avoiding to expand the search to the entire directory tree. In fact, the search runtime complexity of the Trie is
-
Adding directories or files:
# Adding a directory > add dataset # Adding a file > add dataset/file.txt
-
View:
> view
-
View with sort:
- -sort_by: can sort by name, time, or size.
- -r: add this option to reverse the sort.
> view -sort_by name -r
-
View directory:
> view dataset
-
Filter:
- --prefix: filter the files by prefix name (i.e.
test*
). - --type: filter the files by type (image, video, audio, text, application).
- -gt: greater than n bytes.
- -lt: less than n bytes.
> filter dataset --prefix test --type text -gt 256 -lt 16384
- --prefix: filter the files by prefix name (i.e.
-
delete:
can delete files or directories.
delete dataset
Thanks to prompt_toolkit, QOL features such as command history, and clear display are easily implemented.
-
clear: clears the terminal.
> clear
-
history: shows the command history.
> history
-
Rerun old command: the following command will rerun the command number 8 (as shown with
history
).> ! 8
-
help: shows the list of available commands.
> help
It is recommended to create a virtual environment first.
Install the setup.py:
(venv) $ pip install -e .
Once installed, you can run dirman as CLI tool:
(venv) $ dirmancli
Unit testing is implemented using the pytest library. To run the tests:
(venv) $ python -m pytest tests
- Folder size does not update when deleting subfolders.
- Sorting only works running
view
with no arguments. - Incorrect error message when running
filter
with no results. - Empty folders are shown in tree view when files are filtered out.