Jump.pm


Code Index:

#!/usr/bin/perl
# ========================================
# FILE : Jump.pm [last update: 14.08.2010]
# ========================================
# perltidy -b -gnu -l 120 Jump.pm
# ========================================


NAME

Jump - Module and utility to store and recall shortcuts to desired path


=head1 VERSION

Version 1.001


SYNOPSIS

    Jump is a module which manage a list of shortcat to the associated path,
    plus a function Start called when the module is called as standalone
    program.
    
=head1 BASIC INSTALL
   The simple install require that the files Jump.pm and jump.sh are 
   moved to the followind directory 
   
   # cp /home/enzo/workspace/perl/jump/Jump.pm  /usr/local/bin/jump
   # cp  /home/enzo/workspace/perl/jump/jump.sh /etc/profile.d/
   
   The to enable before logout just exec the jump.sh commands
   
   $ source /etc/profile.d/jump.sh
package Jump;

use 5.006;
use strict;
use warnings;
use Config::General;
use Cwd;
use Fcntl;
use Data::Dumper;

our $VERSION = '1.001';

Start() unless caller();

# ==================================================


FUNCTIONS

Helper functions ( external to module Jump )

    Start         
    PrintHelp

Module functions

    new
    LoadDBData
    SaveDBData
    AddEntry
    RemoveEntry
    GetKeyList
    GetValue
    GetDetailedList
    WritePipe
=cut
# ==================================================

sub Start

    The function 'Start' is called when the module is called directly by shell,
    and it instantiate the object Jump in order to use the module as a standalone program.

FILE jump.sh

    The standalone program is tipically called by the bash function jump
    defined in the file /etc/profile.d/jump.sh as follow:
       
     function jump
     {
        pipename="$HOME/FifoJumpChdir" 
        if [[ ! -p $pipename ]]; then mkfifo $pipename; fi 
        ( Jump.pm $*  & ) 2> /dev/null
 
        read line <$pipename
        if [[ $line != "" ]]; then cd $line; fi
        unlink $pipename
     }
    
    
=cut
# ==================================================
sub Start
{
    my ($obj, $cmd, $key, $path, $msg, $target, $lstkey, $mask );
    my ($msgToPipe) = '';

    $cmd  = $ARGV[0] || 'list';
    $key  = $ARGV[1];
    $path = $ARGV[2];

    $obj = new Jump();

    if ($cmd eq "list")
    {
		$mask = $key;
        $msg = $obj->GetDetailedList( $mask );
        print "$msg \n";
    }
    elsif ($cmd eq "add" && defined $key)
    {
        if (!defined $path) { $path = getcwd; }
        $obj->AddEntry($key, $path);
        $obj->SaveDBData();
    }
    elsif ($cmd eq "remove" && defined $key)
    {
        $obj->RemoveEntry($key);
        $obj->SaveDBData();
    }
    elsif ($cmd eq "goto" && defined $key)
    {
        $target = $obj->GetValue($key);
        if (defined $target)
        {
            my ($pwd1, $pwd2);
            my ($pipeName) = $obj->{self}->{namedPipe};
            $pwd1 = cwd();
            print "[$key] Change dir [$pwd1] => [$target]\n";
            $msgToPipe = "$target";

        }
        else    # show possible choice
        {
            $lstkey = $obj->GetKeyList($key);
            print "\n Possible choice: $lstkey \n";
        }
    }
    elsif ($cmd eq "help")
    {
        PrintHelp(1);
    }
    else
    {
        PrintHelp(1);
    }

    $obj->WritePipe($msgToPipe);

}    ##___________ sub Start

# ==================================================

sub PrintHelp

    The function PrintHelp is not part of the module, and is used only 
    to print the help of the function managed bu function Start
=cut
# ==================================================
sub PrintHelp
{
    my ($print) = @_;
    my $msg = <<END;
            USE:
              jump add myhome [\$HOME]  
              jump remove myhome
              jump list [mask]
              jump goto work.c++
END
    if (defined $print && $print == 1) { print "$msg \n"; }
    
    return $msg;

}

# ==================================================
# ==================================================
sub new
{
    my $class = shift;
    my ($myhome) = "$ENV{HOME}";
    my $self = {
                filenameDb => "$myhome\/\.jumpDB",
                dbData     => {},
                namedPipe  => "$myhome\/FifoJumpChdir",
                version    => "$VERSION"
               };
    bless $self, $class;
    if (!-f $self->{filenameDb}) { $self->SaveDBData(); }
    $self->LoadDBData();
    $self->AddEntry('home', $ENV{HOME});
    return $self;
}    ##  ________ sub new

# ==================================================
sub LoadDBData
{
    my ($self) = @_;
    my ($conf, $htmp);
    if (-f $self->{filenameDb})
    {
        $conf           = new Config::General($self->{filenameDb});
        %$htmp          = $conf->getall;
        $self->{dbData} = $htmp;
    }

}    ## ___________ sub LoadDBData

