1
0

ConsoleDisplayTable.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Linq;
  30. using System.Text;
  31. namespace OpenSim.Framework.Console
  32. {
  33. /// <summary>
  34. /// Used to generated a formatted table for the console.
  35. /// </summary>
  36. /// <remarks>
  37. /// Currently subject to change. If you use this, be prepared to change your code when this class changes.
  38. /// </remarks>
  39. public class ConsoleDisplayTable
  40. {
  41. /// <summary>
  42. /// Default number of spaces between table columns.
  43. /// </summary>
  44. public const int DefaultTableSpacing = 2;
  45. /// <summary>
  46. /// Table columns.
  47. /// </summary>
  48. public List<ConsoleDisplayTableColumn> Columns { get; private set; }
  49. /// <summary>
  50. /// Table rows
  51. /// </summary>
  52. public List<ConsoleDisplayTableRow> Rows { get; private set; }
  53. /// <summary>
  54. /// Number of spaces to indent the whole table.
  55. /// </summary>
  56. public int Indent { get; set; }
  57. /// <summary>
  58. /// Spacing between table columns
  59. /// </summary>
  60. public int TableSpacing { get; set; }
  61. public ConsoleDisplayTable()
  62. {
  63. TableSpacing = DefaultTableSpacing;
  64. Columns = new List<ConsoleDisplayTableColumn>();
  65. Rows = new List<ConsoleDisplayTableRow>();
  66. }
  67. public override string ToString()
  68. {
  69. StringBuilder sb = new StringBuilder();
  70. AddToStringBuilder(sb);
  71. return sb.ToString();
  72. }
  73. public void AddColumn(string name, int width)
  74. {
  75. Columns.Add(new ConsoleDisplayTableColumn(name, width));
  76. }
  77. public void AddRow(params object[] cells)
  78. {
  79. Rows.Add(new ConsoleDisplayTableRow(cells));
  80. }
  81. public void AddToStringBuilder(StringBuilder sb)
  82. {
  83. string formatString = GetFormatString();
  84. // System.Console.WriteLine("FORMAT STRING [{0}]", formatString);
  85. // columns
  86. sb.AppendFormat(formatString, Columns.ConvertAll(c => c.Header).ToArray());
  87. // rows
  88. foreach (ConsoleDisplayTableRow row in Rows)
  89. sb.AppendFormat(formatString, row.Cells.ToArray());
  90. }
  91. /// <summary>
  92. /// Gets the format string for the table.
  93. /// </summary>
  94. private string GetFormatString()
  95. {
  96. StringBuilder formatSb = new StringBuilder();
  97. formatSb.Append(' ', Indent);
  98. for (int i = 0; i < Columns.Count; i++)
  99. {
  100. if (i != 0)
  101. formatSb.Append(' ', TableSpacing);
  102. // Can only do left formatting for now
  103. formatSb.AppendFormat("{{{0},-{1}}}", i, Columns[i].Width);
  104. }
  105. formatSb.Append('\n');
  106. return formatSb.ToString();
  107. }
  108. }
  109. public struct ConsoleDisplayTableColumn
  110. {
  111. public string Header { get; set; }
  112. public int Width { get; set; }
  113. public ConsoleDisplayTableColumn(string header, int width) : this()
  114. {
  115. Header = header;
  116. Width = width;
  117. }
  118. }
  119. public struct ConsoleDisplayTableRow
  120. {
  121. public List<object> Cells { get; private set; }
  122. public ConsoleDisplayTableRow(List<object> cells) : this()
  123. {
  124. Cells = cells;
  125. }
  126. public ConsoleDisplayTableRow(params object[] cells) : this()
  127. {
  128. Cells = new List<object>(cells);
  129. }
  130. }
  131. }