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

[bug]Misplaced format If there are UTF-8 characters in the data #30

Closed haptear closed 2 years ago

haptear commented 2 years ago

If there are UTF-8 characters in the data, it will cause the output to be misformatted

error

minhhungit commented 2 years ago

@haptear can you give me your test code please

haptear commented 2 years ago

@minhhungit the test code

using ConsoleTableExt;
using System;
using System.Collections.Generic;

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
            var tableData =new List<object>() { new { Id="xxx", Name="tab其它语言test", Host="127.0.0.1", Port=80, status ="success" } };

            ConsoleTableExt.ConsoleTableBuilder
                .From<dynamic>(tableData)
                .WithTitle("ALL CENTERS", ConsoleColor.Yellow, ConsoleColor.DarkGray)
                .WithFormat(ConsoleTableBuilderFormat.Alternative)
                .ExportAndWriteLine(TableAligntment.Left);

            Console.ReadLine();
        }
    }
}
minhhungit commented 2 years ago

image this is first look, still checking more

minhhungit commented 2 years ago

After adding Console.OutputEncoding = Encoding.Unicode; I see the problem

minhhungit commented 2 years ago

image

it looks it has extra space

haptear commented 2 years ago

Thank you for your quick reply

minhhungit commented 2 years ago

I have no idea, my lack of knowledge about this case

It counts length of text correctly, the text "tab其它语言test" has length 11, but after console print out, console need more space

haptear commented 2 years ago

The actual length of the string "tab其它语言test" is 15,UTF-8 characters should be *2

haptear commented 2 years ago

@minhhungit You can get the actual length with "GetStringLen" GetStringLen("tab其它语言test")==15

public static int GetStringLen(string str)
        {
            int i = 0;//count
            foreach (char newChar in str)
            {
                if ((int)newChar > 127)
                {
                    //utf-8 characters
                    i += 2;
                }
                else
                {
                    i++;
                }
            }
            return i;
        }
minhhungit commented 2 years ago

@haptear thank you about the suggestion but that is not only length issue but also windows console problem

Problem here for a text for example "tab其它语言test", we can get the length is 15, but windows app don't print the length exactly

I tried but no lucky, I am quite sure this is problem of windows console

minhhungit commented 2 years ago

I found this post

https://stackoverflow.com/questions/26975736/why-is-the-length-of-this-string-longer-than-the-number-of-characters-in-it

haptear commented 2 years ago

@minhhungit
hi can you merge request?

minhhungit commented 2 years ago

@haptear thanks, I will review the PR soon

minhhungit commented 2 years ago

released in 3.1.8