#!/usr/bin/perl use MIME::Types; # dirlist.pl by Dave Foster # Openbox pipe menu that shows the contents of a directory, recursively. # REQUIRES MIME::Types module. It's available in portage, albiet ~masked: dev-perl/MIME-Types # Changelog - # 10 Oct 2004 - Version 1.3 (Heading fixed, added MIME Type support, bug fix for "/") # 9 Oct 2004 - Version 1.2 (Fixed & handling) # Version 1.1 (Added ability to ignore files/dirs starting with .) # Version 1.0 (Initial Release) # Future plans - # have a limiter for the number of files it would display. # install instructions: # 1) Put script somewhere (~/.config/openbox/scripts/ works well) # 2) Make script executable (chmod +x dirlist.pl) # 3) Edit openbox menu file: # a) Add # b) Add new; @mimeHandlers = (); @mimeRangeHandlers = (); # ########################### # set your mime handlers here # ########################### # to add an action for a specific mime type: # add_mime_handler("MIME-TYPE", "ACTION", run in terminal (1) ); # # In the action, a %1 indicates the filename. # You can leave the last argument off if you don't want it to run in a terminal. # If you want to run the file (say for application/* types), just put "%1" as the action. # # To cover all of a range of MIMEs, use this notation: type/* # Example: add_mime_handler("text/*", "gedit %1"); # Specific defined MIMEs take precedence over ranged defined types. add_mime_handler("text/*", "gedit %1"); add_mime_handler("text/comma-separated-values", "gnumeric %1"); add_mime_handler("application/x-perl", "gvim %1"); add_mime_handler("application/x-php", "gvim %1"); add_mime_handler("image/*", "feh %1"); add_mime_handler("video/*", "mplayer %1", 1); # ########################## # / set mime handlers # ########################## # # begin script # if ( scalar(@ARGV) != 1 ) { print "\n"; print "\n"; print "\t\n"; print "\t\ttrue\n"; print "\t\n"; print "\n"; exit; } print "\n"; print "\n"; $dir = $ARGV[0]; opendir DIR, $dir; @files = readdir DIR; closedir DIR; @dirs = (); @keepfiles = (); foreach $file ( @files ) { next if ( $file eq "." || $file eq ".." ); next if ( substr($file,0,1) eq "." && $ignoredotfiles); $absolute = $dir; $absolute = $absolute . "/" if ( substr($absolute,-1) ne "/" ); $absolute = $absolute . $file; if ( -d $absolute ) { push @dirs, $absolute; } elsif ( -f $absolute ) { push @keepfiles, $file; } } @dirs = sort(@dirs); @keepfiles = sort(@keepfiles); # print header @chunks = split /\//, $dir; $last = ( $dir eq "/" ) ? "/" : $chunks[$#chunks]; print "\t\n"; print "\t\t$diraction " . string_protect($dir) . "\n"; print "\t\n"; print "\t\n"; foreach ( @dirs ) { script_link($_); } # send a list to print files $absolute = $dir; $absolute = $absolute . "/" if ( substr($absolute,-1) ne "/" ); files_menu($absolute,@keepfiles); print "\n"; sub files_menu { $rootdir = shift; foreach $file ( @_ ) { $abs = $rootdir . $file; @finfo = stat($abs); $sizeout = ( $dofilesize == 1 ) ? $sizeout = sprintf(" (%.1f kb)", $finfo[7] / 1024) : ""; $actiontotake = get_action($abs); $file = string_protect($file); print "\t\n"; print "\t\t" . string_protect($actiontotake) . "\n"; print "\t\n"; } } sub script_link { my $name = shift; $sname = $name; $sname =~ s/[^A-Za-z0-9]/_/g; @chunks = split /\//, $name; $last = string_protect($chunks[$#chunks]); print "\t\n"; } sub string_protect { my $str = shift; $str =~ s/&/&/g; return $str; } # # funcs for mime type stuff! # sub add_mime_handler { $mtype = shift; $action = shift; $interm = ( scalar(@_)>0 ) ? shift : 0; # disgusting... check for the *, add appropriately. if ( $mtype =~ /(\w+)\/\*$/ ) { push @mimeDefaultHandlers, { "mime-type"=>$1, "action"=>$action, "interm"=>$interm }; } else { push @mimeHandlers, { "mime-type"=>$mtype, "action"=>$action, "interm"=>$interm }; } } sub get_action { $file = shift; $retdefault = $defaultAction; $retdefault =~ s/%1/$file/; my MIME::Type $mime = $types->mimeTypeOf($file); return "$retdefault" if ( !defined $mime ); # find mime type in our handler array first foreach $handler ( @mimeHandlers ) { if ( $handler->{"mime-type"} eq "$mime" ) { # replace %1 $action = $handler->{"action"}; $action =~ s/%1/$file/; return ( $handler->{"interm"} == 1 ) ? "$termCommand $action" : $action; } } # if we get here, we didn't find it, so search to see if it matches the first ($major,$minor) = split /\//, "$mime"; foreach $handler ( @mimeDefaultHandlers ) { if ( $handler->{"mime-type"} eq $major ) { # replace %1 $action = $handler->{"action"}; $action =~ s/%1/$file/; return ( $handler->{"interm"} == 1 ) ? "$termCommand $action" : $action; } } # okay if we get here, just get out of here return $retdefault; }