dduan / tre

Tree command, improved.
MIT License
732 stars 15 forks source link

Limit output to fit the size of the window #130

Closed BioBox closed 1 year ago

BioBox commented 1 year ago

I've always wanted an option to the tree command that limits the output to fit in the screen, sort of to give an overview of the hierarchy without needing to use a pager. This is essentially an optimization form of the subset sum problem, which can be solved in linear time complexity under these restrictions.

What do you think? Would a feature like this be acceptable?

In the meantime I'll use the --filelimit=N option for the tree command, as that can achieve what I want nine times out of ten.

BioBox commented 1 year ago

Never-mind. I wrote a shell script that does what I want, which is to give an overview of the directory structure that fits on the screen.

This will not work in cases where the number of directories on a given depth are greater than the screen height, but that rarely ever happens so this is good enough for 95% of cases.

function itree --description 'Interactive tree viewer'
    function path_join
    echo (string join / $argv[1] (realpath --relative-to=$argv[1] $argv[2]))
    end

    set select (tree -nd $argv[1] | sed -e '1d' -e '$d' | fzy | awk '{print $NF}')
    set next (path_join $argv[1] $select)

    # If user selects a deep directory then fill in the missing components
    if [ ! -d $next ]
    set select (fd --prune -t d -g "**/"(basename $next) -1 $argv[1])
    end

    set next (path_join $argv[1] $select)
    set --global tre (realpath $next)

    # Is this the bottom?
    if [ (tree -nd $select | tail -n 1 | cut -d ' ' -f 1) -eq 0 ]
    read -P "It goes no deeper. What do now? (p/l) " response
    switch $response
        case p
        echo (realpath $next)
        case l
        ls $next
    end
    return 0
    end

    read -P "What do? (p/t/l/c) " response
    switch $response
    case p
        echo (realpath $next)
    case t
        tree $next
    case c
        itree $next
    case l
        ls $next
    end
end

I should change fd to find but the former is so much better that I can't be bothered.

BioBox commented 1 year ago

Actually an even better solution than this is clifm.