user search now works

git-svn-id: svn://127.0.0.1/Perl/Proc-ProcessTable-Colorizer/trunk@966 0c1c3402-1be1-de11-8092-0022686faf23
This commit is contained in:
Zane C. B-H 2017-10-16 05:21:41 +00:00
parent f721e832af
commit 82e36d1752
2 changed files with 151 additions and 29 deletions

View File

@ -41,6 +41,10 @@ getopts('p:u:', \%opts);
use Proc::ProcessTable::Colorizer;
my $cps = Proc::ProcessTable::Colorizer->new;
$cps->searchSet( $opts{p} );
$cps->userSet( $opts{p} );
$cps->procSearchSet( $opts{p} );
if ( defined( $opts{u} ) ){
$cps->userSearchSetString( $opts{u} );
}
print $cps->colorize;

View File

@ -52,6 +52,7 @@ sub new {
errorString=>'',
errorExtra=>{
flags=>{
1=>'noUserStringDefined',
},
},
colors=>[
@ -86,7 +87,7 @@ sub new {
nextColor=>0,
showIdle=>0,
proc_search=>undef,
user_search=>undef,
user_search=>[],
self_ignore=>2,
};
bless $self;
@ -205,6 +206,7 @@ sub colorize{
if ($^O =~ /bsd/){
my $bproc=BSD::Process::info( $proc->pid );
$kernel_proc=$bproc->{kthread};
}
#need to find something similar as above for Linux
@ -220,7 +222,11 @@ sub colorize{
$values{'proc'}=$fname;
}
}else{
$values{'proc'}=$cmndline;
if ( $cmndline =~ /^su *$/ ){
$values{'proc'}=$cmndline.'('.$fname.')';
}else{
$values{'proc'}=$cmndline;
}
}
}elsif(
$field eq 'info'
@ -311,19 +317,60 @@ sub colorize{
}
}
#checks to see if it should ignore its self
my $self_ignore=$self->{self_ignore};
if (
#if it is set to 1
( $self_ignore == 1 ) &&
( $proc->{pid} == $$ )
){
$hits++;
$required_hits++;
}elsif(
#if it is set to 2... we only care if we are doing a search...
#meaning required hits are greater than zero
( $required_hits > 0 ) &&
( $self_ignore == 2 ) &&
( $proc->{pid} == $$ )
){
$hits++;
#increment this so it will always be off by one for this proc, meaning it is ignored
$required_hits++;
}
#check to see if it needs to search for users
my $user_search_array=$self->userSearchGet;
if ( defined( $user_search_array->[0] ) ){
my $user=getpwuid($proc->{uid});
$required_hits++;
my $user_search_int=0;
my $matched=0;
#search while we have a user defined and it has not already been matched
while(
defined( $user_search_array->[ $user_search_int ] ) &&
( $matched == 0 )
){
my $to_match=$user_search_array->[ $user_search_int ];
my $to_invert=0;
if ( $to_match=~ /^\!/ ){
$to_invert=1;
$to_match=~s/^\!//;
}
#check if it matches
if ( $to_invert ){
if ( $to_match ne $user ){
$hits++;
$matched=1;
}
}else{
if ( $to_match eq $user ){
$hits++;
$matched=1;
}
}
$user_search_int++;
}
}
if ( $required_hits == $hits ){
@ -598,14 +645,14 @@ sub processColorGet{
return $self->{processColor};
}
=head2 searchGet
=head2 procSearchGet
This returns the search string value that will be used
for matching the proc column.
The return is undefined if one is not set.
my $search_regex=$cps->searchGet;
my $search_regex=$cps->procSearchGet;
if ( defined( $search_regex ) ){
print "search regex: ".$search_regex."\n";
}else{
@ -614,31 +661,31 @@ The return is undefined if one is not set.
=cut
sub searchGet{
sub procSearchGet{
my $self=$_[0];
$self->errorblank;
return $self->{proc_search};
}
=head2 searchSet
=head2 procSearchSet
This sets the proc column search regex to use.
If set to undef(the default), then it will show all procs.
#shows everything
$cps->searchSet( undef );
$cps->procSearchSet( undef );
#search for only those matching musicpd
$cps->seearchSet( 'musicpd' );
$cps->procSeearchSet( 'musicpd' );
#search for those that match /[Zz]whatever/
$cps->searchSet( '[Zz]whatever' );
$cps->procSearchSet( '[Zz]whatever' );
=cut
sub searchSet{
sub procSearchSet{
my $self=$_[0];
my $proc_search=$_[1];
$self->errorblank;
@ -648,19 +695,7 @@ sub searchSet{
return 1;
}
=head2 searchGet
This returns the search string value that will be used
for matching the proc column.
The return is undefined if one is not set.
my $search_regex=$cps->searchGet;
if ( defined( $search_regex ) ){
print "search regex: ".$search_regex."\n";
}else{
print "No search regex.\n";
}
=head2 selfIgnoreGet
=cut
@ -671,9 +706,27 @@ sub selfIgnoreGet{
return $self->{self_ignore};
}
=head2 searchSet
=head2 selfIgnoreSet
Wether or not to show the PID of this processes in the list.
=head3 undef
Resets it to the default, 2.
=head3 0
Always show self PID in the list.
=head3 1
Never show self PID in the list.
=head3 2
Don't show self PID if it is a search.
This is the default.
=cut
@ -682,6 +735,10 @@ sub selfIgnoreSet{
my $self_ignore=$_[1];
$self->errorblank;
if ( ! defined( $self_ignore ) ){
$self_ignore='2';
}
$self->{self_ignore}=$self_ignore;
return 1;
@ -790,6 +847,62 @@ sub timeString{
return $toReturn;
}
=head1 userSearchGet
This gets the user to be searched for and if it should be inverted or not.
This returns an array reference of users to search for.
An selection can be inverted via !.
my $user_search=$cps->userSearchGet;
=cut
sub userSearchGet{
my $self=$_[0];
$self->errorblank;
return $self->{user_search};
}
=head1 userSearchSetString
This gets the user to be searched for and if it should be inverted or not.
This returns an array reference of users to search for.
An selection can be inverted via !.
The string may contain multiple users seperated by a comma.
#search for user foo and bar
$cps->userSearchSetString('foo,bar');
#show users not matching foo
$cps->userSearchSetString('!foo');
=cut
sub userSearchSetString{
my $self=$_[0];
my $user_search_string=$_[1];
$self->errorblank;
if ( ! defined( $user_search_string ) ){
$self->{errorStirng}=1;
$self->{error}='No user search string defined.';
$self->warn;
return undef;
}
my @user_search_array=split(/\,/, $user_search_string);
$self->{user_search}=\@user_search_array;
return 1;
}
=head1 COLORS
These corresponds to L<Term::ANSIColor> colors.
@ -816,6 +929,11 @@ The default is as below.
BRIGHT_MAGENTA
BRIGHT_BLUE
=head1 ERROR CODES/FLAGS
=head2 1 / noUserStringDefined
No string string of users to search for defined.
=head1 AUTHOR