1
0

ConsoleDisplayList.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 ConsoleDisplayList
  40. {
  41. /// <summary>
  42. /// The default divider between key and value for a list item.
  43. /// </summary>
  44. public const string DefaultKeyValueDivider = " : ";
  45. /// <summary>
  46. /// The divider used between key and value for a list item.
  47. /// </summary>
  48. public string KeyValueDivider { get; set; }
  49. /// <summary>
  50. /// Table rows
  51. /// </summary>
  52. public List<KeyValuePair<string, string>> Rows { get; private set; }
  53. /// <summary>
  54. /// Number of spaces to indent the list.
  55. /// </summary>
  56. public int Indent { get; set; }
  57. public ConsoleDisplayList()
  58. {
  59. Rows = new List<KeyValuePair<string, string>>();
  60. KeyValueDivider = DefaultKeyValueDivider;
  61. }
  62. public override string ToString()
  63. {
  64. StringBuilder sb = new StringBuilder();
  65. AddToStringBuilder(sb);
  66. return sb.ToString();
  67. }
  68. public void AddToStringBuilder(StringBuilder sb)
  69. {
  70. string formatString = GetFormatString();
  71. // System.Console.WriteLine("FORMAT STRING [{0}]", formatString);
  72. // rows
  73. foreach (KeyValuePair<string, string> row in Rows)
  74. sb.AppendFormat(formatString, row.Key, row.Value);
  75. }
  76. /// <summary>
  77. /// Gets the format string for the table.
  78. /// </summary>
  79. private string GetFormatString()
  80. {
  81. StringBuilder formatSb = new StringBuilder();
  82. int longestKey = -1;
  83. foreach (KeyValuePair<string, string> row in Rows)
  84. if (row.Key.Length > longestKey)
  85. longestKey = row.Key.Length;
  86. formatSb.Append(' ', Indent);
  87. // Can only do left formatting for now
  88. formatSb.AppendFormat("{{0,-{0}}}{1}{{1}}\n", longestKey, KeyValueDivider);
  89. return formatSb.ToString();
  90. }
  91. public void AddRow(object key, object value)
  92. {
  93. Rows.Add(new KeyValuePair<string, string>(key.ToString(), value.ToString()));
  94. }
  95. }
  96. }