Example usage

To use dirlisting in a project:

Imports

from dirlisting.dirlisting import Dirlisting

A directory listing from a string

input = """
- topdir:
  - subdir1:
    - file1.txt
    - file2.txt
  - subdir2:
    - file3.txt
    - file4.txt
"""

Dirlisting(input).print()
topdir
├── subdir1
│   ├── file1.txt
│   └── file2.txt
└── subdir2
    ├── file3.txt
    └── file4.txt

A directory listing from a file

The file has the same contents as the previous example.

with open("examples/input.yaml", "r") as f:
    listing = Dirlisting(f)
listing.print()
topdir
├── subdir1
│   ├── file1.txt
│   └── file2.txt
└── subdir2
    ├── file3.txt
    └── file4.txt

A sorted directory listing

The contents can easily be sorted by name.

input = """
- topdir:
  - gfile.txt
  - kdir:
    - mfile.txt
    - cfile.txt
  - bdir:
    - afile.txt
    - bfile.txt
"""

Dirlisting(input).print(is_sort=True)
topdir
├── bdir
│   ├── afile.txt
│   └── bfile.txt
├── gfile.txt
└── kdir
    ├── cfile.txt
    └── mfile.txt

A directories first listing

The contents can be listed with directories listed before files. It keeps the basic order of entries and just floats the directories to the top. If sorting is required, is_sort and is_dirsfirst can be used together.

input = """
- topdir:
  - gfile.txt
  - kdir:
    - mfile.txt
    - cfile.txt
  - bdir:
    - afile.txt
    - bfile.txt
  - afile.txt
"""

listing = Dirlisting(input)
listing.print(is_dirsfirst=True)
listing.print(is_sort=True, is_dirsfirst=True)
topdir
├── kdir
│   ├── mfile.txt
│   └── cfile.txt
├── bdir
│   ├── afile.txt
│   └── bfile.txt
├── gfile.txt
└── afile.txt
topdir
├── bdir
│   ├── afile.txt
│   └── bfile.txt
├── kdir
│   ├── cfile.txt
│   └── mfile.txt
├── afile.txt
└── gfile.txt