Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for countdown.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5.  
  6. namespace RobvanderWoude
  7. {
  8. 	class CountDown
  9. 	{
  10. 		static string progver = "1.02";
  11.  
  12.  
  13. 		static int Main( string[] args )
  14. 		{
  15. 			bool returnremaining = false;
  16. 			int seconds = 0;
  17.  
  18.  
  19. 			#region Parse Command Line
  20.  
  21. 			if ( args.Length == 0 || args[0] == "/?" )
  22. 			{
  23. 				return ShowHelp( );
  24. 			}
  25.  
  26. 			if ( args.Length > 2 )
  27. 			{
  28. 				return ShowHelp( "Too many command line arguments" );
  29. 			}
  30.  
  31. 			try
  32. 			{
  33. 				seconds = Convert.ToInt32( args[0] );
  34. 			}
  35. 			catch ( Exception )
  36. 			{
  37. 				return ShowHelp( "Invalid number of seconds ({0})", args[0] );
  38. 			}
  39.  
  40. 			if ( seconds < 1 || seconds > 86400 )
  41. 			{
  42. 				return ShowHelp( "Specified number of seconds ({0}) outside allowed range", args[0] );
  43. 			}
  44.  
  45. 			if ( args.Length == 2 )
  46. 			{
  47. 				if ( args[1].ToUpper( ) == "/R" )
  48. 				{
  49. 					returnremaining = true;
  50. 				}
  51. 				else if ( args[1] == "/?" )
  52. 				{
  53. 					return ShowHelp( );
  54. 				}
  55. 				else
  56. 				{
  57. 					return ShowHelp( "Invalid command line argument \"{0}\"", args[1] );
  58. 				}
  59. 			}
  60.  
  61. 			#endregion Parse Command Line
  62.  
  63.  
  64. 			// Temporarily change console properties
  65. 			Console.CursorVisible = false;
  66. 			Console.TreatControlCAsInput = true;
  67.  
  68. 			// Determine the number of digits
  69. 			int length = seconds.ToString( ).Length;
  70.  
  71. 			// Clear space on screen for countdown counter value
  72. 			Console.Write( new String( ' ', length ) );
  73.  
  74. 			// Start countdown
  75. 			int countdown = seconds;
  76. 			while ( !Console.KeyAvailable && countdown > 0 )
  77. 			{
  78. 				// Wipe previous countdown counter value from screen
  79. 				Console.Write( new String( '\b', length ) );
  80. 				Console.Write( new String( ' ', length ) );
  81. 				Console.Write( new String( '\b', length ) );
  82. 				Console.Write( "{0," + length + "}", countdown );
  83. 				countdown -= 1; // decrement countdown counter
  84. 				Thread.Sleep( 1000 ); // wait 1 second
  85. 			}
  86. 			// Wipe last countdown counter value from screen
  87. 			Console.Write( new String( '\b', length ) );
  88. 			Console.Write( new String( ' ', length ) );
  89. 			Console.Write( new String( '\b', length ) );
  90.  
  91. 			// Restore console properties
  92. 			Console.CursorVisible = true;
  93. 			Console.TreatControlCAsInput = false;
  94.  
  95. 			// If a key is pressed to interrupt countdown, remove it from keyboard buffer
  96. 			if ( countdown > 0 && Console.KeyAvailable )
  97. 			{
  98. 				Console.ReadKey( true );
  99. 			}
  100.  
  101. 			// Return code depends on command line switch and on countdown completion
  102. 			if ( returnremaining )
  103. 			{
  104. 				return countdown; // remaining number of seconds (requires /R)
  105. 			}
  106. 			else if ( countdown == 0 )
  107. 			{
  108. 				return 0; // countdown completed
  109. 			}
  110. 			else
  111. 			{
  112. 				return 2; // countdown interrupted (not with /R)
  113. 			}
  114. 		}
  115.  
  116.  
  117. 		static int ShowHelp( params string[] errmsg )
  118. 		{
  119. 			#region Help Text
  120.  
  121. 			/*
  122. 			CountDown,  Version 1.02
  123. 			Count down for the specified number of seconds or until a key is pressed
  124.  
  125. 			Usage:  COUNTDOWN  seconds  [ /R ]
  126.  
  127. 			Where:  seconds    is in the range of 1 .. 86400 (1 second .. 24 hours)
  128. 			        /R         return code equals the number of Remaining seconds
  129. 			                   (default: return code 0 = OK, 1 = errors, 2 = key pressed)
  130.  
  131. 			Written by Rob van der Woude
  132. 			http://www.robvanderwoude.com
  133. 			*/
  134.  
  135. 			#endregion Help Text
  136.  
  137.  
  138. 			#region Error Message
  139.  
  140. 			if ( errmsg.Length > 0 )
  141. 			{
  142. 				List<string> errargs = new List<string>( errmsg );
  143. 				errargs.RemoveAt( 0 );
  144. 				Console.Error.WriteLine( );
  145. 				Console.ForegroundColor = ConsoleColor.Red;
  146. 				Console.Error.Write( "ERROR:\t" );
  147. 				Console.ForegroundColor = ConsoleColor.White;
  148. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  149. 				Console.ResetColor( );
  150. 			}
  151.  
  152. 			#endregion Error Message
  153.  
  154.  
  155. 			#region Display Help Text
  156.  
  157. 			Console.Error.WriteLine( );
  158.  
  159. 			Console.Error.WriteLine( "CountDown,  Version {0}", progver );
  160.  
  161. 			Console.Error.WriteLine( "Count down for the specified number of seconds or until a key is pressed" );
  162.  
  163. 			Console.Error.WriteLine( );
  164.  
  165. 			Console.Error.Write( "Usage:  " );
  166. 			Console.ForegroundColor = ConsoleColor.White;
  167. 			Console.Error.WriteLine( "COUNTDOWN  seconds  [ /R ]" );
  168. 			Console.ResetColor( );
  169.  
  170. 			Console.Error.WriteLine( );
  171.  
  172. 			Console.Error.Write( "Where:  " );
  173. 			Console.ForegroundColor = ConsoleColor.White;
  174. 			Console.Error.Write( "seconds" );
  175. 			Console.ResetColor( );
  176. 			Console.Error.WriteLine( "    is in the range of 1 .. 86400 (1 second .. 24 hours)" );
  177.  
  178. 			Console.ForegroundColor = ConsoleColor.White;
  179. 			Console.Error.Write( "        /R" );
  180. 			Console.ResetColor( );
  181. 			Console.Error.Write( "         return code equals the number of " );
  182. 			Console.ForegroundColor = ConsoleColor.White;
  183. 			Console.Error.Write( "R" );
  184. 			Console.ResetColor( );
  185. 			Console.Error.WriteLine( "emaining seconds" );
  186.  
  187. 			Console.Error.WriteLine( "                   (default: return code 0 = OK, 1 = errors, 2 = key pressed)" );
  188.  
  189. 			Console.Error.WriteLine( );
  190.  
  191. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  192.  
  193. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  194.  
  195. 			#endregion Display Help Text
  196.  
  197. 			return 1;
  198. 		}
  199. 	}
  200. }
  201.  

page last uploaded: 2016-12-15, 11:20