'How to change in terminal the background color of tab space "\t" by C\C ++ [closed]

I wrote a program in C++ that shows a data table in the terminal. The values are of different lengths, so to arrange them in columns I use "\t" to print a tab space. When try to change the background color of some row, the tab space remain in black background (the default background).

An example of the problem:

Code:

cout << "\033[46mHello\tWorld\033[0m";

Actual output: enter image description here

Output I want: enter image description here



Solution 1:[1]

There is no builtin way that I know of to do what Python does, you would have to code it yourself.

$Data | Format-Hex will show you the character codes in the string, in hexadecimal. That's not easy to read, but it is enough to distinguish different whitespace characters:

PS C:\> $data = "A`r`nZ"
PS C:\> $data |Format-Hex


           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   41 0D 0A 5A                                      A..Z

0D is char 13, carriage return. 0A is char 10, linefeed.

If you wanted to code it, something like this:

PS C:\> $translated = foreach ($char in $data.GetEnumerator()) {
  switch ($char) {
    "`0" { '`0' }
    "`a" { '`a' }
    "`b" { '`b' }
    "`e" { '`e' }
    "`f" { '`f' }
    "`n" { '`n' }
    "`r" { '`r' }
    "`t" { '`t' }
    "`v" { '`v' }
    default { $char }
  }
}

PS C:\> -join $translated
A`r`nZ

Taking the codes from the PowerShell tokenizer, what it will recognize inside double-quoted expandable strings as special characters, and turning them into single-quoted literal strings.

NB. not all of them can be turned back, I don't think. You could approach it differently with a regex to match non-printable control codes, or Unicode categories and try to cover more cases that way.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TessellatingHeckler