ConsoleBase.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 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. if (components[0] == null || components[0] is ConsoleLevel)
  82. {
  83. if (components[0] is ConsoleLevel)
  84. level = ((ConsoleLevel)components[0]).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;
  96. if (components == null || components.Length == 0)
  97. text = format;
  98. else
  99. text = String.Format(format, components);
  100. System.Console.WriteLine(text);
  101. }
  102. public string Prompt(string p)
  103. {
  104. return ReadLine(String.Format("{0}: ", p), false, true);
  105. }
  106. public string Prompt(string p, string def)
  107. {
  108. string ret = ReadLine(String.Format("{0} [{1}]: ", p, def), false, true);
  109. if (ret == String.Empty)
  110. ret = def;
  111. return ret;
  112. }
  113. public string Prompt(string p, List<char> excludedCharacters)
  114. {
  115. bool itisdone = false;
  116. string ret = String.Empty;
  117. while (!itisdone)
  118. {
  119. itisdone = true;
  120. ret = Prompt(p);
  121. foreach (char c in excludedCharacters)
  122. {
  123. if (ret.Contains(c.ToString()))
  124. {
  125. System.Console.WriteLine("The character \"" + c.ToString() + "\" is not permitted.");
  126. itisdone = false;
  127. }
  128. }
  129. }
  130. return ret;
  131. }
  132. public virtual string Prompt(string p, string def, List<char> excludedCharacters, bool echo = true)
  133. {
  134. bool itisdone = false;
  135. string ret = String.Empty;
  136. while (!itisdone)
  137. {
  138. itisdone = true;
  139. if (def == null)
  140. ret = ReadLine(String.Format("{0}: ", p), false, echo);
  141. else
  142. ret = ReadLine(String.Format("{0} [{1}]: ", p, def), false, echo);
  143. if (ret == String.Empty && def != null)
  144. {
  145. ret = def;
  146. }
  147. else
  148. {
  149. if (excludedCharacters != null)
  150. {
  151. foreach (char c in excludedCharacters)
  152. {
  153. if (ret.Contains(c.ToString()))
  154. {
  155. System.Console.WriteLine("The character \"" + c.ToString() + "\" is not permitted.");
  156. itisdone = false;
  157. }
  158. }
  159. }
  160. }
  161. }
  162. return ret;
  163. }
  164. // Displays a command prompt and returns a default value, user may only enter 1 of 2 options
  165. public virtual string Prompt(string prompt, string defaultresponse, List<string> options)
  166. {
  167. bool itisdone = false;
  168. string optstr = String.Empty;
  169. foreach (string s in options)
  170. optstr += " " + s;
  171. string temp = Prompt(prompt, defaultresponse);
  172. while (itisdone == false)
  173. {
  174. if (options.Contains(temp))
  175. {
  176. itisdone = true;
  177. }
  178. else
  179. {
  180. System.Console.WriteLine("Valid options are" + optstr);
  181. temp = Prompt(prompt, defaultresponse);
  182. }
  183. }
  184. return temp;
  185. }
  186. public virtual string ReadLine(string p, bool isCommand, bool e)
  187. {
  188. System.Console.Write("{0}", p);
  189. string cmdinput = System.Console.ReadLine();
  190. return cmdinput;
  191. }
  192. }
  193. }