예제 #1
0
        /// <summary>
        /// Gets an individual row for the table.
        /// </summary>
        /// <param name="row">The number of the row to get a string for.</param>
        /// <param name="withColor"><see langword="true"/> to print with color, otherwise false.</param>
        /// <param name="chrome">The <see cref="IChromeCollection"/> to use.</param>
        /// <param name="totalWidth">The total width of the table.</param>
        /// <param name="numCols">The number of columns in each row.</param>
        /// <param name="columnWidths">The individual column widths.</param>
        /// <returns>A string containing the specified row.</returns>
        private string GetRow(int row, bool withColor, IChromeCollection chrome, int totalWidth, int numCols, int[] columnWidths)
        {
            var sb = new StringBuilder();

            var numLines = 1;

            for (var i = 0; i < numCols; i++)
            {
                numLines = Math.Max(rows[row][i].GetNumberOfLines(columnWidths[i]), numLines);
            }

            for (var line = 0; line < numLines; line++)
            {
                sb.Append(chrome.BodyLeft);

                sb.Append(' ');
                for (var col = 0; col < numCols; col++)
                {
                    sb.Append(rows[row][col].GetLine(line, columnWidths[col], withColor));
                    sb.Append(' ');
                    if (col < numCols - 1)
                    {
                        sb.Append(chrome.BodyInteriorVertical);
                        sb.Append(' ');
                    }
                }

                sb.Append(chrome.BodyRight);
                sb.Append(ConsoleBase.NewLine);
            }

            return(sb.ToString());
        }
예제 #2
0
        /// <summary>
        /// Gets the header for the table. This can be nothing, in which case the standard top-of-body chrome will be added.
        /// </summary>
        /// <param name="chrome">The <see cref="IChromeCollection"/> to use.</param>
        /// <param name="maxWidth">The max width a column can be.</param>
        /// <param name="numCols">The number of columns.</param>
        /// <param name="withColor"><see langword="true"/> to print with color, otherwise false.</param>
        /// <param name="columnWidths">Collection of individual column widths.</param>
        /// <returns>A string containing the table header.</returns>
        private string GetHeader(IChromeCollection chrome, int maxWidth, int numCols, bool withColor, int[] columnWidths)
        {
            var sb = new StringBuilder();

            var numLines = headers.Max(c => c.GetNumberOfLines(maxWidth));

            if (numLines > 0)
            {
                sb.Append(this.GetChromeLine(chrome.HeaderTopLeft, chrome.HeaderTop, chrome.HeaderTopJoin, chrome.HeaderTopRight, columnWidths));
            }

            for (var line = 0; line < numLines; line++)
            {
                sb.Append(chrome.HeaderLeft);

                sb.Append(' ');
                for (var head = 0; head < numCols; head++)
                {
                    sb.Append(headers[head].GetLine(line, columnWidths[head], withColor));
                    sb.Append(' ');
                    if (head < numCols - 1)
                    {
                        sb.Append(chrome.HeaderInteriorVertical);
                        sb.Append(' ');
                    }
                }

                sb.Append(chrome.HeaderRight);
                sb.Append(ConsoleBase.NewLine);
            }

            if (this.Rows.Count > 0)
            {
                if (numLines > 0)
                {
                    sb.Append(this.GetChromeLine(chrome.HeaderBodyLeftJoin, chrome.HeaderBodyHorizontal, chrome.HeaderBodyInteriorJoin, chrome.HeaderBodyRightJoin, columnWidths));
                }
                else
                {
                    sb.Append(this.GetChromeLine(chrome.BodyTopLeft, chrome.BodyTop, chrome.BodyTopJoin, chrome.BodyTopRight, columnWidths));
                }
            }
            else
            {
                sb.Append(this.GetChromeLine(chrome.HeaderBottomLeft, chrome.HeaderBottom, chrome.HeaderBottomJoin, chrome.HeaderBottomRight, columnWidths));
            }

            return(sb.ToString());
        }
