In xterm, can I turn off bold or underline without resetting the current color?

I process input (from sources such as ls -la --color, for example) and underline certain sections of the text. I donโ€™t process these inputs character-by-character, but with a lot of regular expressions, which makes it difficult to keep track of the fact that the substring that I am affecting is already colored or in bold. If I have a red block of text, and I want to emphasize part of it, I could do something like:

s/(123)/\033[4m\1\033[0m/g 

(My expressions are much more complex, in fact, received, processed by themselves, and then broken and analyzed. This is not something that can be done by changing the expression specified here.)

The code above will replace all 123 cases with [UNDERLINE_START] 123 [FORMAT_RESET]. Unfortunately, reset also disables text coloring. This would save me a big headache if I could just turn off the underline when that's all I want to turn off, but I'm sure there is no way to do this. Can someone tell me I'm wrong?

EDIT: I could simplify the question by asking: if I want to turn off the underline, can I do this without affecting the current color of the text, since that color could have been set long before my script even started to run, and I have no way determine what color is it?

+5
source share
2 answers

No one is documenting this, but adding 20 to the decorator codes will disable them:

  echo -e "\\033[34;4m" underlined "\\033[24m" not underlined echo -e "\\033[34;1m" bold "\\033[2m" not bold echo -e "\\033[34;2m" dark "\\033[22m" not dark echo -e "\\033[34;7m" inverse "\\033[27m" not inverse 
+4
source

Instead of these hard-coded escape sequences, use:

 tput smul # set underline tput rmul # remove underline tput smso # set bold on tput rmso # remove bold tput setaf 1 #red tput setaf 2 #green ... tput cup 0 0 # move to pos 0,0 

See "man terminfo" and "man tput" for a complete description of these commands.

Examples:

 function f_help { c_green=$(tput setaf 2 2>/dev/null) c_reset=$(tput sgr0 2>/dev/null) c_bold=$(tput smso 2>/dev/null) echo "${c_bold}DESCRIPTION${c_reset} : ...." } echo "${c_green}GREEN ${c_underline} GREEN AND UNDERLINED${c_nounderline} GREEN AGAIN${c_reset} PLAIN BLACK TEXT" 

the escape sequence $ {c_underline} does not affect the color of the text; he only emphasizes. $ {c_nounderline} disables only underscores.

+1
source

All Articles