(view source code of progressbar.cs as plain text)
using System;
using System.Collections.Generic;
namespace RobvanderWoude
{
internal class ProgressBar
{
static readonly string progver = "1.00";
static int Main( string[] args )
{
int currentvalue = -1;
int maximumvalue = -1;
ConsoleColor bgcolor = ConsoleColor.Green;
#region Parse Command Line
if ( args.Length < 1 || args.Length > 3 )
{
return ShowHelp( );
}
int numcolors = 0;
foreach ( string arg in args )
{
if ( arg == "/?" )
{
return ShowHelp( );
}
if ( arg.ToUpper( ) == "/C" )
{
// /C should be the only argument
if ( args.Length != 1 )
{
return ShowHelp( );
}
// Determine required column width
int columnwidt = 0;
foreach ( ConsoleColor color in Enum.GetValues( typeof( ConsoleColor ) ) )
{
columnwidt = Math.Max( columnwidt, Enum.GetName( typeof( ConsoleColor ), color ).Length );
}
// Display valid colors
Console.WriteLine( "Valid colors:" );
Console.WriteLine( "=============" );
foreach ( ConsoleColor color in Enum.GetValues( typeof( ConsoleColor ) ) )
{
Console.ForegroundColor = color;
// Prvent "invisible" text when foreground color equals background color
if ( Console.ForegroundColor == Console.BackgroundColor )
{
Console.BackgroundColor = (ConsoleColor)( ( (int)color + 7 ) % 16 );
}
Console.WriteLine( "{0,-" + columnwidt + "}\t{1,2}", Enum.GetName( typeof( ConsoleColor ), color ), (int)color );
Console.ResetColor( );
}
return 0;
}
else if ( int.TryParse( arg, out int test ) )
{
if ( currentvalue == -1 && test >= 0 ) // First numeric value is the current value
{
currentvalue = test;
}
else if ( maximumvalue == -1 && test > 0 ) // Second numeric value is the maximum value
{
maximumvalue = test;
}
else if ( test > -1 && test < 16 ) // Third numeric value could be the color
{
if ( numcolors > 0 )
{
return ShowHelp( "Duplicate color argument \"{0}\"", arg );
}
bgcolor = (ConsoleColor)test;
numcolors++;
}
else // No more numeric values allowed
{
return ShowHelp( "Invalid command line argument \"{0}\"", arg );
}
}
else
{
bool validcolor = false;
foreach ( ConsoleColor color in Enum.GetValues( typeof( ConsoleColor ) ) )
{
if ( arg.ToUpper( ) == Enum.GetName( typeof( ConsoleColor ), color ).ToUpper( ) )
{
if ( numcolors > 0 )
{
return ShowHelp( "Duplicate color argument \"{0}\"", arg );
}
bgcolor = color;
validcolor = true;
numcolors++;
}
}
if ( !validcolor )
{
return ShowHelp( "Invalid color \"{0}\", use /C switch to show valid colors", arg );
}
}
}
#endregion Parse Command Line
#region Validate Command Line
if ( maximumvalue == -1 )
{
maximumvalue = Console.WindowWidth;
}
if ( currentvalue == maximumvalue + 1 )
{
currentvalue = maximumvalue;
}
else if ( currentvalue < 0 || currentvalue > maximumvalue + 1 )
{
return ShowHelp( "Invalid current value \"{0}\"", currentvalue.ToString( ) );
}
#endregion Validate Command Line
// Move down 1 line and then return to the current one, to make sure we're not at the bottom line of the screen
Console.WriteLine( );
int linenumber = Console.CursorTop - 1;
Console.SetCursorPosition( 0, linenumber );
// Calculate progress bar length
double progress = Math.Min( 1F, (double)currentvalue / (double)maximumvalue );
int progressbarlength = (int)( progress * (double)( Console.WindowWidth ) );
// Display the calculated progress bar
Console.BackgroundColor = bgcolor;
string progressbar = new string( ' ', progressbarlength );
Console.Write( progressbar );
Console.ResetColor( );
Console.Write( new string( ' ', Console.WindowWidth - progressbarlength ) );
// Move the cursor back to the start of the line
Console.SetCursorPosition( 0, linenumber );
return 0;
}
static int ShowHelp( params string[] errmsg )
{
#region Error Message
if ( errmsg.Length > 0 )
{
List<string> errargs = new List<string>( errmsg );
errargs.RemoveAt( 0 );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR:\t" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
Console.ResetColor( );
}
#endregion Error Message
#region Help Text
/*
ProgressBar.exe, Version 1.00
Batch tool to display a progress bar
Usage: ProgressBar current [ maximum ] [ color ]
or: ProgressBar /C
Where: current is the current value
maximum is the maximum value (default: console width)
color is the progress bar color, either by name or
by number (default: Green)
/C list valid colors by name and number
Notes: The program writes a progress bar, the length of which is determined
by the formula: current / maximum * console width
Return code -1 in case of (command line) errors, otherwise 0.
Written by Rob van der Woude
https://www.robvanderwoude.com
*/
#endregion Help Text
#region Display Help Text
Console.Error.WriteLine( );
Console.Error.WriteLine( "ProgressBar.exe, Version {0}", progver );
Console.Error.WriteLine( "Batch tool to display a progress bar" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "ProgressBar current [ maximum ] [ color ]" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "or: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "ProgressBar /C" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "Where: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "current" );
Console.ResetColor( );
Console.Error.WriteLine( " is the current value" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " maximum" );
Console.ResetColor( );
Console.Error.WriteLine( " is the maximum value (default: console width)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " color" );
Console.ResetColor( );
Console.Error.WriteLine( " is the progress bar color, either by name or" );
Console.Error.WriteLine( " by number (default: Green)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /C" );
Console.ResetColor( );
Console.Error.WriteLine( " list valid colors by name and number" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Notes: The program writes a progress bar, the length of which is determined" );
Console.Error.WriteLine( " by the formula: current / maximum * console width" );
Console.Error.WriteLine( " Return code -1 in case of (command line) errors, otherwise 0." );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
Console.Error.WriteLine( "https://www.robvanderwoude.com" );
#endregion Display Help Text
return -1;
}
}
}
page last modified: 2022-10-20