douglasg14b / BetterConsoleTables

Faster, colorable, more configurable, and more robust console colors & tables for C# console applications
GNU Lesser General Public License v3.0
92 stars 16 forks source link

Exception when piping to file #17

Closed savaged closed 3 years ago

savaged commented 4 years ago

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());
        }
douglasg14b commented 4 years ago

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

savaged commented 4 years ago

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

douglasg14b commented 4 years ago

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

savaged commented 4 years ago

That's fixed it.

douglasg14b commented 4 years ago

Keeping this open as a task item.

douglasg14b commented 3 years ago

Closing as resolved