Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for kolor.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	class Kolor
  10. 	{
  11. 		static string progver = "1.03";
  12.  
  13.  
  14. 		static int Main( string[] args )
  15. 		{
  16. 			#region Initialize Variables
  17.  
  18. 			bool bgonly = false;
  19. 			bool fgonly = false;
  20. 			bool checkupdate = false;
  21. 			bool colorsset = false;
  22. 			bool enforce = false;
  23. 			bool test = false;
  24. 			int bgcolor = (int) Console.BackgroundColor;
  25. 			int fgcolor = (int) Console.ForegroundColor;
  26. 			int rc = 0;
  27. 			int switchcount = 0;
  28.  
  29. 			#endregion Initialize Variables
  30.  
  31. 			#region Command Line Parsing
  32.  
  33. 			if ( args.Length > 2 )
  34. 			{
  35. 				return ShowHelp( );
  36. 			}
  37. 			/*
  38. 						if ( args.Length == 0 )
  39. 						{
  40. 							Console.WriteLine( "{0:X}{1:X}", (int) Console.BackgroundColor, (int) Console.ForegroundColor );
  41. 							return 0;
  42. 						}
  43. 			*/
  44. 			if ( args.Length > 0 )
  45. 			{
  46. 				foreach ( string arg in args )
  47. 				{
  48. 					if ( arg[0] == '/' )
  49. 					{
  50. 						switchcount += 1;
  51. 						if ( arg.Length == 2 )
  52. 						{
  53. 							switch ( arg.ToUpper( ) )
  54. 							{
  55. 								case "/?":
  56. 									return ShowHelp( );
  57. 								case "/B":
  58. 									if ( bgonly )
  59. 									{
  60. 										return ShowHelp( "Duplicate command line switch /B" );
  61. 									}
  62. 									bgonly = true;
  63. 									break;
  64. 								case "/F":
  65. 									if ( fgonly )
  66. 									{
  67. 										return ShowHelp( "Duplicate command line switch /F" );
  68. 									}
  69. 									fgonly = true;
  70. 									break;
  71. 								case "/R":
  72. 									// ignored, for backwards compatibility only
  73. 									break;
  74. 								case "/T":
  75. 									if ( test )
  76. 									{
  77. 										return ShowHelp( "Duplicate command line switch /T" );
  78. 									}
  79. 									test = true;
  80. 									break;
  81. 								case "/U":
  82. 									if ( checkupdate )
  83. 									{
  84. 										return ShowHelp( "Duplicate command line switch /U" );
  85. 									}
  86. 									checkupdate = true;
  87. 									break;
  88. 								case "/Y":
  89. 									if ( enforce )
  90. 									{
  91. 										return ShowHelp( "Duplicate command line switch /Y" );
  92. 									}
  93. 									enforce = true;
  94. 									break;
  95. 								default:
  96. 									return ShowHelp( "Invalid command line switch {0}", arg.ToUpper( ) );
  97. 							}
  98. 						}
  99. 						else
  100. 						{
  101. 							return ShowHelp( "Invalid command line switch {0}", arg.ToUpper( ) );
  102. 						}
  103. 					}
  104. 					else
  105. 					{
  106. 						try
  107. 						{
  108. 							bgcolor = int.Parse( args[0], NumberStyles.HexNumber ) / 16; // first hex digit
  109. 							fgcolor = int.Parse( args[0], NumberStyles.HexNumber ) % 16; // second hex digit
  110. 							colorsset = true;
  111. 						}
  112. 						catch ( Exception e )
  113. 						{
  114. 							return ShowHelp( e.Message );
  115. 						}
  116. 					}
  117. 				}
  118. 				if ( args.Length > 1 )
  119. 				{
  120. 					if ( !colorsset || ( colorsset && !enforce ) )
  121. 					{
  122. 						return ShowHelp( "Invalid combination of command line arguments" );
  123. 					}
  124. 				}
  125. 			}
  126.  
  127. 			#endregion Command Line Parsing
  128.  
  129. 			if ( colorsset )
  130. 			{
  131. 				if ( bgcolor == fgcolor )
  132. 				{
  133. 					rc = 1;
  134. 					if ( !enforce )
  135. 					{
  136. 						return rc; // ignore equal background and foreground color, unless /Y was used
  137. 					}
  138. 				}
  139. 				else
  140. 				{
  141. 					rc = 0;
  142. 				}
  143. 				Console.BackgroundColor = (ConsoleColor) bgcolor;
  144. 				Console.ForegroundColor = (ConsoleColor) fgcolor;
  145. 			}
  146. 			else if ( bgonly )
  147. 			{
  148. 				rc = (int) Console.BackgroundColor;
  149. 				Console.WriteLine( "{0:X}", (int) Console.BackgroundColor );
  150. 			}
  151. 			else if ( fgonly )
  152. 			{
  153. 				rc = (int) Console.ForegroundColor;
  154. 				Console.WriteLine( "{0:X}", (int) Console.ForegroundColor );
  155. 			}
  156. 			else if ( checkupdate )
  157. 			{
  158. 				CheckUpdate( );
  159. 				rc = 0;
  160. 			}
  161. 			else if ( test )
  162. 			{
  163. 				TestColors( );
  164. 				rc = 1;
  165. 			}
  166. 			else
  167. 			{
  168. 				rc = 16 * (int) Console.BackgroundColor + (int) Console.ForegroundColor;
  169. 				Console.WriteLine( "{0:X}{1:X}", (int) Console.BackgroundColor, (int) Console.ForegroundColor );
  170. 			}
  171. 			return rc;
  172. 		}
  173.  
  174.  
  175. 		public static void CheckUpdate( )
  176. 		{
  177. 			string updatecheckurl = String.Format( "http://www.robvanderwoude.com/getlatestver.php?progfile=Kolor.exe&version={0}", progver );
  178. 			Process.Start( updatecheckurl );
  179. 		}
  180.  
  181.  
  182. 		public static void TestColors( )
  183. 		{
  184. 			int width = Console.WindowWidth * 2;
  185. 			string text;
  186. 			for ( int i = 0; i < 16; i++ )
  187. 			{
  188. 				Console.BackgroundColor = (ConsoleColor) i;
  189. 				Console.ForegroundColor = ConsoleColor.White;
  190. 				text = String.Format( "{0,3}  {1,-21}  {0,3}  {1,-21}  {0,3}  {1,-21}", i, ( (ConsoleColor) i ).ToString( ) );
  191. 				if ( i > 9 )
  192. 				{
  193. 					Console.ForegroundColor = ConsoleColor.Black;
  194. 				}
  195. 				Console.Write( "{0,-" + width + "}", text );
  196. 				Console.ResetColor( );
  197. 			}
  198. 		}
  199.  
  200.  
  201. 		public static int ShowHelp( params string[] errmsg )
  202. 		{
  203. 			#region Help Text
  204. 			/*
  205. 			Kolor.exe,  Version 1.03
  206. 			Set or get console background and foreground colors
  207.  
  208. 			Usage:  Kolor.exe  xy [ /Y ]
  209.  
  210. 			   or:  Kolor.exe  [ /B | /F | /T | /U ]
  211.  
  212. 			Where:  x    is a hexadecimal digit for the new background color
  213. 			        y    is a hexadecimal digit for the new foreground color
  214. 			        /B   returns the current Background color on screen and as return code
  215. 			        /F   returns the current Foreground color on screen and as return code
  216. 			        /T   Test: show console colors and their associated numbers
  217. 			        /U   check for program Updates
  218. 			        /Y   enforce new colors even if background and foreground colors are
  219. 			             equal (default: when equal, change is ignored, return code 1)
  220.  
  221. 			Notes:  Unlike CMD's internal COLOR command, which changes the colors for the
  222. 			        entire screen, KOLOR will only change the colors of the text displayed
  223. 			        after the command is issued.
  224. 			        Use KOLOR /T to see the available colors and their numbers.
  225. 			        Kolor.exe without parameters returns current background and foreground
  226. 			        colors as 16 * background + foreground, on screen and as return code.
  227. 			        When setting colors, return code is 0, or 1 on errors.
  228. 			        When reading colors, return code is requested color(s), or 0 on errors.
  229. 			        Returned colors are shown in hexadecimal, for decimal use return code.
  230.  
  231. 			Written by Rob van der Woude
  232. 			http://www.robvanderwoude.com
  233. 			*/
  234.  
  235. 			if ( errmsg.Length > 0 )
  236. 			{
  237. 				List<string> errargs = new List<string>( errmsg );
  238. 				errargs.RemoveAt( 0 );
  239. 				Console.Error.WriteLine( );
  240. 				Console.ForegroundColor = ConsoleColor.Red;
  241. 				Console.Error.Write( "ERROR:\t" );
  242. 				Console.ForegroundColor = ConsoleColor.White;
  243. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  244. 				Console.ResetColor( );
  245.  
  246. 			}
  247.  
  248. 			Console.Error.WriteLine( );
  249.  
  250. 			Console.Error.WriteLine( "Kolor.exe,  Version {0}", progver );
  251.  
  252. 			Console.Error.WriteLine( "Set or get console background and foreground colors" );
  253.  
  254. 			Console.Error.WriteLine( );
  255.  
  256. 			Console.Error.Write( "Usage:  " );
  257. 			Console.ForegroundColor = ConsoleColor.White;
  258. 			Console.Error.WriteLine( "Kolor.exe  xy [ /Y ]" );
  259. 			Console.ResetColor( );
  260.  
  261. 			Console.Error.WriteLine( );
  262.  
  263. 			Console.Error.Write( "   or:  " );
  264. 			Console.ForegroundColor = ConsoleColor.White;
  265. 			Console.Error.WriteLine( "Kolor.exe  [ /B | /F | /T | /U ]" );
  266. 			Console.ResetColor( );
  267.  
  268. 			Console.Error.WriteLine( );
  269.  
  270. 			Console.Error.Write( "Where:  " );
  271. 			Console.ForegroundColor = ConsoleColor.White;
  272. 			Console.Error.Write( "x" );
  273. 			Console.ResetColor( );
  274. 			Console.Error.WriteLine( "    is a hexadecimal digit for the new background color" );
  275.  
  276. 			Console.ForegroundColor = ConsoleColor.White;
  277. 			Console.Error.Write( "        y" );
  278. 			Console.ResetColor( );
  279. 			Console.Error.WriteLine( "    is a hexadecimal digit for the new foreground color" );
  280.  
  281. 			Console.ForegroundColor = ConsoleColor.White;
  282. 			Console.Error.Write( "        /B" );
  283. 			Console.ResetColor( );
  284. 			Console.Error.Write( "   returns the current " );
  285. 			Console.ForegroundColor = ConsoleColor.White;
  286. 			Console.Error.Write( "B" );
  287. 			Console.ResetColor( );
  288. 			Console.Error.WriteLine( "ackground color on screen and as return code" );
  289.  
  290. 			Console.ForegroundColor = ConsoleColor.White;
  291. 			Console.Error.Write( "        /F" );
  292. 			Console.ResetColor( );
  293. 			Console.Error.Write( "   returns the current " );
  294. 			Console.ForegroundColor = ConsoleColor.White;
  295. 			Console.Error.Write( "F" );
  296. 			Console.ResetColor( );
  297. 			Console.Error.WriteLine( "oreground color on screen and as return code" );
  298.  
  299. 			Console.ForegroundColor = ConsoleColor.White;
  300. 			Console.Error.Write( "        /T   T" );
  301. 			Console.ResetColor( );
  302. 			Console.Error.WriteLine( "est: show console colors and their associated numbers" );
  303.  
  304. 			Console.ForegroundColor = ConsoleColor.White;
  305. 			Console.Error.Write( "        /U" );
  306. 			Console.ResetColor( );
  307. 			Console.Error.Write( "   check for program " );
  308. 			Console.ForegroundColor = ConsoleColor.White;
  309. 			Console.Error.Write( "U" );
  310. 			Console.ResetColor( );
  311. 			Console.Error.WriteLine( "pdates" );
  312.  
  313. 			Console.ForegroundColor = ConsoleColor.White;
  314. 			Console.Error.Write( "        /Y" );
  315. 			Console.ResetColor( );
  316. 			Console.Error.WriteLine( "   enforce new colors even if background and foreground colors are" );
  317.  
  318. 			Console.Error.WriteLine( "             equal (default: when equal, change is ignored, return code 1)" );
  319.  
  320. 			Console.Error.WriteLine( );
  321.  
  322. 			Console.Error.WriteLine( "Notes:  Unlike CMD's internal COLOR command, which changes the colors for the" );
  323.  
  324. 			Console.ForegroundColor = ConsoleColor.White;
  325. 			Console.Error.Write( "        entire screen" );
  326. 			Console.ResetColor( );
  327. 			Console.Error.WriteLine( ", KOLOR will only change the colors of the text displayed" );
  328.  
  329. 			Console.ForegroundColor = ConsoleColor.White;
  330. 			Console.Error.Write( "        after" );
  331. 			Console.ResetColor( );
  332. 			Console.Error.WriteLine( " the command is issued." );
  333.  
  334. 			Console.Error.Write( "        Use " );
  335. 			Console.ForegroundColor = ConsoleColor.White;
  336. 			Console.Error.Write( "KOLOR /T" );
  337. 			Console.ResetColor( );
  338. 			Console.Error.WriteLine( " to see the available colors and their numbers." );
  339.  
  340. 			Console.Error.WriteLine( "        Kolor.exe without parameters returns current background and foreground" );
  341.  
  342. 			Console.Error.Write( "        colors as " );
  343. 			Console.ForegroundColor = ConsoleColor.White;
  344. 			Console.Error.Write( "16 * background + foreground" );
  345. 			Console.ResetColor( );
  346. 			Console.Error.WriteLine( ", on screen and as return code." );
  347.  
  348. 			Console.Error.Write( "        When " );
  349. 			Console.ForegroundColor = ConsoleColor.White;
  350. 			Console.Error.Write( "setting" );
  351. 			Console.ResetColor( );
  352. 			Console.Error.WriteLine( " colors, return code is 0, or 1 on error." );
  353.  
  354. 			Console.Error.Write( "        When " );
  355. 			Console.ForegroundColor = ConsoleColor.White;
  356. 			Console.Error.Write( "reading" );
  357. 			Console.ResetColor( );
  358. 			Console.Error.WriteLine( " colors, return code is requested color(s), or 0 on error." );
  359.  
  360. 			Console.Error.WriteLine( "        Returned colors are shown in hexadecimal, for decimal use return code." );
  361.  
  362. 			Console.Error.WriteLine( );
  363.  
  364. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  365.  
  366. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  367.  
  368. 			#endregion Help Text
  369.  
  370. 			return 1;
  371. 		}
  372. 	}
  373. }
  374.  

page last uploaded: 2017-08-21, 14:26