Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a command (phpunit) which has a colored output. According to the watch, command I should be able to use the --color flag to allow color rendering to pass through. However, this isn't working. Is there any other ways of solving this?

share|improve this question
4  
Are you sure that is not the command that do not output colors when stdout is not a terminal? Try phpunit | cat –  enzotib Mar 5 '12 at 15:05
1  
While @enzotib probably is right, a BASH script could be used as a workaround anyway. –  sr_ Mar 5 '12 at 16:45
    
phpunit | cat didn't work unfortunately. However the bash script approach worked great. Thanks! –  netbrain Mar 6 '12 at 7:45
1  
@netbrain: as supposed, the fact that phpunit | cat do not work is a symptom that the problem is in phpunit and not in watch. –  enzotib Mar 6 '12 at 9:17
    
On some common Unixes (Such as Snow Leopard), --color is not a valid flag for watch. –  Stefan Lasiewski Mar 7 '12 at 0:35

2 Answers 2

up vote 2 down vote accepted

phpunit | cat didn't work unfortunately. However the bash script approach worked great. Thanks!

    #!/bin/bash
    while true; do
        (echo -en '\033[H'
            CMD="$@"
            bash -c "$CMD" | while read LINE; do 
                echo -n "$LINE"
                echo -e '\033[0K' 
            done
            echo -en '\033[J') | tac | tac 
        sleep 2 
    done

    botch my-command
share|improve this answer
5  
Please update your answer with more details. As it is, if the comments on your question get removed, it is not very helpful. Include at least the link to that script you're using, or better: whatever you ended up doing exactly so it can help future visitors if that link goes dead. –  Mat Mar 6 '12 at 8:19
    
@netbrain also phpunit | cat wasnt supposed to work. It was supposed to be a test to prove watch wasn't removing the colors, but rather phpunit wasnt outputting them when it noticed that STDOUT wasnt a TTY. –  Patrick Jun 5 '12 at 0:51
    
Also remember to accept your own answer. –  bahamat Jul 4 '12 at 17:54

Here my implementation, it's a bash script but it's very easy convert it to function (to change 'exit' to 'return')

#!/bin/bash

trap ctrl_c INT

function ctrl_c()
{
    echo -en "\033[?7h" #Enable line wrap
    echo -e "\033[?25h" #Enable cursor
    exit 0
}

function print_usage()
{
    echo
    echo '  Usage: cwatch [sleep time] "command"'
    echo '  Example: cwatch "ls -la"'
    echo
}

if [ $# -eq 0 ] || [ $# -gt 2 ]
then
    print_usage
    exit 1
fi

SLEEPTIME=1
if [ $# -eq 2 ]
then
    SLEEPTIME=${1}
    if [[ $SLEEPTIME = *[[:digit:]]* ]]
    then
        shift
    else
        print_usage
        exit 1
    fi
fi

CMD="${1}"
echo -en "\033[?7l" #Disable line wrap
echo -en "\033[?25l" #Disable cursor
while (true)
do

    (echo -en "\033[H" #Sets the cursor position where subsequent text will begin
    echo -e "Every ${SLEEPTIME},0s: '\033[1;36m${CMD}\033[0m'\033[0K"
    echo -e "\033[0K" #Erases from the current cursor position to the end of the current line
    BASH_ENV=~/.bashrc bash -O expand_aliases -c "${CMD}" | while IFS='' read -r LINE 
    do
        echo -n "${LINE}"
        echo -e "\033[0K" #Erases from the current cursor position to the end of the current line
    done
    #echo -en "\033[J") | tac | tac #Erases the screen from the current line down to the bottom of the screen
    echo -en "\033[J") #Erases the screen from the current line down to the bottom of the screen
    sleep ${SLEEPTIME}
done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.