Text-file-tree: A library for converting text file trees to native file paths

Hi.

I created a library for converting text file trees to native file paths. The file trees can be created using ASCII or Unicode text.

ASCII

home/username
|-- .config/nvim
|-- .local/bin
|   |-- helix
|   |-- nvim
|-- bin
    |-- vim
    |-- nano

Unicode

home/username
├── .config/nvim
├── .local/bin
│   ├── helix
│   └── nvim
└── bin
    ├── vim
    └── nano

The output is native file paths.

Linux and MacOS

home/username
home/username/.config/nvim
home/username/.local/bin
home/username/.local/bin/helix
home/username/.local/bin/nvim
home/username/bin
home/username/bin/vim
home/username/bin/nano 

Windows

home\username
home\username\.config\nvim
home\username\.local\bin
home\username\.local\bin\helix
home\username\.local\bin\nvim
home\username\bin
home\username\bin\vim
home\username\bin\nano 

The library is hosted on GitHub.

8 Likes

Cool. I like little libraries like this which do one very specific thing, they make great Unix-style CLI filter programs as well.

I’m curious, what prompted its creation?

Have you considered adding the dual operation, taking a list of files and producing a text tree? That would make it two very specific things, but when they’re inversions that’s a special case IMHO.

1 Like

Sweet. Is an even terser ascii version worth supporting?

1 Like

I thought about creating the library while I was working on a file converter. The converter generated multiple files from a single file. I needed to generate some files that would contain some of the generated content. Part of the file structure was known before the conversion. I didn’t want to do repetitive work.

I didn’t think about creating a dual operation because I didn’t have a use for it. But, I thought about the library being used to work on output of programs like tree. I think it would be a good idea to add that to the library. The name of the library is generic enough for that. Care to make a pull request?

The library already supports arbitrary indentations. You can add support for any kind of indentations by creating a slice containing the desired indentations. For example, to support the format in your suggestion, you would do the following:

    const SPACE_INDENTATION_LIST = &[_][]const u8{ "    " };

    const tree =
        \\home/username
        \\    .config/nvim
        \\    .local/bin
        \\        helix
        \\        nvim
        \\    bin
        \\        vim
        \\        nano
    ;

    const paths = TextFileTree.init(
        allocator,
        SPACE_INDENTATION_LIST,
        tree,
    );

I’ll add that to the library. Thank you for your suggestion.

3 Likes