douglasg14b/BetterConsoleTables

Exception when piping to file

savaged opened this issue · 6 comments

Given a IList data set added to a Table using the From method is written to the console
When the result is piped to a file (e.g. dotnet run > test.txt)
Then an unhandled exception is raised, namely:

Unhandled exception. System.ArgumentOutOfRangeException: Positive number required. (Parameter 'width')
Actual value was -1.
   at System.ConsolePal.SetWindowSize(Int32 width, Int32 height)
   at System.Console.set_WindowWidth(Int32 value)
   at BetterConsoleTables.Table.PadRow(String row)
   at BetterConsoleTables.Table.FormatHeader(Int32[] columnLengths, IList`1 values, Char innerDelimiter, Char outerDelimiter)
   at BetterConsoleTables.Table.ToString(Int32[] columnLengths)
   at BetterConsoleTables.Table.ToString()

Example code:

        public void Present<T>(IList<T> index) where T : IModel
        {
            var table = new Table(TableConfiguration.Markdown());
            table.From(index);
            Console.WriteLine(table.ToString());
        }

Looks like I need to expand my try/catch block for a no-console scenario.


If the table is running without a console, be sure to set the TableConfiguration ConsoleAvailable to false. Give that a try and let me know if the error persists.

Example:

        public void Present<T>(IList<T> index) where T : IModel
        {
            var table = new Table(TableConfiguration.Markdown());
            table.Config.ConsoleAvailable = false;
            table.From(index);
            Console.WriteLine(table.ToString());
        }

Unfortunately I don't have this documented anywhere as of yet

error CS0176: Member 'TableConfiguration.ConsoleAvailable' cannot be accessed with an instance reference; qualify it with a type name instead

That just means it's a static field, which was my mistake on instructing you from memory.

Just access it directly on the configuration type. TableConfiguration.ConsoleAvailable = false

That's fixed it.

Keeping this open as a task item.

Closing as resolved