ConsoleTableExt icon indicating copy to clipboard operation
ConsoleTableExt copied to clipboard

Suggestion: Ignore special control characters / virtual terminal sequences in row length calculation

Open Decimation opened this issue 3 years ago • 4 comments

Description

Special control characters such as ANSI escape codes are included in calculation of row length (which is determined by string length). This results in table rows appearing larger than they should be.

Example:

This is used for the row (outlined); the ANSI escape sequence is red yWxj5WcF8h_2022-05-06_(03h47m50s)

This results in the following output:

c1hBYeWyNq_2022-05-06_(03h49m30s) (The affected row is red)

This is because the length of the ANSI escape characters in the example is 7, which results in 7 additional characters in the row cell:

VsDebugConsole_2LEKSjhfj0

Possible Solution

A possible solution would be using regex to filter such characters and escape sequences from the row content string, and using the resulting string length to determine the row length.

See Console virtual terminal sequences

Decimation avatar May 06 '22 08:05 Decimation

thank you @Decimation I will check it when I have more time, sorry quite busy these days

minhhungit avatar Jan 14 '23 15:01 minhhungit

@Decimation can you pls give me a sample of code so I can reproduce it

minhhungit avatar Feb 05 '23 01:02 minhhungit

@Decimation can you pls give me a sample of code so I can reproduce it

The first picture in the OP contains one but I'll send a more concrete example later when possible

Decimation avatar Feb 07 '23 02:02 Decimation

The issue makes ugly the to colorize output, because color characters in console are calculated as table width. For example showing negative numbers in RED

  .WithFormatter(4, text =>
  {
      if (Int32.Parse(text.ToString()) < 0)
      {
          return text.ToString().Pastel(Color.Red);
      }
      return text.ToString();
  })

Is shown like this

 | Days                       | 
-|----------------------------| 
 | -811  |                      
 | -553  |                      
 | -546  |                      

mchudinov avatar Dec 03 '23 13:12 mchudinov