public static TextTableLine CreateLine( string nameColumnText, string typeColumnText, string statusColumnText, Color color, int textModifiers) { var nameColumnItem = CreateColumn(nameColumnText, .6f, color, textModifiers); var typeColumnItem = CreateColumn(typeColumnText, .2f, color, textModifiers); var statusColumnItem = CreateColumn(statusColumnText, .2f, color, textModifiers); var items = SList.Create <TextTableColumn>(2); SList.Add(items, nameColumnItem); SList.Add(items, typeColumnItem); SList.Add(items, statusColumnItem); var line = new TextTableLine(); line.Items = items; line.ItemsSeparator = string.Empty; line.MaxLineSize = DataHolder.TerminalData.MaxLineWidthInChars; line.MaxLineSizeIsForced = true; line.ItemsSeparator = " | "; line.SeparatorModifier.Color = color; line.SeparatorModifier.Modifiers = TextModifiers.Bold; TextUtil.FormatLineConsideringWeightsAndSize(line); return(line); }
/// <summary> /// Calculates and stores the sizes of the items of the given line accordingly to the maxLineSize and items weight. /// </summary> public static TextTableLine CalculateIdealTableItemSize(TextTableLine line) { int maxLineSize = line.MaxLineSize; // If no max line size was defined, do nothing if (maxLineSize <= 0) { return(line); } int separatorLength = 0; if (!string.IsNullOrEmpty(line.ItemsSeparator)) { separatorLength = line.ItemsSeparator.Length; } maxLineSize -= separatorLength * (line.Items.Count - 1); if (line.AddSeparatorOnStart) { maxLineSize -= separatorLength; } if (line.AddSeparatorOnEnd) { maxLineSize -= separatorLength; } DebugUtil.Assert(maxLineSize <= 0, "THE MAX LINE SIZE IS NOT BIG ENOUGH TO FIT ANY OF THE ITEMS"); for (int i = 0; i < line.Items.Count; i++) { var item = line.Items[i]; var weight = item.WeightOnLine; int size; if (Mathf.Approximately(weight, 0f)) { size = item.Text.Length; // assume natural size } else { size = Mathf.RoundToInt(maxLineSize * weight); } if (line.MaxLineSizeIsForced) { item.Size = size; } else { item.Size = Math.Min(item.Size, size); } line.Items[i] = item; } return(line); }
public static void Setup() { HeaderLine = CreateLine("NAME", "TYPE", "STATUS", HeaderColor, TextModifiers.Bold | TextModifiers.Underline); var pathArgValidation = new CommandLineArgValidationOption(); pathArgValidation.ArgumentName = string.Empty; pathArgValidation.Requirements = ArgRequirement.ValueRequired; ArgValidationOptions = new[] { pathArgValidation }; }
/// <summary> /// Formats a table line accordingly to the given options. /// </summary> public static string FormatTableLine(TextTableLine line) { var builder = new StringBuilder(line.Items.Count * 128); var collumns = line.Items; var separatorModified = ModifyText(line.ItemsSeparator, line.SeparatorModifier); string text; for (int i = 0; i < collumns.Count; i++) { var collumn = collumns[i]; text = FormatTableItem(collumn); bool addSeparator = true; if (string.IsNullOrEmpty(line.ItemsSeparator)) { addSeparator = false; } else if (i == 0 && !line.AddSeparatorOnStart) { addSeparator = false; } if (addSeparator) { text = string.Format("{0}{1}", separatorModified, text); } builder.Append(text); } if (line.AddSeparatorOnEnd) { builder.Append(separatorModified); } var builderText = builder.ToString(); line.FormattedText = builderText; return(builderText); }
/// <summary> /// Shorthand for calling GetIdealTableItemSize and FormatTableLine. /// </summary> public static string FormatLineConsideringWeightsAndSize(TextTableLine line) { line = CalculateIdealTableItemSize(line); return(FormatTableLine(line)); }
/// <summary> /// Shorthand for calling FormatTableLine and returning the length of the result. /// </summary> public static int CalculateTableLineLength(TextTableLine line, bool removeSymbolsLength) { var text = FormatTableLine(line); return(text.Length); }