You are not logged in.
Pages: 1
Hello,
I am trying to setup my PS1 with hex code colors. I have written a program which hashes the current directory into a hex code, and I would like to take that output and put it into PS1.
Specifically, my PS1 currently looks like this:
'[\[\033[01;37m\]$PWD\033[01;00m\]] 'I would like to replace the '37' color code that colors the $PWD white with the output from my program. This seems like this should be feasible, as I can enter "echo -e '\033]10;#0000ff\007'" to change my foreground to blue, but when I tried to implement this by setting my .bashrc to:
trap "color='#'$(~/code/dircolor $PWD)" DEBUG
echo $color
PS1='[\[\033[01;${color}m\]$PWD\033[01;00m\]] 'the result is:
#88c688
[ome/user] 1;2cI have also found reference to being able to insert hex codes via "\e]PXRRGGBB" where 'X' is a hex character and the 'RRGGBB' is the hex color code however when I enter this as an argument to "echo -e" to test, this just results in the terminal outputting 'RRGGBB'
I've been poking at this for a few hours now and I'm not sure where to go from here.
For reference, I am using URXVT for my terminal emulator.
Offline
I'm not sure what that trap is about, but you need to put the command substitution in your PS1 as the PWD will change, e.g.,
PS1='\[\e]Px$(~/code/dircolor $PWD)\]$PWD ...'If it's your own code, you'd be better off just writing this all into the "dircolor" program so PS1 can just be:
PS1='$(dircolor)'The program can check the current working directory, do what it does, and print out all the escape codes and content needed. I had a setup much like this for a while.
You may also want to consider a different name for your program to not confuse it with dircolors.
Last edited by Trilby (2020-08-22 16:37:20)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
I'm not sure what that trap is about, but you need to put the command substitution in your PS1 as the PWD will change, e.g.,
PS1='\[\e]Px$(~/code/dircolor $PWD)\]$PWD ...'
The "trap" was to ensure that it was always updated whenever I change directory. I was under the impression that .bashrc is only executed when the terminal emulator starts. I take it I don't need to do that then?
Also, that code didn't seem to work as it outputted the color code. As a test, instead of using my program as an output, I just hard-coded an arbitrary color code so that my PS1 looks like:
PS1='\[\e]PABBCCDD\]$PWD 'This results in:
ABBCCDD/home/user If it's your own code, you'd be better off just writing this all into the "dircolor" program so PS1 can just be:
PS1='$(dircolor)'The program can check the current working directory, do what it does, and print out all the escape codes and content needed. I had a setup much like this for a while.
That's a good idea. Once we get the actual syntax correct, I might do that.
You may also want to consider a different name for your program to not confuse it with dircolors.
Good catch
. I'll make a note to change that. I didn't know 'dircolors' was a command.
Offline
So, I found the issue:
Despite many sources on the Internet telling me that URXVT does support 24-bit color, I found this article which points out that URXVT only really tries to translate 24-bit color into 8-bit. They have a list of other terminals that do properly support it.
I ran the following test command they had listed, and while it did not work in URXVT, it did work in kitty, which was one of the terminal emulators listed that supports it.
printf "\x1b[38;2;255;100;0mTRUECOLOR\x1b[0m\n"Time to switch terminal emulators lol.
Don't you just love it when the Internet sends you down hours of needless troubleshooting
![]()
Offline
I was under the impression that .bashrc is only executed when the terminal emulator starts.
It is, but if you have a single-quoted command substitution in PS1, the command will be evaluated for each prompt.
As for the color codes, very few terminals support 16G different colors at a time. Quite a few, however, including urxvt, can use 256 colors and you can remap most of those 256* colors to any hex code for one of 16G colors. Do you really need more than 256 different colors in your terminal? Or could you just map the hex codes to one of the 256 colors once, then use the much simpler, and much more well supported 256 color ansi sequences?
* note: you can remap all 256 colors if you wish, but I'd strongly advise keeping 0-15 shades of their expected colors, and 232-255 grayscale - but this still leaves 216 that you can do whatever you want with. As virtually no terminal program would use "truecolor" sequences for the terminal (just 256) then the only use for the 16G colors would be in your prompt. So, unless you expect your prompt to have more than 216 different colors, then urxvt should be just fine.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
TheMohawkNinja wrote:I was under the impression that .bashrc is only executed when the terminal emulator starts.
It is, but if you have a single-quoted command substitution in PS1, the command will be evaluated for each prompt.
As for the color codes, very few terminals support 16G different colors at a time. Quite a few, however, including urxvt, can use 256 colors and you can remap most of those 256* colors to any hex code for one of 16G colors. Do you really need more than 256 different colors in your terminal? Or could you just map the hex codes to one of the 256 colors once, then use the much simpler, and much more well supported 256 color ansi sequences?
* note: you can remap all 256 colors if you wish, but I'd strongly advise keeping 0-15 shades of their expected colors, and 232-255 grayscale - but this still leaves 216 that you can do whatever you want with. As virtually no terminal program would use "truecolor" sequences for the terminal (just 256) then the only use for the 16G colors would be in your prompt. So, unless you expect your prompt to have more than 216 different colors, then urxvt should be just fine.
Yeah, that's a fair point.
I've retooled the program and now it works just fine with 256 colors. My PS1 now reads:
PS1='[\[\033[1;38;5;$(~/code/dircolor $PWD)m\]$PWD\033[01;00m\]] 'I'll rename the program in a bit to differentiate it from 'dircolors'.
Thanks again!
Offline
Again, if it's your program, you could really simplify that - the program "dircolor" can print the escape codes itself. Note the end with "\033[01;00m" doesn't make much sense, that should just be "\033[0m". Also, you are placing the "\[" and "\]" around everything which will screw up readline (so if you use up-arrow or similar to recall previous commands, your entry will get butchered). To use those correctly, they should be just around the non-printing sequences (the color sequences).
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Pages: 1