minhhungit / ConsoleTableExt

A fluent library to print out a nicely formatted table in a console application C#
MIT License
323 stars 36 forks source link

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

Open Decimation opened 2 years ago

Decimation commented 2 years ago

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

minhhungit commented 1 year ago

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

minhhungit commented 1 year ago

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

Decimation commented 1 year ago

@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

mchudinov commented 7 months ago

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  |