[MDEV-7552] Add colors in the shell Created: 2015-02-06  Updated: 2015-10-30  Resolved: 2015-10-30

Status: Closed
Project: MariaDB Server
Component/s: Scripts & Clients
Fix Version/s: N/A

Type: Epic Priority: Major
Reporter: Fabio Alessandro Locati Assignee: Unassigned
Resolution: Won't Fix Votes: 1
Labels: None

Epic Name: Colors in the shell

 Description   

It would be great to have, at least optional, colors for the command line.

The most critical one I think it's for the "prompt" variable to be able to easily recognise production and testing environment, while it would be a nice to have to be able to use it even for queries (as you write them) and for results, warnings and errors.



 Comments   
Comment by Jean Weisbuch [ 2015-02-07 ]

Its possible to add colors to the prompt by modifying the mysql CLI tool by adding ANSI color codes to processed_prompt.append() calls on the construct_prompt() function located on client/mysql.cc, for example replacing :

processed_prompt.append("localhost");

By :

processed_prompt.append("\033[01;32mlocalhost\033[00m");

At line 5120 on 10.0.16 will end up having the prompt "\h" to output "localhost" in green while keeping the rest of the prompt with the default color.

As its less than convenient to have to modify the source code to put statically colors, the simplest way to allow colors on prompts is to add new "variables" on the construct_prompt() function such as :

      case '0':
        /* print the following text in the default terminal color */
        processed_prompt.append("\033[00m");
        break;
      case '2':
        /* print the following text in green */
        processed_prompt.append("\033[01;32m");
        break;
      case '3':
        /* print the following text in yellow */
        processed_prompt.append("\033[01;33m");
        break;

And adding a reset of the color in the end of the prompt (if \0 was not called before the end of the prompt) by replacing at the end of the function :

  processed_prompt.append('\0');

With :

  processed_prompt.append("\033[00m\0");

(doing this only if a color code has been specified on the prompt would be safer/cleaner)

Then having a prompt of "\2\N\0 \h [\3\d\0]> " would display "MariaDB localhost [(none)]> ".

You might also be interrested in MDEV-505 which is about adding the ability to display the actual server hostname instead of localhost when connecting to localhost/socket.

Comment by Fabio Alessandro Locati [ 2015-02-07 ]

the prompt is also possible to be modified from the .my.cnf file, but the problem is that if I use

prompt='master [\d]> '

I see it properly (not colored but I see what I'm expecting, while if I write

prompt='\033[0;31mmaster\033[00m [\d]> '

it shows the string instead of "parsing" the \033[0;31m r\033[00m codes to actual colours.
This I believe whould be the expected behaviour

Comment by Jean Weisbuch [ 2015-02-08 ]

Allowing the input of complete color codes this way would require some more complex changes as the color codes have the default delimiter ";" on it and also because it only allow macros of one character as it parse only the next character after an antislash "\".

I also dont think that it would make it easier to read and simpler to modify than my initial proposal but its only my point of view.

Comment by Fabio Alessandro Locati [ 2015-02-10 ]

It's surely true your point, it will be easier with a "remap" of colours.

At the moment IIRC, there are 256 colours available so the code would become pretty long if for each color 4 lines are added (that would be 1024 lines). So a way to put the colors directly from the input string to the output string could be line saving.

At the end of the day, I'm more looking for the feature than the implementation itself.

Comment by Jean Weisbuch [ 2015-02-11 ]

There are 8 main ANSI colors (including white and black) and the same 8 background colors : http://www.bluesock.org/~willg/dev/ansi.html
Attributing 16 macros could become a bit tricky, using only the main colors as direct macros using numbers and using a modifier for setting the background color is also possible but would make it a bit less complicated for end user.

Attributes such as bold/light, blinking or underline could be implemented as modifiers ; for example if \u was for underline, every color codes defined afterwise would be underlined and another \u or a \0 (a macro to unset all style attributes) would undefine the modifier.

For example :

prompt \u\2\N\0 \h [\3\d\0]>

Would display :

MariaDB localhost [(none)]>

Comment by Sergei Golubchik [ 2015-10-30 ]

One can easily do it now. I've just got my client to show

MariaDB (test)>

with this line in my .my.cnf:

[mysql]
prompt="^[[1;32m\N^[[0;39m (^[[1;31m\d^[[0;39m)> "

That is, you can put ANSI sequences directly in the config file and they will work just fine.

Generated at Thu Feb 08 07:20:28 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.