1

I’m making some post-formatting of a log for better readability.

Using regular expressions, my script recognizes some patterns in the stream and makes the correspondent text bold by inserting the $(tput bold) and $(tput sgr0) values at the beginning and at the end of the text respectively. The stream is not textual, it already contains some special formatting escape sequences like colors and so on.

    $(tput setaf 6)
    Part of this text can be $(tput bold)bold$(tput sgr0) but
    the rest of the text has lost its initial color.

The problem is that I cannot figure out how to turn off the bold mode only. Using $(tput sgr0) drops all the other current terminal settings to their default values, including the color.

I use man terminfo to know what “capname” I should use. For example, I know how to make some text italic, because the list contains both — the sitm capname to enter_italic_mode and the ritm capname to exit_italic_mode.

    $(tput setaf 6)
    Part of this text can be $(tput sitm)italic$(tput ritm) but
    the rest of the text still has its initial color.

I’d like to use the same approach to make text bold because the terminfo list contains the bold capname to enter_bold_mode. But there is neither “exit_bold_name” string variable nor a correspondent capname!

Am I missing something? Should I use another approach? In all the articles I have found on the Internet it is used the sgr0 capname solution only.

oneastok
  • 323
  • 1
  • 11
  • is `tput smso` and `tput rmso` an acceptable approach? – William Pursell Sep 25 '21 at 22:26
  • I tried. `tput smso` makes the background dark and inverts the color of text. That’s not I’d like, unfortunately. – oneastok Sep 25 '21 at 22:29
  • The documentation claims that `sc` and `rc` will save and restore position, but `tput sc; echo foo; tput bold; echo bar; tput rc; echo baz` seems to work in my terminal to restore attributes rather than position. – William Pursell Sep 25 '21 at 22:34
  • `printf "Hello, $(tput sc)$(tput bold)this$(tput rc) world. \n"` produces just `Hello, world.` The word “this” disappears. Do I do something wrong? – oneastok Sep 25 '21 at 22:57
  • No, what you are seeing is what I would expect. I am extremely surprised to find that I do not see that behavior, but instead I see the attributes being saved and restored. Very curious to know why. – William Pursell Sep 25 '21 at 23:01
  • Ah...I am seeing that behavior because the cursor position is at the bottom of the visible screen, so the scrolling effect has an impact. – William Pursell Sep 25 '21 at 23:02
  • The behavior is terminal-dependent. Restoring attributes on cursor-restore happens to be something that VT100s did (and derived stuff such as xterm do -- though for others, *ymmv*). – Thomas Dickey Sep 26 '21 at 00:49
  • Not all terminals support an explicit undo for **bold**. VT100 did not. Conventional terminfo took info account what was common to many terminals. – Thomas Dickey Sep 26 '21 at 00:51
  • This is the same type of question as [this one](https://stackoverflow.com/questions/68893646/reset-only-the-foreground-color-using-terminfo/68914665#68914665). I'd mark it as a duplicate. – Thomas Dickey Sep 26 '21 at 00:56
  • The link you’ve provided does not answer the question. When I make some text bold, the text already has a color that is unknown and arbitrary. Resetting to the default color, as suggested there, in fact, is the same as `tput sgr0`. And, unlike the author of that question, I don’t know the hard codes that I could use. – oneastok Sep 26 '21 at 15:24

0 Answers0