I'm trying to add an alias for lolcat that will invoke after each command put into the terminal.
I only know how to add it to select items, anyone know how to just make it a standard output?
aliasbashterminal
I'm trying to add an alias for lolcat that will invoke after each command put into the terminal.
I only know how to add it to select items, anyone know how to just make it a standard output?
Best Answer
UPDATE
I put my implementation here with install instructions:
https://github.com/dosentmatter/rainbow-bash-prompt
It uses the C implementation of lolcat but I modified it a little to make it psuedo-random.
OLD ANSWER
Here is how I did it. Add this to your
.bashrc
:It works by running
lolcat
dynamically on$PS1_colorless
and doing ased
to add in control characters so line wrapping works correctly.It takes a little less than 200 milliseconds to run. So it might be too slow for you. Here is an example I timed:
I chose to use
PS1
and notPROMPT_COMMAND
because I wanted to be able to use the bash prompt escape sequences such as\h
,\W
, etc. which wouldn't be possible withPROMPT_COMMAND
since I need to pass it toPS1
for expansion before runninglolcat
. You can use alternatives like${HOSTNAME%%.*}
to imitate the behavior of\h
but I don't think you can do it for something likeIt uses
-f
to forcelolcat
to run even though the output is being piped tosed
.lolcat
defaults to not output anything if the output is not to a tty.After doing the
lolcat
, it does ased
to surround the ansi color syntax such asESC[38;5;184m
(ESC
is the control character\033
) with the control charactersSOH
andSTX
so it becomesSOHESC[38;5;184mSTX
.SOH
andSTX
are just\[
and\]
respectively in the bash prompt control characters. I have to useSOH
andSTX
control characters instead of\[
and\]
because I am generating colors dynamically.\[
and\]
would not work because they are translated to control characters whenPS1
is first parsed.\[
and\]
would work if you usedPROMPT_COMMAND
to generate output forPS1
but then I wouldn't be able to use\u
and\W
because lolcat would have been run before passing\u
toPS1
for expansion.For reference: https://stackoverflow.com/questions/24839271/bash-ps1-line-wrap-issue-with-non-printing-characters-from-an-external-command
Characters surrounded by
SOH
andSTX
are treated as non printing characters. This is required so line-wrapping works correctly and doesn't treat the ansi color as actual visible characters.If you want to use literal non-printable control characters, you can replace
this
with this
Note the
^[
,^A
, and^B
are the literal control characters so you can't just copy and paste. You would have to use something like vim to enter themIn vim:
^[
== Ctrl+vEsc or Ctrl+vo033^A
== Ctrl+vCtrl+a or Ctrl+vo001^B
== Ctrl+vCtrl+b or Ctrl+vo002EDIT (a little faster in python): The python one seem to run a little bit faster:
Change these variables in your .bash_profile:
The python version has extra newlines in the output so I remove them with the
tr
. It also removes trailing spaces so I removed the trailing spaces fromPS1_colorless
and add them back withPS1_spacing
.You can get it here: https://github.com/tehmaze/lolcat
I will update if I ever get the C port working: https://github.com/jaseg/lolcat
I can't get it to build on mac os. If anyone can get it working on linux or mac os, please let me know.