Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for progressbar.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4.  
  5. namespace RobvanderWoude
  6. {
  7. 	internal class ProgressBar
  8. 	{
  9. 		static readonly string progver = "1.00";
  10.  
  11.  
  12. 		static int Main( string[] args )
  13. 		{
  14. 			int currentvalue = -1;
  15. 			int maximumvalue = -1;
  16. 			ConsoleColor bgcolor = ConsoleColor.Green;
  17.  
  18.  
  19. 			#region Parse Command Line
  20.  
  21. 			if ( args.Length < 1 || args.Length > 3 )
  22. 			{
  23. 				return ShowHelp( );
  24. 			}
  25.  
  26. 			int numcolors = 0;
  27.  
  28. 			foreach ( string arg in args )
  29. 			{
  30. 				if ( arg == "/?" )
  31. 				{
  32. 					return ShowHelp( );
  33. 				}
  34. 				if ( arg.ToUpper( ) == "/C" )
  35. 				{
  36. 					// /C should be the only argument
  37. 					if ( args.Length != 1 )
  38. 					{
  39. 						return ShowHelp( );
  40. 					}
  41.  
  42. 					// Determine required column width
  43. 					int columnwidt = 0;
  44. 					foreach ( ConsoleColor color in Enum.GetValues( typeof( ConsoleColor ) ) )
  45. 					{
  46. 						columnwidt = Math.Max( columnwidt, Enum.GetName( typeof( ConsoleColor ), color ).Length );
  47. 					}
  48. 					// Display valid colors
  49. 					Console.WriteLine( "Valid colors:" );
  50. 					Console.WriteLine( "=============" );
  51. 					foreach ( ConsoleColor color in Enum.GetValues( typeof( ConsoleColor ) ) )
  52. 					{
  53. 						Console.ForegroundColor = color;
  54. 						// Prvent "invisible" text when foreground color equals background color
  55. 						if ( Console.ForegroundColor == Console.BackgroundColor )
  56. 						{
  57. 							Console.BackgroundColor = (ConsoleColor)( ( (int)color + 7 ) % 16 );
  58. 						}
  59. 						Console.WriteLine( "{0,-" + columnwidt + "}\t{1,2}", Enum.GetName( typeof( ConsoleColor ), color ), (int)color );
  60. 						Console.ResetColor( );
  61. 					}
  62. 					return 0;
  63. 				}
  64. 				else if ( int.TryParse( arg, out int test ) )
  65. 				{
  66. 					if ( currentvalue == -1 && test >= 0 ) // First numeric value is the current value
  67. 					{
  68. 						currentvalue = test;
  69. 					}
  70. 					else if ( maximumvalue == -1 && test > 0 ) // Second numeric value is the maximum value
  71. 					{
  72. 						maximumvalue = test;
  73. 					}
  74. 					else if ( test > -1 && test < 16 ) // Third numeric value could be the color
  75. 					{
  76. 						if ( numcolors > 0 )
  77. 						{
  78. 							return ShowHelp( "Duplicate color argument \"{0}\"", arg );
  79. 						}
  80. 						bgcolor = (ConsoleColor)test;
  81. 						numcolors++;
  82. 					}
  83. 					else // No more numeric values allowed
  84. 					{
  85. 						return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  86. 					}
  87. 				}
  88. 				else
  89. 				{
  90. 					bool validcolor = false;
  91. 					foreach ( ConsoleColor color in Enum.GetValues( typeof( ConsoleColor ) ) )
  92. 					{
  93. 						if ( arg.ToUpper( ) == Enum.GetName( typeof( ConsoleColor ), color ).ToUpper( ) )
  94. 						{
  95. 							if ( numcolors > 0 )
  96. 							{
  97. 								return ShowHelp( "Duplicate color argument \"{0}\"", arg );
  98. 							}
  99. 							bgcolor = color;
  100. 							validcolor = true;
  101. 							numcolors++;
  102. 						}
  103. 					}
  104. 					if ( !validcolor )
  105. 					{
  106. 						return ShowHelp( "Invalid color \"{0}\", use /C switch to show valid colors", arg );
  107. 					}
  108. 				}
  109. 			}
  110.  
  111. 			#endregion Parse Command Line
  112.  
  113.  
  114. 			#region Validate Command Line
  115.  
  116. 			if ( maximumvalue == -1 )
  117. 			{
  118. 				maximumvalue = Console.WindowWidth;
  119. 			}
  120.  
  121. 			if ( currentvalue == maximumvalue + 1 )
  122. 			{
  123. 				currentvalue = maximumvalue;
  124. 			}
  125. 			else if ( currentvalue < 0 || currentvalue > maximumvalue + 1 )
  126. 			{
  127. 				return ShowHelp( "Invalid current value \"{0}\"", currentvalue.ToString( ) );
  128. 			}
  129.  
  130. 			#endregion Validate Command Line
  131.  
  132.  
  133. 			// Move down 1 line and then return to the current one, to make sure we're not at the bottom line of the screen
  134. 			Console.WriteLine( );
  135. 			int linenumber = Console.CursorTop - 1;
  136. 			Console.SetCursorPosition( 0, linenumber );
  137.  
  138. 			// Calculate progress bar length
  139. 			double progress = Math.Min( 1F, (double)currentvalue / (double)maximumvalue );
  140. 			int progressbarlength = (int)( progress * (double)( Console.WindowWidth ) );
  141.  
  142. 			// Display the calculated progress bar
  143. 			Console.BackgroundColor = bgcolor;
  144. 			string progressbar = new string( ' ', progressbarlength );
  145. 			Console.Write( progressbar );
  146. 			Console.ResetColor( );
  147. 			Console.Write( new string( ' ', Console.WindowWidth - progressbarlength ) );
  148.  
  149. 			// Move the cursor back to the start of the line
  150. 			Console.SetCursorPosition( 0, linenumber );
  151.  
  152. 			return 0;
  153. 		}
  154.  
  155.  
  156. 		static int ShowHelp( params string[] errmsg )
  157. 		{
  158. 			#region Error Message
  159.  
  160. 			if ( errmsg.Length > 0 )
  161. 			{
  162. 				List<string> errargs = new List<string>( errmsg );
  163. 				errargs.RemoveAt( 0 );
  164. 				Console.Error.WriteLine( );
  165. 				Console.ForegroundColor = ConsoleColor.Red;
  166. 				Console.Error.Write( "ERROR:\t" );
  167. 				Console.ForegroundColor = ConsoleColor.White;
  168. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  169. 				Console.ResetColor( );
  170. 			}
  171.  
  172. 			#endregion Error Message
  173.  
  174.  
  175. 			#region Help Text
  176.  
  177. 			/*
  178. 			ProgressBar.exe,  Version 1.00
  179. 			Batch tool to display a progress bar
  180.  
  181. 			Usage:    ProgressBar   current  [ maximum ]  [ color ]
  182.  
  183. 			or:       ProgressBar   /C
  184.  
  185. 			Where:    current       is the current value
  186. 			          maximum       is the maximum value (default: console width)
  187. 			          color         is the progress bar color, either by name or
  188. 			                        by number (default: Green)
  189. 			          /C            list valid colors by name and number
  190.  
  191. 			Notes:    The program writes a progress bar, the length of which is determined
  192. 			          by the formula: current / maximum * console width
  193. 			          Return code -1 in case of (command line) errors, otherwise 0.
  194.  
  195. 			Written by Rob van der Woude
  196. 			https://www.robvanderwoude.com
  197. 			*/
  198.  
  199. 			#endregion Help Text
  200.  
  201.  
  202. 			#region Display Help Text
  203.  
  204. 			Console.Error.WriteLine( );
  205.  
  206. 			Console.Error.WriteLine( "ProgressBar.exe,  Version {0}", progver );
  207.  
  208. 			Console.Error.WriteLine( "Batch tool to display a progress bar" );
  209.  
  210. 			Console.Error.WriteLine( );
  211.  
  212. 			Console.Error.Write( "Usage:    " );
  213. 			Console.ForegroundColor = ConsoleColor.White;
  214. 			Console.Error.WriteLine( "ProgressBar   current  [ maximum ]  [ color ]" );
  215. 			Console.ResetColor( );
  216.  
  217. 			Console.Error.WriteLine( );
  218.  
  219. 			Console.Error.Write( "or:       " );
  220. 			Console.ForegroundColor = ConsoleColor.White;
  221. 			Console.Error.WriteLine( "ProgressBar   /C" );
  222. 			Console.ResetColor( );
  223.  
  224. 			Console.Error.WriteLine( );
  225.  
  226. 			Console.Error.Write( "Where:    " );
  227. 			Console.ForegroundColor = ConsoleColor.White;
  228. 			Console.Error.Write( "current" );
  229. 			Console.ResetColor( );
  230. 			Console.Error.WriteLine( "       is the current value" );
  231.  
  232. 			Console.ForegroundColor = ConsoleColor.White;
  233. 			Console.Error.Write( "          maximum" );
  234. 			Console.ResetColor( );
  235. 			Console.Error.WriteLine( "       is the maximum value (default: console width)" );
  236.  
  237. 			Console.ForegroundColor = ConsoleColor.White;
  238. 			Console.Error.Write( "          color" );
  239. 			Console.ResetColor( );
  240. 			Console.Error.WriteLine( "         is the progress bar color, either by name or" );
  241.  
  242. 			Console.Error.WriteLine( "                        by number (default: Green)" );
  243.  
  244. 			Console.ForegroundColor = ConsoleColor.White;
  245. 			Console.Error.Write( "          /C" );
  246. 			Console.ResetColor( );
  247. 			Console.Error.WriteLine( "            list valid colors by name and number" );
  248.  
  249. 			Console.Error.WriteLine( );
  250.  
  251. 			Console.Error.WriteLine( "Notes:    The program writes a progress bar, the length of which is determined" );
  252.  
  253. 			Console.Error.WriteLine( "          by the formula: current / maximum * console width" );
  254.  
  255. 			Console.Error.WriteLine( "          Return code -1 in case of (command line) errors, otherwise 0." );
  256.  
  257. 			Console.Error.WriteLine( );
  258.  
  259. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  260.  
  261. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  262.  
  263. 			#endregion Display Help Text
  264.  
  265.  
  266. 			return -1;
  267. 		}
  268. 	}
  269. }

page last modified: 2022-10-20