Esempio n. 1
0
        public int Run()
        {
            normalForeColor = Console.ForegroundColor;
            normalBackColor = Console.BackgroundColor;
            highlightForeColor = ConsoleColor.Cyan;
            highlightBackColor = Console.BackgroundColor;
            errorForeColor = ConsoleColor.Red;
            errorBackColor = Console.BackgroundColor;

            if (args.Length == 0 || (args.Length > 0 && args[0].EqualsOneOf(StringComparison.InvariantCultureIgnoreCase, "/?", "/h", "/help"))) {
                DisplayUsage(args.Length > 1 ? args[1] : null);
                return 0;
            }
            if (args[0].EqualsOneOf(StringComparison.InvariantCultureIgnoreCase, "/hidden")) {
                DisplayUsage(null, true);
                return 0;
            }

            string cmdline;
            StringBuilder cmdlineBuilder;
            int result;

            // TODO change this project to use the Config settings.

            bool translateCR = false;
            bool translateLF = false;
            bool translateTab = false;
            bool wrapOutput = false;

            cmdlineBuilder = new StringBuilder();

            try {
                if (args.Length == 2 && (args[0].Equals("-s", StringComparison.CurrentCultureIgnoreCase) || args[0].Equals("-set", StringComparison.CurrentCultureIgnoreCase))) {
                    ConsoleColor tmpColor;
                    string arg;

                    arg = args[1];
                    if (arg.StartsWith("{") && arg.EndsWith("}")) {
                        arg = arg.Substring(1, arg.Length - 2);
                    }

                    // Convert old DOS colors to .net ConsoleColor
                    arg = ConvertDOSColors(arg); // TODO support bg

                    if (int.TryParse(arg, out result)
                            && result >= (int)ConsoleColor.Black && result <= (int)ConsoleColor.White) {
                        tmpColor = (ConsoleColor)result;
                    } else if (ConsoleColorNames.Contains(arg, StringComparison.InvariantCultureIgnoreCase)) {
                        tmpColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), arg, true);
                    } else {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("error in -s argument: invalid color specified");
                        Console.ForegroundColor = normalForeColor;
                        return 10;
                    }

                    try {
                        Console.ForegroundColor = tmpColor;
                    } catch (Exception) {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("error in -s argument: invalid color specified");
                        Console.ForegroundColor = normalForeColor;
                        return 10;
                    }

                    return 0;
                }

                if (ConsoleEx.IsInputRedirected) {
                    // When piping text to pcolor, I'm assuming that there is only
                    // one argument for pcolor and that is a color.
                    cmdlineBuilder.Append(args[0]);
                    cmdlineBuilder.Append(Console.In.ReadToEnd());
                } else {
                    // Read all the content from the arguments.
                    for (int argsi = 0; argsi < args.Length; argsi++) {
                        string a = args[argsi];
                        string al = a.ToLowerInvariant();

                        if (al.Equals("-f") || al.Equals("-file")) {
                            if (argsi <= args.Length - 2) {
                                StringBuilder contents;

                                argsi++;
                                contents = new StringBuilder();

                                if (!File.Exists(args[argsi])) {
                                    Console.ForegroundColor = errorForeColor;
                                    WriteLine(ConsoleColor.Red, "-f    the file was not found");
                                    DisplayUsage("-f");
                                    Console.ForegroundColor = normalForeColor;
                                    return 4;
                                }

                                if (contents.LoadFromFile(args[argsi])) {
                                    cmdlineBuilder.Append(contents.ToString());
                                }
                            } else {
                                Console.ForegroundColor = errorForeColor;
                                WriteLine(ConsoleColor.Red, "-f    is missing its file name");
                                DisplayUsage("-f");
                                Console.ForegroundColor = normalForeColor;
                                return 4;
                            }

                        } else if (al.EndsWith("-escape") || al.EndsWith("-crlf") || al.EndsWith("-cr-lf") || al.EndsWith("-cr-lf-tab")) {
                            translateCR = true;
                            translateLF = true;
                            translateTab = true;
                        } else if (al.EndsWith("!escape") || al.EndsWith("!crlf") || al.EndsWith("!cr-lf") || al.EndsWith("!cr-lf-tab")) {
                            translateCR = false;
                            translateLF = false;
                            translateTab = false;

                        } else if (al.EndsWith("-cr")) {
                            translateCR = true;
                        } else if (al.EndsWith("!cr")) {
                            translateCR = false;
                        } else if (al.EndsWith("-lf")) {
                            translateLF = true;
                        } else if (al.EndsWith("!lf")) {
                            translateLF = false;
                        } else if (al.EndsWith("-tab")) {
                            translateTab = true;
                        } else if (al.EndsWith("!tab")) {
                            translateTab = false;

                        } else if (al.EndsWith("-wrap")) {
                            wrapOutput = true;
                        } else if (al.EndsWith("!wrap")) {
                            wrapOutput = false;

                        } else {
                            // Convert hand-typed/specific linefeeds and tabs
                            if (translateCR) {
                                a = a.Replace("\\r\\n", "\r\n");
                            }
                            if (translateLF) {
                                a = a.Replace("\\n", "\n");
                            }
                            if (translateTab) {
                                a = a.Replace("\\t", "\t");
                            }

                            if (wrapOutput) {
                                cmdlineBuilder.Append(Text.Wrap(a));
                            } else {
                                cmdlineBuilder.Append(a);
                            }

                            if (argsi < args.Length - 1 && !a.EndsWith("\n")) {
                                cmdlineBuilder.Append(" ");
                            }
                        }
                    }
                }

                cmdline = cmdlineBuilder.ToString();

                // Convert old DOS colors to .net ConsoleColor
                cmdline = ConvertDOSColors(cmdline);

                // Provide a default color.
                if (!cmdline.StartsWith("{")) {
                    cmdline = "{" + normalForeColor.ToString() + "/" + normalBackColor.ToString() + "}" + cmdline;
                }

                WriteColoredString(cmdline);

            } catch (Exception ex) {
                // do nothing
                Console.Write(ex.Message);
                return 1;
            }

            Console.ForegroundColor = normalForeColor;

            return 0;
        }