as-tree icon indicating copy to clipboard operation
as-tree copied to clipboard

Option to group directories last

Open jzinn opened this issue 3 years ago • 3 comments

When finding paths with find and fd, I often pipe the results into a script I wrote called pathsort that groups files before directories.

Piping the output of pathsort into as-tree has no effect, as as-tree sorts its input.

It would be nice if as-tree had an option to group files before directories.

Gnu ls has the --group-directories-first option. tree has the --dirsfirst option. A long flag like those would be fine, but a one letter short flag would be ideal. Neither has an option to group files first.

For list output, I can see both groupings to be useful, but the existing tools only offer directories first.

For find and tree output, for some reason grouping files before directories makes more sense to me. I'm not sure why. Maybe it's because once you've seen the files in the directory, you can be done thinking about that directory for the remainder of the output.

This is my pathsort script:

#!/bin/sh
set -euo pipefail

# 1.  Replace the last `/` with `/ /`
# 2.  Sort
# 3.  Replace the `/ /` with `/`
pathsort()
{
        sed -E 's/(.*)\//\1\/ \//' | sort | sed -E 's/(.*)\/ \//\1\//'
}

case "${1-}" in
        -fd )
                # It would be nice if `fd` had an option to prepend `./` to all relative paths,
                # so that all paths have at least one `/` in them.
                sed 's/^/.\//' | pathsort
                ;;
        * )
                pathsort
                ;;
esac

Off topic

I have this in my shell's rc file:

fdtree() { fd . -H -E .git "$@" | as-tree ; }

I've been using fdtree more than tree lately because I can write fdtree -E aaa -E bbb to exclude using multiple patterns, while tree can only have one -I ignore pattern.

Thanks for as-tree. I love it!

jzinn avatar Nov 19 '21 06:11 jzinn

I just noticed there's a request for directories first: https://github.com/jez/as-tree/issues/9

jzinn avatar Nov 19 '21 07:11 jzinn

Thanks. I mostly don't work on this project anymore, because tree has a --fromfile option built in that behaves just as well or better than as-tree. You can use tree --dirsfirst --fromfile instead.

jez avatar Nov 19 '21 08:11 jez

Woah! I did not know about tree --fromfile. Thanks for that!

jzinn avatar Nov 19 '21 08:11 jzinn