Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash, Always display colored bar at top of screen

Tags:

bash

Is there anyway to modify the bash profile scripts to always display a colored bar at top of screen. I have a requirement to show a colored hostname, username, and ipaddress on the screen at all times, but i don't want to overload PS1 as it would make the prompt take up over half of the default console width.

like image 528
ruckc Avatar asked Mar 22 '23 10:03

ruckc


2 Answers

Not perfect, but this shows you how to fix part of your prompt on the first row of the screen:

PS1='\w \[\e[s\e[1;1H\e[42m\]\h \u ipaddress\[\e[0m\e[u\]\$ '

A breakdown:

  • \e[s - save the current cursor position
  • \e[1;1H - move the cursor to row 1, column 1 (numbered from the upper left-hand corner
  • \e[u - restore the cursor to the previously saved position
  • \e42m - make the background green
  • \e0m - restore the default foreground/background colors
  • \[...\] - enclose the various non-printing characters so that bash can correctly compute the length of the prompt.

Wikipedia lists other escape codes. The two things missing from this answer are how to extend the bar all the way across the string and how to set the correct IP address.

Update: I believe this covers the changes that ruckc made:

PS1='\[\e[s\e[1;1H\e[42m\e[K\h \u ipaddress\e[0m\e[u\]\w \$ '
like image 76
chepner Avatar answered Apr 10 '23 16:04

chepner


How about add a \n inside your PS1, so that you always use a new line with full width?

like image 32
BigTailWolf Avatar answered Apr 10 '23 15:04

BigTailWolf