/CatalogueRaisonne/CRShared/Source/Threads/ConsoleUIThread.cs

https://github.com/bretambrose/CatalogueRaisonnePublic · C# · 110 lines · 68 code · 15 blank · 27 comment · 11 complexity · d27a6b53c700a9dcc30ba7c7c4881c5e MD5 · raw file

  1. /*
  2. ConsoleUIThread.cs
  3. (c) Copyright 2010-2011, Bret Ambrose (mailto:bretambrose@gmail.com).
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. namespace CRShared
  18. {
  19. public class CConsoleUIThread : CUIThreadBase
  20. {
  21. // Construction
  22. protected CConsoleUIThread() :
  23. base()
  24. {
  25. m_BaseInstance = this;
  26. }
  27. // Methods
  28. // Protected interface and overrides
  29. protected override void Service( long current_time )
  30. {
  31. base.Service( current_time );
  32. Read_Input();
  33. m_CompleteInputLines.Apply( input_line => Process_Console_Input( input_line ) );
  34. m_CompleteInputLines.Clear();
  35. }
  36. protected virtual bool Process_Console_Input( string input_string )
  37. {
  38. if ( input_string.Length == 0 )
  39. {
  40. return false;
  41. }
  42. if ( input_string[ 0 ] == '/' )
  43. {
  44. CUIInputSlashCommandRequest request = new CUIInputSlashCommandRequest( input_string );
  45. Add_Input_Request( request );
  46. return true;
  47. }
  48. return false;
  49. }
  50. // Private interface
  51. private void Read_Input()
  52. {
  53. while ( Console.KeyAvailable )
  54. {
  55. ConsoleKeyInfo key_info = Console.ReadKey();
  56. if ( key_info.Key == ConsoleKey.Enter )
  57. {
  58. m_CompleteInputLines.Add( m_CurrentInputLine );
  59. Console.WriteLine( m_CurrentInputLine );
  60. m_CurrentInputLine = "";
  61. }
  62. else if ( key_info.Key == ConsoleKey.Backspace )
  63. {
  64. if ( m_CurrentInputLine.Length > 0 )
  65. {
  66. m_CurrentInputLine = m_CurrentInputLine.Substring( 0, m_CurrentInputLine.Length - 1 );
  67. }
  68. }
  69. else
  70. {
  71. m_CurrentInputLine += key_info.KeyChar;
  72. }
  73. }
  74. }
  75. // UI Notice Handlers
  76. [GenericHandler]
  77. private void Handle_Text_Output_Notification( CUITextOutputNotification notice )
  78. {
  79. CLog.Log( ELoggingChannel.UI, ELogLevel.Low, String.Format( "Console output: {0}", notice.Text ) );
  80. Console.WriteLine( notice.Text );
  81. }
  82. // Properties
  83. public static CConsoleUIThread BaseInstance { get { return m_BaseInstance; } }
  84. // Fields
  85. private List< string > m_CompleteInputLines = new List< string >();
  86. private string m_CurrentInputLine = "";
  87. private static CConsoleUIThread m_BaseInstance = null;
  88. }
  89. }