예제 #3
0
        /// <summary>
        /// Gets the body of the table.
        /// </summary>
        /// <param name="chrome">The <see cref="IChromeCollection"/> to use.</param>
        /// <param name="withColor"><see langword="true"/> to print with color, otherwise false.</param>
        /// <param name="totalWidth">The total width of the table.</param>
        /// <param name="numRows">The total number of rows in the table.</param>
        /// <param name="numCols">The number of columns in each row.</param>
        /// <param name="columnWidths">Collection of individual column widths.</param>
        /// <returns>A string containing the table body.</returns>
        private string GetBody(IChromeCollection chrome, bool withColor, int totalWidth, int numRows, int numCols, int[] columnWidths)
        {
            var sb = new StringBuilder();

            for (var row = 0; row < numRows; row++)
            {
                sb.Append(this.GetRow(row, withColor, chrome, totalWidth, numCols, columnWidths));

                if (row < (numRows - 1))
                {
                    sb.Append(this.GetChromeLine(chrome.BodyLeftJoin, chrome.BodyInteriorHorizontal, chrome.BodyInteriorJoin, chrome.BodyRightJoin, columnWidths));
                }
            }

            sb.Append(this.GetChromeLine(chrome.BodyBottomLeft, chrome.BodyBottom, chrome.BodyBottomJoin, chrome.BodyBottomRight, columnWidths));

            return(sb.ToString());
        }
예제 #4
0
        /// <summary>
        /// Returns a string containing this table formatted for displaying in a log or console.
        /// </summary>
        /// <param name="maxWidth">The maximum width to allow each cell to be.</param>
        /// <param name="withColor">A value indicating whether or not to print the cell colors with the table.</param>
        /// <param name="chrome">The <see cref="IChromeCollection"/> to use when printing the table.</param>
        /// <returns>A string containing the formatted table.</returns>
        public string ToString(int maxWidth, bool withColor, IChromeCollection chrome)
        {
            var sb = new StringBuilder();

            var numRows = this.NumberOfRows;
            var numCols = this.NumberOfColumns;

            if (maxWidth == 0)
            {
                maxWidth = this.rows.Max(r => r.Max(c => c.GetTotalWidth())) * numCols;
            }

            var columnWidths = new int[numCols];

            for (var i = 0; i < numCols; i++)
            {
                columnWidths[i] = Math.Min(rows.Max(r => r[i].GetTotalWidth()), maxWidth);
                if (headers != null && headers.Count > i)
                {
                    columnWidths[i] = Math.Min(Math.Max(columnWidths[i], headers[i].GetTotalWidth()), maxWidth);
                }

                columnWidths[i] = Math.Max(columnWidths[i], MinimumColumnWidth);
            }

            var totalWidth = columnWidths.Sum();

            totalWidth += (numCols * 2) + numCols + 1;

            if (headers.Count > 0)
            {
                sb.Append(this.GetHeader(chrome, maxWidth, numCols, withColor, columnWidths));
            }
            else
            {
                sb.Append(this.GetChromeLine(chrome.BodyTopLeft, chrome.BodyTop, chrome.BodyTopJoin, chrome.BodyTopRight, columnWidths));
            }

            sb.Append(this.GetBody(chrome, withColor, totalWidth, numRows, numCols, columnWidths));

            return(sb.ToString());
        }
예제 #5
0
 /// <summary>
 /// Returns a string containing this table formatted for displaying in a log or console.
 /// <para>Uses the <see cref="Formatters.Chrome"/> property for determining what chrome
 /// characters to use.</para>
 /// </summary>
 /// <param name="maxWidth">The maximum width to allow each cell to be.</param>
 /// <param name="chrome">The <see cref="IChromeCollection"/> to use when printing the table.</param>
 /// <returns>A string containing the formatted table.</returns>
 public string ToString(int maxWidth, IChromeCollection chrome)
 {
     return(this.ToString(maxWidth, Options.Instance.ColorizeConsoleOutput, Formatters.Chrome));
 }
예제 #6
0
 /// <summary>
 /// Returns a string containing this table formatted for displaying in a log or console.
 /// <para>Uses the <see cref="Formatters.Chrome"/> property for determining what chrome
 /// characters to use.</para>
 /// </summary>
 /// <param name="chrome">The <see cref="IChromeCollection"/> to use when printing the table.</param>
 /// <returns>A string containing the formatted table.</returns>
 public string ToString(IChromeCollection chrome)
 {
     return(this.ToString(0, Options.Instance.ColorizeConsoleOutput, chrome));
 }