예제 #1
0
        /// <summary>
        /// Prints console row.
        /// </summary>
        /// <param name="cols"></param>
        /// <param name="colLengths"></param>
        public static string Row(string[] cols, int[] colLengths)
        {
            var options = new AlignLineOptions
            {
                Lengths = colLengths,
                Borders = new[] { "", "  ", "" },
                TreatDBytesTableLineAsByte = false,
                OverflowHidden             = true,
            };

            return(GetAlignConsoleLine(cols, options));
        }
예제 #2
0
        /// <summary>
        /// Prints console table with no border for models.
        /// </summary>
        /// <param name="headers"></param>
        /// <param name="colLines"></param>
        /// <param name="lengths"></param>
        public static string NoBorderTable(string[] headers, string[][] colLines, int[] lengths)
        {
            var sb = new StringBuilder();

            var options = new AlignLineOptions
            {
                Lengths = lengths,
                Borders = new[] { "", "  ", "" },
                TreatDBytesTableLineAsByte = false,
            };

            if (!(headers is null))
            {
                sb.AppendLine(GetAlignConsoleLine(headers, options));
            }

            foreach (var colLine in colLines)
            {
                sb.AppendLine(GetAlignConsoleLine(colLine, options));
            }

            return(sb.ToString());
        }
예제 #3
0
        public static string GetAlignConsoleLine(string[] cols, AlignLineOptions options)
        {
            if (cols.Length != options.Lengths.Length)
            {
                throw new ArgumentException($"The length of the argument `{nameof(cols)}` and `{nameof(options.Lengths)}` must be same.");
            }

            var lineCols = new string[cols.Length];

            Array.Copy(cols, lineCols, cols.Length);

            var sb = new StringBuilder(options.Lengths.Sum() + options.Borders.Sum(x => x.Length));

            var cellList    = new List <string>();
            var needNewLine = true;

            while (needNewLine)
            {
                needNewLine = false;
                foreach (var kv in lineCols.AsKvPairs())
                {
                    var lineCol = kv.Value;
                    var length  = options.Lengths[kv.Key];

                    if (options.GetStringLengthA(lineCol) <= length)
                    {
                        lineCols[kv.Key] = "";
                        cellList.Add(lineCol.PadRightA(length));
                    }
                    else
                    {
                        for (int i = 0, lineLengthA = 0; i < lineCol.Length; i++)
                        {
                            var chLengthA = options.GetCharLengthA(lineCol[i]);

                            if (lineLengthA + chLengthA <= length)
                            {
                                lineLengthA += chLengthA;
                            }
                            else
                            {
                                var lineContent = lineCol.Substring(0, i);
                                cellList.Add(lineContent.PadRightA(length));

                                lineCols[kv.Key] = lineCol.Substring(i);
                                needNewLine      = true;
                                break;
                            }
                        }
                    }
                }

                sb.AppendLine($"{options.Borders[0]}{cellList.Join(options.Borders[1])}{options.Borders[2]}");
                cellList.Clear();

                if (options.OverflowHidden)
                {
                    break;
                }
            }

            sb.Length -= Environment.NewLine.Length;
            return(sb.ToString());
        }