Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mini Project: Change the color of the terminal based on the time of day

Good Evening,

I am pretty new to Unix so maybe this mini project is too ambitious. Hoping someone could point in in the right direction.

Working in a cubicle and I can't see how light it is outside. In general I use a yellow terminal in the morning (sunny) and a black/green past 3:00 (night).

What I would like to do (partly just to practice my bash). Is to write a script that will at 3:00 change the color of all of my terminals. If that works maybe I will make them change every hour or something like that. Would that be safe? Is this possible?

Here are some of my specs: Solaris 10 Bash shell Gnome

I am looking at this right now: Change Gnome terminal theme programmatically

like image 818
sixtyfootersdude Avatar asked Jan 27 '10 23:01

sixtyfootersdude


2 Answers

If I were doing this, I'd start with PROMPT_COMMAND. Bash will run that script just before displaying a prompt.

You have a couple of choices. You could have a script itself inside PROMPT_COMMAND:

PROMPT_COMMAND='if [ is_morning ]; then echo "MORNING_COLORS'; else echo "EVENING_COLORS"; fi

Or you could have PROMPT_COMMAND run an external command (which could also be a bash script or you could use a different language if you wanted) to do all the work there:

PROMPT_COMMAND=/path/to/setcolor_timeofday

The only hole I see in this is that if you have a program running when the time changes over (for example, using tail -f to watch a file), the background won't change until you return to the bash prompt.

like image 170
R Samuel Klatchko Avatar answered Nov 09 '22 22:11

R Samuel Klatchko


Modifying the shell foreground and background colors via PS1 in bash will be the easiest, as pointed out by others here. R Samuel Klatchko's approach with PROMPT_COMMAND is probably the most versatile. However, this has drawbacks. Bash can only use 'dull' background colors, and its foreground/background colors can be overridden by any application. Modifying your Gnome Terminal theme programmatically, especially in realtime, will be far harder, but produce much prettier results.

like image 30
Sparr Avatar answered Nov 09 '22 21:11

Sparr