I think I use this program more than any other. I bet the readers here can help me think of cool ways to make it better. ;) Keep in mind, that this was one of the very first programs I ever wrote in perl--so it's pretty bad. I very nearly tried this one in C.

This also used to come with a program called irun, but it was really just a 'psall | head -1; psall | grep $USER'. :)

#!/usr/bin/perl use Term::Size; $psl=`ps auxfw`; @psla=split("\n", $psl); ($width, undef) = Term::Size::chars; $Iam=getpwuid($<); foreach $l (@psla) { %clen = ( "user" => 8, "pid" => 5, "pcpu" => 4, "pmem" => 4, "size" => 5, "rss" => 5, "tty" => 5, "stat" => 3, "start" => 6, "time" => 5, ); %proc = ( "user" => substr($l, 0, $clen{"user"} ), "pid" => substr($l, 9, $clen{"pid"} ), "pcpu" => substr($l, 15, $clen{"pcpu"} ), "pmem" => substr($l, 20, $clen{"pmem"} ), "size" => substr($l, 25, $clen{"size"} ), "rss" => substr($l, 31, $clen{"rss"} ), "tty" => substr($l, 36, $clen{"tty"} ), "stat" => substr($l, 41, $clen{"stat"} ), "start" => substr($l, 45, $clen{"start"} ), "time" => substr($l, 53, $clen{"time"} ), "cmd" => substr($l, 63 ) ); $_ = $proc{"user"}; if ( /root/ ) { $usercolor = "^[[0;31m"; } elsif ( /$Iam/ ) { $usercolor = "^[[0;34m"; } else { $usercolor = "^[[0;36m"; } @out = ( $usercolor, $proc{"user"}, "^[[0;33m", $proc{"tty"}, " ", "^[[1;30m", $proc{"pid" }, " ", "^[[0;37m", $proc{"pcpu"}, " ", "^[[1;37m", $proc{"pmem"}, " ", "^[[1m", $proc{"cmd" } ); $out = substr join("",@out), 0, ($width+(7*6)-3); print $out, "\n"; } #USER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMAND #012345678901234567890123456789012345678901234567890123456789012345678 +90123456 # 1 . . 2 . .3 . .4 . 5 . 6 + 7

#shandor    986  0.0  0.0  1752    0 pts/0    SW   16:54   0:00      \_ [bash]

Replies are listed 'Best First'.
RE: psall (color ps)
by qi3ber (Scribe) on Jul 26, 2000 at 19:50 UTC
    I did some tweaking of this myself, and implemented command line wrapping, and cleaned it up a bit. Here's my version of the code.

    Update:  Ok, I did some more tweaking, and now it actually works the way that I intended it to.  There are still some changes that I would like to make to it before too much longer.

    #!/usr/bin/perl use Term::Size; $psl=`ps auxfw`; @psla=split("\n", $psl); chomp @psla; ($screen_width, undef) = Term::Size::chars; die "Screen to skinny, can't run, please make your screen at least 40 +columns wide\n" if ($screen_width<40); $Iam=getpwuid($<); foreach $l (@psla) { ( $user, $pid, $pcpu, $pmem, undef, undef, $tty, undef, undef, undef, @cmd ) = split / +/, $l; if ( $user =~ /root/ ) { $usercolor = "^[[0;31m"; } elsif ( $user =~ /$Iam/ ) { $usercolor = "^[[0;34m"; } else { $usercolor = "^[[0;36m"; } &printline( $user, $tty, $pid, $pcpu, $pmem, join( " ", @cmd ), $u +sercolor ); } sub printline { my ( $user, $tty, $pid, $pcpu, $pmem, $cmd, $usercolor ) = @_; my $ttycolor = "^[[0;33m"; my $pidcolor = "^[[1;30m"; my $pcpucolor = "^[[0;37m"; my $pmemcolor = "^[[1;37m"; my $cmdcolor = "^[[1m"; my $line_prestuff = 36; my $cmd_width = $screen_width-$line_prestuff-1; my $cont_start = $line_prestuff+2; my $cmd_cont = $cmd_width-2; $cmd =~ s/^\\/ \\/; $cmd =~ s/^\|/ |/; printf( "$usercolor%-8s $ttycolor%-5s $pidcolor%5s " . "$pcpucolor%4s $pmemcolor%4s $cmdcolor%-${cmd_width}s\n" +, $user, $tty, $pid, $pcpu, $pmem, substr($cmd, 0, $cmd_wid +th) ); $cmd = substr( $cmd, $cmd_width ); do { next unless length($cmd); printf( "%${cont_start}s$cmdcolor%-${cmd_cont}s\n", " ", subst +r($cmd, 0, $cmd_cont) ); } while ( $cmd = substr( $cmd, $cmd_const ) ); }
RE: psall (color ps)
by turnstep (Parson) on Jul 26, 2000 at 18:37 UTC
    My quick version. Not tested, as I currently have no access to a machine with Term or ps, so feel free to correct :)
    #!perl use Term::Size; ($width) = Term::Size::chars; ## Remove parens? %usercolor = ( ## Easy to change and add more! root => "^[[0;31m", scalar getpwuid($<) => "^[[0;34m", DEFAULT => "^[[0;36m", ); %color = ( ## Also easy to change tty => "^[[0;33m", pid => "^[[1;30m", pcpu => "^[[0;37m", pmem => "^[[1;37m", cmd => "^[[1m", ); for (`ps auxfw`) { ($user,$pid,$pcpu,$pmem, undef,undef,undef,undef,undef,undef,$cmd) = split(/\s+/, $_); $usercolor = $usercolor{$user} || $usercolor{'DEFAULT'}; ## Symbolic refs would work nice below too $tty = "$color{'tty'} $tty"; $pid = "$color{'pid'} $tty"; $pcpu = "$color{'pcpu'} $tty"; $pmem = "$color{'pmem'} $tty"; $cmd = "$color{'cmd'} $tty"; $out = "$usercolor $user $tty $pid $pcpu $pmem $cmd"; print substr($out, 0, ($width+(7*6)-3)); }