Skip to content

Commit

Permalink
some error catching & timout
Browse files Browse the repository at this point in the history
  • Loading branch information
mfiers committed Apr 4, 2011
1 parent ade5aa2 commit 708f6dc
Showing 1 changed file with 57 additions and 39 deletions.
96 changes: 57 additions & 39 deletions bin/moa_prompt
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
#!/bin/bash

function _moa_prompt_2 {
function _moa_prompt_3 {


#Get the last command
#based on:
#http://stackoverflow.com/questions/945288/...
# ...saving-current-directory-to-bash-history
#
local lc=$(history 1)
lc="${lc# *[0-9]* }"

#save the last command to a user specific location
echo $lc > ~/.moa.last.command

[[ -d '.moa' ]] || return 0
#and add it to the local history if this is a mob job
echo $lc >> .moa/history

#if there is no template, there is not much we can do
[[ -f '.moa/template' ]] || return 0

#prepare for visualization
local blue="\033[34m"
local black="\033[30m"
Expand All @@ -29,6 +10,7 @@ function _moa_prompt_2 {
local red="\033[31m"
local magenta="\033[35m"
local green="\033[32m"
local bg="\033[47m"
local reset="\033[0m"

#print an empty line
Expand All @@ -37,20 +19,20 @@ function _moa_prompt_2 {
#local line="${bg}${ELINE}\033[1K\033[0G"

#get status information
[[ -f '.moa/status' ]] && status=$(< .moa/status)
case "$status" in
error) status="${red}Error${reset}"
[[ -f '.moa/status' ]] && rawstatus=$(< .moa/status)
case "$rawstatus" in
error) status="${red}Error${gray}"
;;
success) status="${gray}Success${reset}"
success) status="${magenta}Success${gray}"
;;
interrupted) status="${black}Interrupted${reset}"
interrupted) status="${black}Interrupted${gray}"
;;
*) status="?"
*) status=$rawstatus
esac

#get template information
template=`(grep moa_id .moa/template | cut -c9-) 2>/dev/null`

#get last run dates
if [[ -f '.moa/log' ]]; then
ll=`tail -1 .moa/log`
Expand All @@ -67,30 +49,66 @@ function _moa_prompt_2 {
minutes=$((tdiff % day_secs % hour_secs / minute_secs))
seconds=$((tdiff % day_secs % hour_secs % minute_secs))
# pretty-print
timing=" on: ${pstart} taking"
timing=":${pstart} ("
if [[ ${days} > 0 ]]; then
timing="$timing ${days}d, ${hours}h, ${minutes}m and ${seconds}s."
timing="${timing}${days}d, ${hours}h, ${minutes}m & ${seconds}s)"
elif [[ ${hours} > 0 ]]; then
timing="$timing ${hours}h, ${minutes}m and ${seconds}s."
timing="${timing}${hours}h, ${minutes}m & ${seconds}s)"
elif [[ ${minutes} > 0 ]]; then
timing="$timing ${minutes}m and ${seconds}s. "
timing="${timing}${minutes}m & ${seconds}s)"
else
timing="$timing ${seconds}s."
timing="${timing}${seconds}s)"
fi
fi
echo -e "${gray}...Moa/${blue}${template}${gray}. ${status}${gray}${timing}${reset}"
txtbit=":moa/${template}:$rawstatus$timing"
frmbit="${gray}:moa/${blue}${template}${gray}:$status$timing${reset}"
echo -en $bg
printf "%$(($COLUMNS-${#txtbit}))s" " "
echo -e ${frmbit}
}

function _moa_prompt {
function _moa_prompt_2 {

#Get the last command
#based on:
#http://stackoverflow.com/questions/945288/...
# ...saving-current-directory-to-bash-history
#
local lc=$(history 1)
lc="${lc# *[0-9]* }"

#save the last command to a user specific location
echo $lc > ~/.moa.last.command

[[ -d '.moa' ]] || return 0

#and add it to the local history if this is a mob job
echo $lc >> .moa/history

#if there is no template, there is not much we can do
[[ -f '.moa/template' ]] || return 0


# two step procedure - here we define a trap & call
# the second function..
function errex() {
return 0
function exxx() {
echo -n 'xx'
return 0
}
trap "errex" ERR
trap "exxx" ERR SIGUSR1

_moa_prompt_2 2>/dev/null
_moa_prompt_3 &
PID=$!

{ ( sleep 1; kill -SIGUSR1 $PID) & } 2>/dev/null
SPID=$!
disown $SPID

trap - ERR
wait $PID
kill $SPID 2>/dev/null
trap - ERR SIGUSR1
}

function _moa_prompt {
_moa_prompt_2 2>/dev/null
}

0 comments on commit 708f6dc

Please sign in to comment.