# ==================================================
sub SaveDBData
{
    my ($self) = @_;
    my ($conf);
    $self->{dbData}->{lastSaved} = localtime(time);
    $conf = new Config::General();
    $conf->save_file($self->{filenameDb}, $self->{dbData});

}    ## ___________ sub SaveDBData

# ==================================================
#  $self->AddEntry( 'home', $ENV{HOME} );
# ==================================================
sub AddEntry
{
    my ($self, $key, $value) = @_;
    my ($oldval);
    $key = lc($key);
    $oldval = $self->{dbData}->{links}->{$key} || '';
    if ($oldval ne "$value")
    {
        printf "Added new entry to jump: %15s => %s \n", $key, $value;
        $self->{dbData}->{links}->{$key} = "$value";
    }

}    ## _________  sub AddEntry

# ==================================================
#  $self->RemoveEntry( 'home' );
# ==================================================
sub RemoveEntry
{
    my ($self, $key) = @_;
    $key = lc($key);
    delete $self->{dbData}->{links}->{$key};

}    ## _______  sub RemoveEntry

# ==================================================
sub GetKeyList
{
    my ($self, $mask) = @_;
    my ($k, $hash, $sres);
    $sres = "";
    $hash = $self->{dbData}->{links};
    foreach $k (sort keys %$hash)
    {
        if (defined $mask && $k !~ /$mask/) { next; }
        $sres .= sprintf " %s ", $k;
    }
    return $sres;
}    ## _________ sub GetKeyList

# ==================================================
sub GetValue
{
    my ($self, $k) = @_;
    my $sres = $self->{dbData}->{links}->{$k};
    return $sres;
}    ## _________ sub GetValue

# ==================================================
sub GetDetailedList
{
    my ($self, $mask) = @_;
    my ($k, $v, $hash, $sres);
    $sres = "Shortcut list: \n";
    $hash = $self->{dbData}->{links};
    foreach $k (sort keys %$hash)
    {
		$v = $self->{dbData}->{links}->{$k};
        if (defined $mask &&  $k !~ /$mask/ && $v !~ /$mask/ ) { next; }
        $sres .= sprintf "%12s : %s \n", $k, $v;
    }

    return $sres;
}    ## _________ sub GetDetailedList

# ==================================================

sub WritePipe

    $obj->WritePipe( $msg );
    Send the message to the named pipe in order
    to communicate with the parent shell
# ==================================================
sub WritePipe
{
    my ($self, $msg) = @_;
    my ($pipeName) = $self->{namedPipe};

    if ( ! -p $pipeName )
    {
		print "Pipe $pipeName NOT FOUND [$!] \n"; 
		return;
	}
    sysopen(FIFO, $pipeName, O_WRONLY) or warn "can't write $pipeName $!";
    print FIFO "$msg\n";
    close FIFO;
    # select(undef, undef, undef, 0.2);

}    ## ________ sub WritePipe


# =======================================================
# =======================================================



EXAMPLES

     [enzo@enzo6 jump]$ ./Jump.pm list
     Shortcut list: 
             home : /home/enzo 
      
     [enzo@enzo6 jump]$ ./Jump.pm  help 
                 USE:
                   jump add myhome $HOME  
                   jump remove myhome
                   jump list
      
     [enzo@enzo6 jump]$ ./Jump.pm  add jump 
     Added new entry to jump:            jump => /home/enzo/workspace/perl/jump 
     
     [enzo@enzo6 jump]$ ./Jump.pm  remove jump 
     
     [enzo@enzo6 jump]$ ./Jump.pm  add jump 
     Added new entry to jump:            jump => /home/enzo/workspace/perl/jump 
     
     [enzo@enzo6 jump]$ ./Jump.pm  list
     Shortcut list: 
             jump : /home/enzo/workspace/perl/jump 
             home : /home/enzo 
      
     [enzo@enzo6 jump]$ ./Jump.pm  add work /home/enzo/workspace
     Added new entry to jump:            work => /home/enzo/workspace 
     
     [enzo@enzo6 jump]$ ./Jump.pm  list
     Shortcut list: 
             jump : /home/enzo/workspace/perl/jump 
             work : /home/enzo/workspace 
             home : /home/enzo
     [enzo@enzo6 jump]$ ./Jump.pm  list workspace
     Shortcut list: 
             jump : /home/enzo/workspace/perl/jump 
             work : /home/enzo/workspace


AUTHOR

Enzo Arlati, <enzo.arlati at libero.it>


BUGS

Please report any bugs or feature requests to bug-jump at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Jump

You can also look for information at:


ACKNOWLEDGEMENTS


COPYRIGHT & LICENSE

Copyright 2010 Enzo Arlati.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.


1;