1
0

ConsoleBase.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.Diagnostics;
  30. using System.Reflection;
  31. using System.Text;
  32. using System.Threading;
  33. using log4net;
  34. namespace OpenSim.Framework.Console
  35. {
  36. public class ConsoleLevel
  37. {
  38. public string m_string;
  39. ConsoleLevel(string v)
  40. {
  41. m_string = v;
  42. }
  43. static public implicit operator ConsoleLevel(string s)
  44. {
  45. return new ConsoleLevel(s);
  46. }
  47. public static string ToString(ConsoleLevel s)
  48. {
  49. return s.m_string;
  50. }
  51. public override string ToString()
  52. {
  53. return m_string;
  54. }
  55. }
  56. public class ConsoleBase : IConsole
  57. {
  58. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  59. protected string prompt = "# ";
  60. public IScene ConsoleScene { get; set; }
  61. public string DefaultPrompt { get; set; }
  62. public ConsoleBase(string defaultPrompt)
  63. {
  64. DefaultPrompt = defaultPrompt;
  65. }
  66. public virtual void LockOutput()
  67. {
  68. }
  69. public virtual void UnlockOutput()
  70. {
  71. }
  72. public virtual void Output(string format)
  73. {
  74. System.Console.WriteLine(format);
  75. }
  76. public virtual void Output(string format, params object[] components)
  77. {
  78. //string level = null;
  79. if (components != null && components.Length > 0)
  80. {
  81. ConsoleLevel cl = components[0] as ConsoleLevel;
  82. if (cl != null)
  83. {
  84. //level = cl.ToString();
  85. if (components.Length > 1)
  86. {
  87. object[] tmp = new object[components.Length - 1];
  88. Array.Copy(components, 1, tmp, 0, components.Length - 1);
  89. components = tmp;
  90. }
  91. else
  92. components = null;
  93. }
  94. }
  95. string text = (components == null || components.Length == 0) ? format : String.Format(format, components);
  96. System.Console.WriteLine(text);
  97. }
  98. public string Prompt(string p)
  99. {
  100. return ReadLine(String.Format("{0}: ", p), false, true);
  101. }
  102. public string Prompt(string p, string def)
  103. {
  104. string ret = ReadLine(String.Format("{0} [{1}]: ", p, def), false, true);
  105. if (ret.Length == 0)
  106. ret = def;
  107. return ret;
  108. }
  109. public string Prompt(string p, List<char> excludedCharacters)
  110. {
  111. bool itisdone = false;
  112. string ret = String.Empty;
  113. while (!itisdone)
  114. {
  115. itisdone = true;
  116. ret = Prompt(p);
  117. foreach (char c in excludedCharacters)
  118. {
  119. if (ret.Contains(c.ToString()))
  120. {
  121. System.Console.WriteLine("The character \"" + c.ToString() + "\" is not permitted.");
  122. itisdone = false;
  123. }
  124. }
  125. }
  126. return ret;
  127. }
  128. public virtual string Prompt(string p, string def, List<char> excludedCharacters, bool echo = true)
  129. {
  130. bool itisdone = false;
  131. string ret = String.Empty;
  132. while (!itisdone)
  133. {
  134. itisdone = true;
  135. if (def == null)
  136. ret = ReadLine(String.Format("{0}: ", p), false, echo);
  137. else
  138. ret = ReadLine(String.Format("{0} [{1}]: ", p, def), false, echo);
  139. if (ret.Length == 0 && def != null)
  140. {
  141. ret = def;
  142. }
  143. else
  144. {
  145. if (excludedCharacters != null)
  146. {
  147. foreach (char c in excludedCharacters)
  148. {
  149. if (ret.Contains(c.ToString()))
  150. {
  151. System.Console.WriteLine("The character \"" + c.ToString() + "\" is not permitted.");
  152. itisdone = false;
  153. }
  154. }
  155. }
  156. }
  157. }
  158. return ret;
  159. }
  160. // Displays a command prompt and returns a default value, user may only enter 1 of 2 options
  161. public virtual string Prompt(string prompt, string defaultresponse, List<string> options)
  162. {
  163. bool itisdone = false;
  164. string optstr = String.Empty;
  165. foreach (string s in options)
  166. optstr += " " + s;
  167. string temp = Prompt(prompt, defaultresponse);
  168. while (itisdone == false)
  169. {
  170. if (options.Contains(temp))
  171. {
  172. itisdone = true;
  173. }
  174. else
  175. {
  176. System.Console.WriteLine("Valid options are" + optstr);
  177. temp = Prompt(prompt, defaultresponse);
  178. }
  179. }
  180. return temp;
  181. }
  182. public virtual string ReadLine(string p, bool isCommand, bool e)
  183. {
  184. System.Console.Write("{0}", p);
  185. string cmdinput = System.Console.ReadLine();
  186. return cmdinput;
  187. }
  188. }
  189. }