2

In GNU screen in the status it is possible to put conditional things, such as

%?%F%{+b r}%:%{+b b}%?

%F is the test; if true it turns on bold red formatting %{+b r}, whereas if false it turns on bold blue formatting %{+b b}.

Conditional statements like this with %F (which tests for focus) are the only ones I have seen in examples of .screenrc files or in the screen manual.

What other conditional statements can be used? The manual says:
"the part to the next '%?' is displayed only if a '%' escape inside the part expands to a non-empty string" which is a little confusing. Can I for example change the colour depending on the time of day or day of the week?

Thanks

3

Create a script that outputs something when the condition is true. This example will output a space when the time is between 9 AM and 4:59 PM.

#!/bin/bash
time=$(date +%H)
if (( time >= 9 && time <= 16 ))
then
    echo " "
fi

Save this script. Let's name it "screenbtdaytime". Also, do chmod u+x screenbtdaytime (or +x without the u to make it universally executable).

To demo this, use this line instead of the if above so the change occurs every 10 seconds so you don't have to wait all day to see the effect:

if (( $(date +%s) / 10 % 2 ))    # temporary for demo

Now in screen press Ctrl-a and : to bring up the command prompt and type in these two commands:

backtick 1 0 1 /path/to/screenbttime
caption always "%?%{+b by}%1`%:%{+b yb} %?%C | %D, %M %d, %Y"

You can use hardstatus instead of caption if you prefer, I believe.

Changing the 0 to 60 in the backtick command will make the updates happen once a minute rather than at the default rate.

This assigns the script to backtick command 1 (the first "1") which is used in the caption where you see %1`. You can have other commands associated with other numbers so you can have multiple things going on.

Since the script outputs a space during the designated time, it triggers the conditional %? which is set to output color codes for blue text on a yellow background. The "else" portion (%:) is performed when there's no output from the script and the colors are yellow on a blue background. An extra space is added in the "else" to visually match the one that is displayed as the output of the script.

1
  • Fantastic, thanks a lot. It does indeed work in the hardstatus line as well.
    – simonb
    Nov 11 '10 at 4:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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