Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit output to fit the size of the window #130

Closed
BioBox opened this issue Feb 6, 2023 · 2 comments
Closed

Limit output to fit the size of the window #130

BioBox opened this issue Feb 6, 2023 · 2 comments

Comments

@BioBox
Copy link

BioBox commented Feb 6, 2023

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
Copy link
Author

BioBox commented Feb 9, 2023

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 BioBox closed this as completed Feb 9, 2023
@BioBox
Copy link
Author

BioBox commented Feb 9, 2023

Actually an even better solution than this is clifm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant