private static string GetUnicodeAwarePaddedString(ColoredString coloredString, int space, Justify justify) { int actualSpace = Math.Max(space - coloredString.Width, 0); return(justify switch { Justify.Left => coloredString.ToColoredString() + new string(' ', actualSpace), Justify.Right => new string(' ', actualSpace) + coloredString.ToColoredString(), _ => throw new NotImplementedException(message: $"Justify {justify} not implemented."), });
private int GetColumnOccupiedSpace(int index) { int maxOccupiedSpace = HeaderStrings[index].Width; foreach (DataRow row in this.Rows) { ColoredString currentString = (ColoredString)row[index]; int currentOccupiedSpace = currentString.Width; if (currentOccupiedSpace > maxOccupiedSpace) { maxOccupiedSpace = currentOccupiedSpace; } } return(maxOccupiedSpace); }
public CsvTable(string csvPath, bool hasHeader = true, int fieldSpace = 2) { HasHeader = hasHeader; FieldSpace = fieldSpace; FieldSpaceString = new string(' ', FieldSpace); if (!File.Exists(csvPath)) { throw new FileNotFoundException(message: $"CSV file {csvPath} does not exist.", fileName: csvPath); } StreamReader stream = new StreamReader(csvPath, Encoding.UTF8); TextFieldParser reader = new TextFieldParser(stream) { HasFieldsEnclosedInQuotes = true, Delimiters = new string[] { "," }, CommentTokens = Array.Empty <string>(), TrimWhiteSpace = false, TextFieldType = FieldType.Delimited, }; if (HasHeader) { string[] headerBlock = reader.ReadFields(); foreach (string header in headerBlock) { HeaderStrings.Add(new ColoredString(header, ColoredString.ColorCode.NoColor)); this.Columns.Add(header, typeof(ColoredString)); } } else { string[] headerBlock = reader.ReadFields(); foreach (string header in headerBlock) { HeaderStrings.Add(new ColoredString("", ColoredString.ColorCode.NoColor)); this.Columns.Add("", typeof(ColoredString)); } // rewind to starting point of the stream stream.BaseStream.Position = 0; stream.DiscardBufferedData(); } while (!reader.EndOfData) { string[] entryBlock = reader.ReadFields(); DataRow row = this.NewRow(); for (int i = 0; i < entryBlock.Length; i++) { row[i] = new ColoredString(entryBlock[i], ColoredString.ColorCode.NoColor); } this.Rows.Add(row); } }