Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for hideinput.cs

(view source code of hideinput.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4.  
  5. namespace RobvanderWoude
  6. {
  7. 	class HideInput
  8. 	{
  9. 		static string progver = "1.01";
  10.  
  11.  
  12. 		static int Main( string[] args )
  13. 		{
  14. 			try
  15. 			{
  16. 				bool clearscreen = false;
  17.  
  18. 				if ( args.Length > 1 )
  19. 				{
  20. 					return ShowHelp( "Too many command line arguments" );
  21. 				}
  22. 				if ( args.Length == 1 )
  23. 				{
  24. 					switch ( args[0].ToUpper( ) )
  25. 					{
  26. 						case "/C":
  27. 							clearscreen = true;
  28. 							break;
  29. 						case "/?":
  30. 							return ShowHelp( );
  31. 						default:
  32. 							return ShowHelp( "Invalid command line argument \"{0}\"", args[0] );
  33. 					}
  34. 				}
  35.  
  36. 				// Set console foreground color to background color to hide what's being typed
  37. 				ConsoleColor color = Console.ForegroundColor;
  38. 				Console.ForegroundColor = Console.BackgroundColor;
  39.  
  40. 				// Read 1 line of input from the console
  41. 				string input = Console.ReadLine( );
  42.  
  43. 				// Restore the original console foreground color
  44. 				Console.ForegroundColor = color;
  45.  
  46. 				if ( clearscreen )
  47. 				{
  48. 					// Clear the screen
  49. 					Console.Clear( );
  50. 				}
  51.  
  52. 				// Display the input - which should be redirected for this program to be of any use
  53. 				Console.WriteLine( input );
  54.  
  55. 				// Returncode 0 for success, or 2 if the input was empty or whitespace only
  56. 				if ( string.IsNullOrWhiteSpace( input ) )
  57. 				{
  58. 					return 2;
  59. 				}
  60. 				else
  61. 				{
  62. 					return 0;
  63. 				}
  64. 			}
  65. 			catch ( Exception e )
  66. 			{
  67. 				return ShowHelp( e.Message );
  68. 			}
  69. 		}
  70.  
  71. 		public static int ShowHelp( params string[] errmsg )
  72. 		{
  73. 			#region Help Text
  74.  
  75. 			/*
  76. 			HideInput,  Version 1.01
  77. 			Batch utility to read 1 line of input while hiding what's being typed, by
  78. 			temporarily setting the console foreground color equal to its background color
  79.  
  80. 			Usage:  FOR /F "tokens=*" %%A IN ('HIDEINPUT') DO SET password=%%A
  81. 			   or:  FOR /F "tokens=*" %%A IN ('HIDEINPUT /C') DO SET password=%%A
  82.  
  83. 			Where:  /C  Clears the screen to remove what's typed from the screen buffer
  84.  
  85. 			Notes:  Even though invisible while typing, typed input will be "visible" in
  86. 			        the screen buffer. The "invisible" text can be made visible with
  87. 			        Windows' own COLOR command, or by any program that can access the
  88. 			        screen buffer. To prevent this, use the program's /C switch or
  89. 			        Windows' own CLS command to clear the screen and screen buffer.
  90. 			        Return code ("errorlevel") is 0 if valid input is received, 1 in case
  91. 			        of (command line) errors, 2 if input is empty or whitespace only.
  92.  
  93. 			Written by Rob van der Woude
  94. 			http://www.robvanderwoude.com
  95. 			*/
  96.  
  97. 			#endregion Help Text
  98.  
  99.  
  100. 			#region Error Message
  101.  
  102. 			if ( errmsg.Length > 0 )
  103. 			{
  104. 				List<string> errargs = new List<string>( errmsg );
  105. 				errargs.RemoveAt( 0 );
  106. 				Console.Error.WriteLine( );
  107. 				Console.ForegroundColor = ConsoleColor.Red;
  108. 				Console.Error.Write( "ERROR:\t" );
  109. 				Console.ForegroundColor = ConsoleColor.White;
  110. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  111. 				Console.ResetColor( );
  112. 			}
  113.  
  114. 			#endregion Error Message
  115.  
  116.  
  117. 			#region Display Help Text
  118.  
  119. 			Console.Error.WriteLine( );
  120.  
  121. 			Console.Error.WriteLine( "HideInput,  Version {0}", progver );
  122.  
  123. 			Console.Error.WriteLine( "Batch utility to read 1 line of input while hiding what's being typed, by" );
  124.  
  125. 			Console.Error.WriteLine( "temporarily setting the console foreground color equal to its background color" );
  126.  
  127. 			Console.Error.WriteLine( );
  128.  
  129. 			Console.Error.Write( "Usage:  FOR /F \"tokens=*\" %%A IN ('" );
  130. 			Console.ForegroundColor = ConsoleColor.White;
  131. 			Console.Error.Write( "HIDEINPUT" );
  132. 			Console.ResetColor( );
  133. 			Console.Error.WriteLine( "') DO SET password=%%A" );
  134.  
  135. 			Console.Error.Write( "   or:  FOR /F \"tokens=*\" %%A IN ('" );
  136. 			Console.ForegroundColor = ConsoleColor.White;
  137. 			Console.Error.Write( "HIDEINPUT /C" );
  138. 			Console.ResetColor( );
  139. 			Console.Error.WriteLine( "') DO SET password=%%A" );
  140.  
  141. 			Console.Error.WriteLine( );
  142.  
  143. 			Console.Error.Write( "Where:  " );
  144. 			Console.ForegroundColor = ConsoleColor.White;
  145. 			Console.Error.Write( "/C  C" );
  146. 			Console.ResetColor( );
  147. 			Console.Error.WriteLine( "lears the screen to remove what's typed from the screen buffer" );
  148.  
  149. 			Console.Error.WriteLine( );
  150.  
  151. 			Console.Error.WriteLine( "Notes:  Even though invisible while typing, typed input will be \"visible\" in" );
  152.  
  153. 			Console.Error.WriteLine( "        the screen buffer. The \"invisible\" text can be made visible with" );
  154.  
  155. 			Console.Error.Write( "        Windows' own " );
  156. 			Console.ForegroundColor = ConsoleColor.White;
  157. 			Console.Error.Write( "COLOR" );
  158. 			Console.ResetColor( );
  159. 			Console.Error.WriteLine( " command, or by any program that can access the" );
  160.  
  161. 			Console.Error.Write( "        screen buffer. To prevent this, use the program's " );
  162. 			Console.ForegroundColor = ConsoleColor.White;
  163. 			Console.Error.Write( "/C" );
  164. 			Console.ResetColor( );
  165. 			Console.Error.WriteLine( " switch or" );
  166.  
  167. 			Console.Error.Write( "        Windows' own " );
  168. 			Console.ForegroundColor = ConsoleColor.White;
  169. 			Console.Error.Write( "CLS" );
  170. 			Console.ResetColor( );
  171. 			Console.Error.WriteLine( " command to clear the screen and screen buffer." );
  172.  
  173. 			Console.Error.WriteLine( "        Return code (\"errorlevel\") is 0 if valid input is received, 1 in case" );
  174.  
  175. 			Console.Error.WriteLine( "        of (command line) errors, 2 if input is empty or whitespace only." );
  176.  
  177. 			Console.Error.WriteLine( );
  178.  
  179. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  180.  
  181. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  182.  
  183. 			#endregion Display Help Text
  184.  
  185.  
  186. 			return 1;
  187. 		}
  188.  
  189. 	}
  190. }
  191.  

page last uploaded: 2015-12-04, 16:51