#!/usr/bin/perl -w #use lib $ENV{HOME}."/pgpanel2/perl"; use Gtk2 -init; my @files=(); my %paths=(); my $fn=0; for my $f (@ARGV) { if(-f $f) { $f =~ s%[/]+$%%g; $f =~ s%//%/%g; my @parts=split(m%/%,$f); $f=pop @parts; my $dir=join("/",@parts); print STDERR "dir=$dir\n"; $paths{$fn}=$dir; push(@files,$f); $fn++; } } if(!@files) { system("pgprompt","tell","MESSAGE=No files were specified!"); exit 1; } my $win=new Gtk2::Window -toplevel; my $vbox=new Gtk2::VBox(0,0); my %w=(); my $minlen=0; my $true=1; my $false=0; #csincock::Gtk->tryloadfont("fixed"); for my $f (@files) { } for my $f (@files) { my $l=length($f); if($l && ($minlen == 0 || $l < $minlen)) { $minlen=$l; } } my $nfiles=@files; my $commonpart=""; for (my $l=$minlen;$l>=0;$l--) { my $matchedall=$true; my $first=substr($files[0],0,$l); print STDERR "first=$first\n"; for my $f (@files) { print STDERR substr($f,0,$l) . "\n"; if(!(substr($f,0,$l) eq $first)) { print STDERR "failed\n"; $matchedall=$false; last; } } if($matchedall) { print STDERR "success at first=$first\n"; $commonpart=$first; last; } } if(!$commonpart) { system("pgprompt","tell","MESSAGE=The files have no common filename prefix."); } if(length($commonpart)>0 && $commonpart =~ /^(.*)-$/) { $commonpart=$1; } my %strs=(); $fn=0; my $ln=length($commonpart); for my $f (@files) { print STDERR "f=$f, ln=$ln, str= ".substr($f,$ln)."\n"; $strs{$fn}=substr($f,$ln); $fn++; } use Data::Dumper; print ::Dumper([%strs]); sub setstr($) { my $str=shift; for my $n (keys %strs) { $w{$n}->set_text($str.$strs{$n}); } } my $text=new Gtk2::Entry(); print STDERR "***"x10;print STDERR "\n"; sub setWidgetFont($$$) { my ($widg,$fontname,$wot)=@_; my $font=Gtk2::Pango::FontDescription->from_string($fontname); if(defined($font)) { $widg->modify_font($font); } } setWidgetFont($text,"fixed",$true); print STDERR "fuck\n"; $text->set_text($commonpart); $vbox->pack_start($text,1,1,1); $fn=0; $text->signal_connect('move-cursor', sub { print STDERR "Move cursor\n"; } ); $text->signal_connect('changed', sub { &setstr($text->get_text()); } ); $text->signal_connect('activate', sub { &doit(); }); my %eb=(); for my $f (@files) { my $eb= new Gtk2::EventBox(); my $ftext=new Gtk2::Entry(); #$ftext->max_length($minlength+30); $eb->add($ftext); $eb{$ftext}=$eb; $w{$fn}=$ftext; $ftext->set_text($f); $vbox->pack_start($eb,1,1,1); $ftext->show; setWidgetFont($ftext,"fixed",$true); $ftext->set_editable($false); # $ftext->set_sensitive($false); #print STDERR "fuck2\n"; $fn++; } my $windowWidth=500; my $windowHeight=@files*20; $win->set_default_size($windowWidth,$windowHeight); &setstr($commonpart); my $buttons=new Gtk2::HBox(0,0); my $ok=new Gtk2::Button("Do it"); my $cancel=new Gtk2::Button("Cancel"); $cancel->signal_connect('clicked', sub { Gtk2->main_quit; }); $ok->signal_connect('clicked', sub { doit();}); $buttons->pack_start($cancel,1,1,1); $buttons->pack_start($ok,1,1,1); $ok->can_default($true); #$ok->can_focus($true); #$ok->request_focus(); #$ok->set_default($true); #my $hhbox=new Gtk2::HBox(1,1); $vbox->pack_start($buttons,1,1,1); $win->set_position(-center); my $hhbox=new Gtk2::HBox(1,1); $hhbox->pack_start($vbox,1,1,1); $win->add($hhbox); #$n=0; #sleep 5; for my $n (keys %strs) { my $ft=$w{$n}; if($ft) { $ft->signal_connect('realize',sub { $ft->modify_text("normal",Gtk2::Gdk::Color->parse("red")); }); } } $win->show_all; $text->can_focus($true); $text->grab_focus(); $ok->grab_default; $text->select_region(0,length($text->get_text)); Gtk2->main; sub doit() { my $str=$text->get_text(); for my $n (keys %strs) { my $path=$paths{$n} || ""; if($path) { $path.="/"; } my $newname=$str.$strs{$n}; my $orig=$path.$commonpart.$strs{$n}; my @cmd=("pgrenamefile","--",$orig,$newname); system(@cmd); } Gtk2->main_quit; }