tags:

views:

128

answers:

1

I'm trying to control the titles of my xterm windows and my cleverness has finally outpaced my knowledge. :-)

I have three functions: one which sets the title of the window; one which takes a passed command, calls the title function, and executes the command; and one which resumes a job after using jobs to determine the title:

title () {
    echo -en "\e]1;$(hostname) : $1\a\e]2;$(hostname) : $2\a"
}

run () {
    title $(basename $1) "$*";
    $*
}

fg () {
    if [[ "x" == "x$1" ]]; then
        title $(jobs | awk '/\['$1'\]/{print $3}') "$(jobs | awk -F '  +' '/\[[0-9]\]\+/{print $3}')";
    else
        title $(jobs | awk '/\['$1'\]/{print $3}') "$(jobs | awk -F '  +' '/\['$1'\]/{print $3}')";
    fi;
    builtin fg $*
}

Now, all of this is working beautifully… well, mostly. Setting the title by calling the function manually works fine. Setting the title by way of the run function works fine. Setting the title by resuming a job works fine… unless the job was started with the run function:

$ nano foo.txt
<CTRL-Z>
$ run nano bar.txt
<CTRL-Z>
$ jobs
[1]-  Stopped                 nano foo.txt
[2]+  Stopped                 $*

Well, I suppose that is, technically, the name of the command being executed by the run function, but that isn't really a useful thing to know, in this case.

So, since I have not only reached but far exceeded the limits of my knowledge about Bash, perhaps someone here can help me fix this. :-)

A: 

Well, it isn't ideal, but I've come up with a solution which at least sets my titles correctly:

fg () {
    if [[ "x" == "x$1" ]]; then
        _CMD=$(ps -o cmd --no-headers $(jobs -l | awk '/\[[0-9]\]\+/{print $2}'));
    else
        _CMD=$(ps -o cmd --no-headers $(jobs -l | awk '/\['$1'\]/{print $2}'));
    fi;
    title $(basename $(echo $_CMD | awk '{print $1}')) "$_CMD";
    unset _CMD;
    builtin fg $*;
}

This uses jobs' -l option to get the process ID, then finds it with ps, which has the correct command listed. Unfortunately, ps seems to be quite slow, causing a noticeable delay (~¼ second) when resuming.

Ben Blank