#!/bin/bash

### Curby's GeekTool process list script
### by Michael 'Curby' Lee
### v1.01.00, 2008-09-10

###   Description
#
#  Outputs a list of the most resource-consuming processes.  For CPU output, it
#  displays the process name, process ID, current processor usage, and absolute
#  processor time used.  For memory output, it displays the process name,
#  process ID, and current memory usage both as a percentage of physical memory
#  and as an absolute number of Megabytes.
#

###   Suggested use
#
#  This script can be executed from the OS X commandline without administrative
#  privileges, but it is intended for use with GeekTool.  Suggested options for
#  GeekTool are a refresh time of 10 seconds, a 9-point Monaco font, a 238x195
#  window for CPU output and a 220x195 window for memory output.  Turn off the
#  "Force carriage return" option.  The number of processes displayed can be
#  customized below.
#

###   License
#
#  Copyright (C) 2007  Michael Lee <kirbysdl@hotmail.com>
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#



#-------------------------------------------------------------------------------
#  Configuration
#-------------------------------------------------------------------------------
rows=15



#-------------------------------------------------------------------------------
#  Output
#-------------------------------------------------------------------------------
if [[ "z$1" == "zcpu" ]]; then
  ps -c -r -ax -o command,pid,pcpu,time | sed 's/\(PID *\)%/\1 %/' | head -n $(($rows+1))
elif [[ "z$1" == "zmem" ]]; then
  ps -c -m -ax -o command,pid,pmem,rss=RSIZE | sed 's/\(.\{23\}\)/\1 /' | head -n $(($rows+1))
else
  echo "Usage: $0 type"
  echo 'where type is either "cpu" or "mem"'
fi
