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. using System.Threading;
  4.  
  5.  
  6. namespace RobvanderWoude
  7. {
  8. 	internal class ProgressBar
  9. 	{
  10. 		static readonly string progver = "2.00";
  11.  
  12.  
  13. 		static int Main( string[] args )
  14. 		{
  15. 			#region Initialize Variables
  16.  
  17. 			int currentvalue = 0;
  18. 			bool currentset = false;
  19. 			int minimumvalue = 0;
  20. 			bool minimumset = false;			
  21. 			int maximumvalue = 100;
  22. 			bool maximumset = false;
  23. 			int parsedargs = 0;
  24. 			ConsoleColor poscolor = ConsoleColor.Green;
  25. 			bool poscolorset = false;
  26. 			ConsoleColor negcolor = ConsoleColor.Green;
  27. 			bool negcolorset = false;
  28. 			bool linearscale = false;
  29. 			bool writescale = true;
  30. 			bool debug = false;
  31. #if DEBUG
  32. 			debug = true;
  33. #endif
  34. 			#endregion Initialize Variables
  35.  
  36.  
  37. 			#region Parse Command Line
  38.  
  39. 			if ( args.Length == 0 )
  40. 			{
  41. 				return ShowHelp( );
  42. 			}
  43.  
  44. 			foreach ( string arg in args )
  45. 			{
  46. 				if ( arg == "/?" )
  47. 				{
  48. 					return ShowHelp( );
  49. 				}
  50. 				if ( arg.ToUpper( ) == "/C" )
  51. 				{
  52. 					// /C should be the only argument
  53. 					if ( args.Length != 1 )
  54. 					{
  55. 						return ShowHelp( );
  56. 					}
  57. 					// Display valid colors
  58. 					Console.WriteLine( "Valid colors:" );
  59. 					Console.WriteLine( "=============" );
  60. 					foreach ( ConsoleColor color in Enum.GetValues( typeof( ConsoleColor ) ) )
  61. 					{
  62. 						Console.ForegroundColor = color;
  63. 						// Prevent "invisible" text when foreground color equals background color
  64. 						if ( Math.Abs( Console.ForegroundColor - Console.BackgroundColor ) < 2 )
  65. 						{
  66. 							Console.BackgroundColor = (ConsoleColor)( ( (int)color + 7 ) % 16 );
  67. 						}
  68. 						Console.Write( "{0,-13}", Enum.GetName( typeof( ConsoleColor ), color ) );
  69. 						Console.ResetColor( );
  70. 						Console.WriteLine( );
  71. 					}
  72. 					return 0;
  73. 				}
  74. 				else if ( arg.ToUpper( ) == "/D" )
  75. 				{
  76. 					debug = true;
  77. 					parsedargs++;
  78. 				}
  79. 				else if ( arg.ToUpper( ) == "/L" )
  80. 				{
  81. 					linearscale = true;
  82. 					parsedargs++;
  83. 				}
  84. 				else if ( arg.ToUpper( ) == "/NS" )
  85. 				{
  86. 					writescale = false;
  87. 					parsedargs++;
  88. 				}
  89. 				else if ( int.TryParse( arg, out int test ) )
  90. 				{
  91. 					if ( !currentset ) // First numeric value is the mandatory current value
  92. 					{
  93. 						currentvalue = test;
  94. 						currentset = true;
  95. 					}
  96. 					else if ( !minimumset ) // Second numeric value can be either the minimum or maximum value
  97. 					{
  98. 						minimumvalue = test;
  99. 						minimumset = true;
  100. 					}
  101. 					else if ( !maximumset ) // Third numeric value must be the maximum value
  102. 					{
  103. 						maximumvalue = test;
  104. 						maximumset = true;
  105. 					}
  106. 					else // No more numeric values allowed
  107. 					{
  108. 						return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  109. 					}
  110. 					parsedargs++;
  111. 				}
  112. 				else
  113. 				{
  114. 					bool validcolor = false;
  115. 					foreach ( ConsoleColor color in Enum.GetValues( typeof( ConsoleColor ) ) )
  116. 					{
  117. 						if ( arg.ToUpper( ) == Enum.GetName( typeof( ConsoleColor ), color ).ToUpper( ) )
  118. 						{
  119. 							if ( !poscolorset )
  120. 							{
  121. 								poscolor = color;
  122. 								poscolorset = true;
  123. 								validcolor = true;
  124. 								parsedargs++;
  125. 							}
  126. 							else if ( !negcolorset )
  127. 							{
  128. 								negcolor = color;
  129. 								negcolorset = true;
  130. 								validcolor = true;
  131. 								parsedargs++;
  132. 							}
  133. 							else
  134. 							{
  135. 								validcolor = false;
  136. 							}
  137. 						}
  138. 					}
  139. 					if ( !validcolor )
  140. 					{
  141. 						return ShowHelp( "Invalid color \"{0}\", use /C switch to show valid colors", arg );
  142. 					}
  143. 				}
  144. 			}
  145.  
  146. 			#endregion Parse Command Line
  147.  
  148.  
  149. 			#region Validate Command Line
  150.  
  151. 			if ( !maximumset )
  152. 			{
  153. 				if ( !minimumset )
  154. 				{
  155. 					maximumvalue = 100;
  156. 					maximumset = false;
  157. 					minimumvalue = 0;
  158. 					minimumset = false;
  159. 				}
  160. 				else
  161. 				{
  162. 					maximumvalue = minimumvalue;
  163. 					maximumset = true;
  164. 					minimumvalue = 0;
  165. 					minimumset = false;
  166. 				}
  167. 			}
  168.  
  169. 			if ( minimumvalue * maximumvalue >= 0 )
  170. 			{
  171. 				if ( negcolorset )
  172. 				{
  173. 					return ShowHelp( "Specify a second color only if minimum < 0 and maximum > 0" );
  174. 				}
  175. 				linearscale = true;
  176. 			}
  177.  
  178. 			if ( currentvalue == maximumvalue + 1 )
  179. 			{
  180. 				currentvalue = maximumvalue;
  181. 			}
  182. 			else if ( currentvalue < minimumvalue || currentvalue > maximumvalue + 1 )
  183. 			{
  184. 				return ShowHelp( "Invalid current value \"{0}\"", currentvalue.ToString( ) );
  185. 			}
  186.  
  187. 			if ( parsedargs != args.Length )
  188. 			{
  189. 				return ShowHelp( "Invalid command line, most likely caused by duplicate arguments" );
  190. 			}
  191.  
  192. 			#endregion Validate Command Line
  193.  
  194.  
  195. 			Console.ResetColor( );
  196.  
  197.  
  198. 			#region Scale
  199.  
  200. 			if ( writescale )
  201. 			{
  202. 				string scale;
  203. 				if ( minimumvalue < 0 && maximumvalue > 0 )
  204. 				{
  205. 					int zeropos = (int)Math.Floor( Console.WindowWidth * (double)-minimumvalue / ( maximumvalue - minimumvalue ) );
  206. 					scale = string.Format( "{0}{1}0{2}{3}", minimumvalue, new string( ' ', zeropos - minimumvalue.ToString( ).Length ), new string( ' ', Console.WindowWidth - zeropos - minimumvalue.ToString( ).Length ), maximumvalue );
  207. 				}
  208. 				else
  209. 				{
  210. 					scale = string.Format( "{0}{1}{2}", minimumvalue, new string( ' ', Console.WindowWidth - minimumvalue.ToString( ).Length - maximumvalue.ToString( ).Length  ), maximumvalue );
  211. 				}
  212. 				Console.ForegroundColor = poscolor;
  213. 				Console.WriteLine( scale );
  214. 				Console.ResetColor( );
  215. 				if ( debug )
  216. 				{
  217. 					Thread.Sleep( 1000 );
  218. 				}
  219. 			}
  220.  
  221. 			#endregion Scale
  222.  
  223.  
  224. 			// Move down 1 line and then return to the one above current one, to make sure we're not at the bottom line of the screen
  225. 			Console.WriteLine( );
  226. 			int linenumber = Console.CursorTop - 1;
  227. 			Console.SetCursorPosition( 0, linenumber );
  228.  
  229. 			// Calculate progress bar length
  230. 			double progress = Math.Min( 1F, ( currentvalue - (double)minimumvalue ) / ( maximumvalue - (double)minimumvalue ) );
  231. 			int progressbarlength = (int)( progress * Console.WindowWidth );
  232.  
  233. 			// Display the calculated progress bar
  234. 			if ( negcolorset )
  235. 			{
  236. 				if ( linearscale )
  237. 				{
  238. 					int negbarlength = (int)( -minimumvalue * Console.WindowWidth / ( maximumvalue - minimumvalue ) );
  239. 					Console.BackgroundColor = negcolor;
  240. 					Console.Write( new string( ' ', Math.Min( progressbarlength, negbarlength ) ) );
  241. 					if ( progressbarlength > negbarlength )
  242. 					{
  243. 						Console.BackgroundColor = poscolor;
  244. 						Console.Write( new string( ' ', progressbarlength - negbarlength ) );
  245. 					}
  246. 					// Erase remainder of line
  247. 					Console.ResetColor( );
  248. 					Console.Write( new string( ' ', Console.WindowWidth - progressbarlength ) );
  249. 				}
  250. 				else
  251. 				{
  252. 					if ( currentvalue == 0 )
  253. 					{
  254. 						Console.ResetColor( );
  255. 						Console.Write( new string( ' ', Console.WindowWidth ) );
  256. 					}
  257. 					else
  258. 					{
  259. 						int neghalflength = (int)( -minimumvalue * Console.WindowWidth / ( maximumvalue - minimumvalue ) );
  260. 						int poshalflength = Console.WindowWidth - neghalflength;
  261. 						if ( currentvalue < 0 )
  262. 						{
  263. 							progress = (double)currentvalue / minimumvalue;
  264. 							progressbarlength = (int)( progress * neghalflength );
  265. 							Console.ResetColor( );
  266. 							Console.Write( new string( ' ', neghalflength - progressbarlength ) );
  267. 							Console.BackgroundColor = negcolor;
  268. 							Console.Write( new string( ' ', progressbarlength ) );
  269. 							// Erase remainder of line
  270. 							Console.ResetColor( );
  271. 							Console.Write( new string( ' ', poshalflength ) );
  272. 						}
  273. 						else
  274. 						{
  275. 							progress = (double)currentvalue / maximumvalue;
  276. 							progressbarlength = (int)( progress * poshalflength );
  277. 							Console.ResetColor( );
  278. 							Console.Write( new string ( ' ', neghalflength ) );
  279. 							Console.BackgroundColor = poscolor;
  280. 							Console.Write( new String( ' ', progressbarlength ) );
  281. 							Console.ResetColor( );
  282. 							// Erase remainder of line
  283. 							Console.ResetColor( );
  284. 							Console.Write( new string( ' ', poshalflength - progressbarlength ) );
  285.  
  286. 						}
  287. 					}
  288. 				}
  289. 			}
  290. 			else
  291. 			{
  292. 				Console.BackgroundColor = poscolor;
  293. 				string progressbar = new string( ' ', progressbarlength );
  294. 				Console.Write( progressbar );
  295. 				// Erase remainder of line
  296. 				Console.ResetColor( );
  297. 				Console.Write( new string( ' ', Console.WindowWidth - progressbarlength ) );
  298. 			}
  299.  
  300. 			// Move the cursor back to the start of the line
  301. 			Console.SetCursorPosition( 0, linenumber );
  302.  
  303. 			return 0;
  304. 		}
  305.  
  306.  
  307. 		static int ShowHelp( params string[] errmsg )
  308. 		{
  309. 			#region Error Message
  310.  
  311. 			if ( errmsg.Length > 0 )
  312. 			{
  313. 				List<string> errargs = new List<string>( errmsg );
  314. 				errargs.RemoveAt( 0 );
  315. 				Console.Error.WriteLine( );
  316. 				Console.ForegroundColor = ConsoleColor.Red;
  317. 				Console.Error.Write( "ERROR:\t" );
  318. 				Console.ForegroundColor = ConsoleColor.White;
  319. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  320. 				Console.ResetColor( );
  321. 			}
  322.  
  323. 			#endregion Error Message
  324.  
  325.  
  326. 			#region Help Text
  327.  
  328. 			/*
  329. 			ProgressBar.exe,  Version 2.00
  330. 			Batch tool to display a progress bar
  331.  
  332. 			Usage:    ProgressBar   current  [ options ]
  333.  
  334. 			or:       ProgressBar   /C
  335.  
  336. 			Where:    current       is the current value
  337.  
  338. 			Options:  minimum       is the minimum value      (default:     0)
  339. 			          maximum       is the maximum value      (default:   100)
  340. 			          color         is the progress bar color (default: Green)
  341. 			          2ndcolor      is the "sub zero" color   (default: Green)
  342. 			          /C            list valid Colors (no other arguments allowed)
  343. 			          /L            start progress bar at Left (default: start at 0)
  344. 			          /NS           do Not show Scale above progress bar
  345.  
  346. 			Notes:    /L means the progress bar starts at the minimum value, whereas
  347. 			          by default the progress bar starts at 0. If the range
  348. 			          minimum..maximum does not include 0, /L is assumed.
  349. 			          Return code -1 in case of (command line) errors, otherwise 0.
  350.  
  351. 			Written by Rob van der Woude
  352. 			https://www.robvanderwoude.com
  353. 			*/
  354.  
  355. 			#endregion Help Text
  356.  
  357.  
  358. 			#region Display Help Text
  359.  
  360. 			Console.Error.WriteLine( );
  361.  
  362. 			Console.Error.WriteLine( "ProgressBar.exe,  Version {0}", progver );
  363.  
  364. 			Console.Error.WriteLine( "Batch tool to display a progress bar" );
  365.  
  366. 			Console.Error.WriteLine( );
  367.  
  368. 			Console.Error.Write( "Usage:    " );
  369. 			Console.ForegroundColor = ConsoleColor.White;
  370. 			Console.Error.WriteLine( "ProgressBar   current  [ options ]" );
  371. 			Console.ResetColor( );
  372.  
  373. 			Console.Error.WriteLine( );
  374.  
  375. 			Console.Error.Write( "or:       " );
  376. 			Console.ForegroundColor = ConsoleColor.White;
  377. 			Console.Error.WriteLine( "ProgressBar   /C" );
  378. 			Console.ResetColor( );
  379.  
  380. 			Console.Error.WriteLine( );
  381.  
  382. 			Console.Error.Write( "Where:    " );
  383. 			Console.ForegroundColor = ConsoleColor.White;
  384. 			Console.Error.Write( "current" );
  385. 			Console.ResetColor( );
  386. 			Console.Error.WriteLine( "       is the current value" );
  387.  
  388. 			Console.Error.WriteLine( );
  389.  
  390. 			Console.Error.Write( "Options:  " );
  391. 			Console.ForegroundColor = ConsoleColor.White;
  392. 			Console.Error.Write( "minimum" );
  393. 			Console.ResetColor( );
  394. 			Console.Error.WriteLine( "       is the minimum value      (default:     0)" );
  395.  
  396. 			Console.ForegroundColor = ConsoleColor.White;
  397. 			Console.Error.Write( "          maximum" );
  398. 			Console.ResetColor( );
  399. 			Console.Error.WriteLine( "       is the maximum value      (default:   100)" );
  400.  
  401. 			Console.ForegroundColor = ConsoleColor.White;
  402. 			Console.Error.Write( "          color" );
  403. 			Console.ResetColor( );
  404. 			Console.Error.WriteLine( "         is the progress bar color (default: Green)" );
  405.  
  406. 			Console.ForegroundColor = ConsoleColor.White;
  407. 			Console.Error.Write( "          2ndcolor" );
  408. 			Console.ResetColor( );
  409. 			Console.Error.WriteLine( "      is the \"sub zero\" color   (default: Green)" );
  410.  
  411.  
  412. 			Console.ForegroundColor = ConsoleColor.White;
  413. 			Console.Error.Write( "          /C" );
  414. 			Console.ResetColor( );
  415. 			Console.Error.WriteLine( "            list valid Colors (no other arguments allowed)" );
  416.  
  417. 			Console.ForegroundColor = ConsoleColor.White;
  418. 			Console.Error.Write( "          /L" );
  419. 			Console.ResetColor( );
  420. 			Console.Error.WriteLine( "            start progress bar at Left (default: start at 0)" );
  421.  
  422. 			Console.ForegroundColor = ConsoleColor.White;
  423. 			Console.Error.Write( "          /NS" );
  424. 			Console.ResetColor( );
  425. 			Console.Error.WriteLine( "           do Not show Scale above progress bar" );
  426.  
  427. 			Console.Error.WriteLine( );
  428.  
  429. 			Console.Error.Write( "Notes:    " );
  430. 			Console.ForegroundColor = ConsoleColor.White;
  431. 			Console.Error.Write( "/L" );
  432. 			Console.ResetColor( );
  433. 			Console.Error.Write( " means the progress bar starts at the " );
  434. 			Console.ForegroundColor = ConsoleColor.White;
  435. 			Console.Error.Write( "minimum" );
  436. 			Console.ResetColor( );
  437. 			Console.Error.WriteLine( " value, whereas" );
  438.  
  439. 			Console.Error.WriteLine( "          by default the progress bar starts at 0. If the range" );
  440.  
  441. 			Console.ForegroundColor = ConsoleColor.White;
  442. 			Console.Error.Write( "          minimum..maximum" );
  443. 			Console.ResetColor( );
  444. 			Console.Error.Write( " does not include 0, " );
  445. 			Console.ForegroundColor = ConsoleColor.White;
  446. 			Console.Error.Write( "/L" );
  447. 			Console.ResetColor( );
  448. 			Console.Error.WriteLine( " is assumed." );
  449.  
  450. 			Console.Error.WriteLine( "          Return code -1 in case of (command line) errors, otherwise 0." );
  451.  
  452. 			Console.Error.WriteLine( );
  453.  
  454. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  455.  
  456. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  457.  
  458. 			#endregion Display Help Text
  459.  
  460.  
  461. 			return -1;
  462. 		}
  463. 	}
  464. }

page last modified: 2024-04-16; loaded in 0.0121 seconds