Exemple #1
0
        public static CatOptions LoadOptions( string[] arguments )
        {
            CatOptions catOptions;
            string appname;

            catOptions = new CatOptions();
            appname = catOptions.appname;

            catOptions.useStdIn = StdInEx.IsInputRedirected;

            // Load the environment variables..
            if (envars.contains("l")) {
                catOptions.showLineNumbers = envars.attr<bool>(appname + "_l");
            }
            if (envars.contains(appname + "_w")) {
                catOptions.wrapText = envars.attr<bool>(appname + "_w");
            }
            if (envars.contains(appname + "_ib")) {
                catOptions.ignoreBlankLines = envars.attr<bool>(appname + "_ib");
            }
            if (envars.contains(appname + "_ibw")) {
                catOptions.ignoreWhitespaceLines = envars.attr<bool>(appname + "_ibw");
            }
            if (envars.contains(appname + "_il")) {
                catOptions.ignoreLines = envars.attr<string>(appname + "_il");
            }
            if (envars.contains(appname + "_f")) {
                catOptions.forcePlainText = envars.attr<bool>(appname + "_f");
            }
            if (envars.contains(appname + "_forcePlugin")) {
                catOptions.forceSpecificPlugin = envars.attr<string>(appname + "_forcePlugin");
            }

            // Load the command-line arguments..
            foreach (string a in arguments) {
                if ((a.StartsWith("/") || a.StartsWith("-") || a.StartsWith("!")) && !a.StartsWith("/~/") && !a.StartsWith("/c/")) {
                    string arg = a;
                    while (arg.StartsWith("/") || arg.StartsWith("-")) {
                        arg = arg.Substring(1);
                    }

                    if (arg.Equals("?", StringComparison.CurrentCultureIgnoreCase) || arg.StartsWith("h", StringComparison.CurrentCultureIgnoreCase)) {
                        // ?, h, help
                        catOptions.showHelp = true;

                    } else if (arg.Equals("pp", StringComparison.CurrentCultureIgnoreCase) || arg.StartsWith("pause-", StringComparison.CurrentCultureIgnoreCase)) {
                        // pp, pause-at, pause-at-end
                        catOptions.pauseAtEnd = true;
                    } else if (arg.Equals("!pp", StringComparison.CurrentCultureIgnoreCase) || arg.StartsWith("!pause-", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.pauseAtEnd = false;

                    } else if (arg.StartsWith("p", StringComparison.CurrentCultureIgnoreCase)) {
                        // p, pause
                        catOptions.pauseAfterEachPage = true;
                    } else if (arg.StartsWith("!p", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.pauseAfterEachPage = false;

                    } else if (arg.StartsWith("l", StringComparison.CurrentCultureIgnoreCase)) {
                        // l, lines, line-numbers
                        catOptions.showLineNumbers = true;
                    } else if (arg.StartsWith("!l", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.showLineNumbers = false;

                    } else if (arg.StartsWith("w", StringComparison.CurrentCultureIgnoreCase)) {
                        // w, wrap, wrap-lines
                        catOptions.wrapText = true;
                    } else if (arg.StartsWith("!w", StringComparison.CurrentCultureIgnoreCase) || arg.Equals("no-wrap", StringComparison.CurrentCultureIgnoreCase)) {
                        // !w, !wrap, !wrap-lines, no-wrap
                        catOptions.wrapText = false;

                    } else if (arg.StartsWith("force-plugin:", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.forceSpecificPlugin = arg.Substring(13);
                    } else if (arg.StartsWith("!force-plugin:", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.forceSpecificPlugin = "";

                    } else if (arg.StartsWith("f", StringComparison.CurrentCultureIgnoreCase)) {
                        // f, force, force-text, force-plain-text
                        catOptions.forcePlainText = true;
                    } else if (arg.StartsWith("!f", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.forcePlainText = false;

                    } else if (arg.Equals("ib", StringComparison.CurrentCultureIgnoreCase) || arg.StartsWith("ignore-b", StringComparison.CurrentCultureIgnoreCase)) {
                        // ib, ignore-b, ignore-blank, ignore-blank-lines
                        catOptions.ignoreBlankLines = true;
                    } else if (arg.Equals("!ib", StringComparison.CurrentCultureIgnoreCase) || arg.StartsWith("!ignore-b", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.ignoreBlankLines = false;

                    } else if (arg.Equals("ibw", StringComparison.CurrentCultureIgnoreCase) || arg.StartsWith("ignore-w", StringComparison.CurrentCultureIgnoreCase)) {
                        // ibw, ignore-w, ignore-white, ignore-whitespace, ignore-whitespace-lines
                        catOptions.ignoreBlankLines = true;
                        catOptions.ignoreWhitespaceLines = true;
                    } else if (arg.Equals("!ibw", StringComparison.CurrentCultureIgnoreCase) || arg.StartsWith("!ignore-w", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.ignoreWhitespaceLines = false;

                    } else if (arg.StartsWith("il:", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.ignoreLines = arg.Substring(3);
                    } else if (arg.StartsWith("ignore:", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.ignoreLines = arg.Substring(7);
                    } else if (arg.StartsWith("ignore-lines:", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.ignoreLines = arg.Substring(13);
                    } else if (arg.Equals("!ib", StringComparison.CurrentCultureIgnoreCase) || arg.StartsWith("!ignore-b", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.ignoreBlankLines = false;

                    } else if (arg.StartsWith("i:", StringComparison.CurrentCultureIgnoreCase)) {
                        // i:n
                        int val;
                        if (int.TryParse(arg.Substring(2), out val)) {
                            catOptions.indentCharacters = val;
                        } else {
                            Console.WriteLine("Invalid value for `/i`: {0}", arg.Substring(2));
                        }
                    } else if (arg.StartsWith("indent:", StringComparison.CurrentCultureIgnoreCase)) {
                        // indent:n
                        int val;
                        if (int.TryParse(arg.Substring(7), out val)) {
                            catOptions.indentCharacters = val;
                        } else {
                            Console.WriteLine("Invalid value for `/indent`: {0}", arg.Substring(7));
                        }

                    } else if (arg.Equals("expand", StringComparison.CurrentCultureIgnoreCase) || arg.Equals("!compress", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.normalExpanded = true;
                        catOptions.extraExpanded = true;
                    } else if (arg.Equals("!expand", StringComparison.CurrentCultureIgnoreCase) || arg.Equals("compress", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.normalExpanded = false;
                        catOptions.extraExpanded = false;

                    } else if (arg.StartsWith("normal", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.normalExpanded = true;
                    } else if (arg.StartsWith("!normal", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.normalExpanded = false;

                    } else if (arg.StartsWith("extra", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.extraExpanded = true;
                    } else if (arg.StartsWith("!extra", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.extraExpanded = false;

                    } else if (arg.StartsWith("show-pl", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.showPlugins = true;

                    } else if (arg.StartsWith("show-env", StringComparison.CurrentCultureIgnoreCase)) {
                        catOptions.showEnvVars = true;

                    }
                } else {
                    catOptions.files.Add(a);
                }
            }

            return catOptions;
        }
Exemple #2
0
        public int run( CatOptions catOptions )
        {
            //Thread.Sleep(10000);

            PlainText textHandler;
            bool foundIt;

            textHandler = new PlainText();

            LoadPlugins();

            if (catOptions.useStdIn) {
                string f = Path.GetTempFileName();
                File.WriteAllText(f, Console.In.ReadToEnd());
                catOptions.files.Clear();
                catOptions.files.Add(f);
            }

            for (int i = 0; i < catOptions.files.Count; i++) {
                string file = catOptions.files[i];

                // filename standardization / prepping..
                char ps = Path.DirectorySeparatorChar;
                char ops = (ps == '/') ? '\\' : '/';

                while (file.IndexOf(ops) > -1) {
                    file = file.Replace(ops, ps);
                }
                if (file.StartsWith("~" + ps) || file.StartsWith(ps + "~" + ps)) {
                    if (ps == '\\') {
                        file = "%UserProfile%" + Path.DirectorySeparatorChar + file.Substring(file.IndexOf("~" + ps) + 2);
                    } else {
                        file = "~" + Path.DirectorySeparatorChar + file.Substring(file.IndexOf("~" + ps) + 2);
                    }
                }
                if (file.IndexOf("%") > -1) {
                    file = Environment.ExpandEnvironmentVariables(file);
                }

                if (!File.Exists(file)) {
                    Console.WriteLine("cat.exe: File was not found: " + file);
                    continue;
                }

                foundIt = false;

                // Try to use automatic selection (via plugin.CanCat())
                if (!catOptions.forcePlainText && catOptions.forceSpecificPlugin.Length == 0) {
                    foreach (ICataloger ic in handlers) {
                        if (ic.CanCat(catOptions, file)) {
                            ic.Cat(catOptions, file);
                            foundIt = true;
                            break;
                        }
                    }
                }

                // Try to use the specified plugin (matching plugin.Name)
                if (catOptions.forceSpecificPlugin.Length > 0) {
                    foreach (ICataloger ic in handlers) {
                        if (ic.Name.Equals(catOptions.forceSpecificPlugin, StringComparison.CurrentCultureIgnoreCase)) {
                            ic.Cat(catOptions, file);
                            foundIt = true;
                            break;
                        }
                    }
                    if (!foundIt) {
                        Console.WriteLine("cat.exe: Could not find the specified plugin: " + catOptions.forceSpecificPlugin);
                        //return 1;
                    }
                }

                // If automatic didn't work and there wasn't a specificed plugin (or it failed),
                // use the default PlainText plugin.
                if (!foundIt) {
                    textHandler.Cat(catOptions, file);
                }
            }

            return 0;
        }
Exemple #3
0
        public bool Cat( CatOptions catOptions, string fileName, int lineStart, long linesToWrite )
        {
            int lineNumber;
            int padLen;
            int winWidth = Console.WindowWidth - 1;
            string l, lt;

            lineStart = Math.Max(lineStart, 0);
            lineNumber = 0;
            padLen = catOptions.showLineNumbers ? 3 : 0;
            if (linesToWrite < 0) {
                linesToWrite = long.MaxValue;
            }

            using (StreamReader reader = File.OpenText(fileName)) {
                while (!reader.EndOfStream) {
                    l = reader.ReadLine();
                    lt = l.Trim();
                    lineNumber++;

                    if (lineNumber < lineStart) {
                        continue;
                    }

                    if (catOptions.ignoreLines.Length > 0 && l.StartsWith(catOptions.ignoreLines, StringComparison.CurrentCultureIgnoreCase)) {
                        continue;
                    } else if (catOptions.ignoreBlankLines && l.Length == 0) {
                        continue;
                    } else if (catOptions.ignoreWhitespaceLines && lt.Length == 0) {
                        continue;
                    }

                    if (catOptions.showLineNumbers) {
                        Console.BackgroundColor = catOptions.lineNumBackColor;
                        Console.ForegroundColor = catOptions.lineNumForeColor;
                        Console.Write("{0," + padLen + "}", lineNumber);
                        Console.BackgroundColor = catOptions.defaultBackColor;
                        Console.ForegroundColor = catOptions.defaultForeColor;
                    }

                    if (lt.Length > 0) {
                        if (catOptions.wrapText) {
                            if (catOptions.indentCharacters > 0) {
                                Console.WriteLine(Bricksoft.PowerCode.Text.Wrap(l.TrimEnd(), winWidth, 0, catOptions.indentCharacters));
                            } else {
                                Console.WriteLine(Bricksoft.PowerCode.Text.Wrap(l.TrimEnd(), winWidth, 0, padLen));
                            }
                        } else {
                            Console.WriteLine(l.TrimEnd());
                        }
                    } else {
                        Console.WriteLine("  ");
                    }

                    if (lineNumber >= linesToWrite) {
                        break;
                    }
                }

                reader.Close();
            }

            return true;
        }
Exemple #4
0
 public bool Cat( CatOptions catOptions, string fileName )
 {
     return Cat(catOptions, fileName, 0, long.MaxValue);
 }
Exemple #5
0
 public bool CanCat( CatOptions catOptions, string fileName )
 {
     return true;
 }
Exemple #6
0
        public bool CatOld( CatOptions catOptions, string fileName, int lineStartsAt, string[] lines )
        {
            int lineNumber;
            int padLen;
            int winWidth = Console.WindowWidth - 1;

            if (lines != null && lines.Length > 0) {
                lineNumber = Math.Min(Math.Max(lineStartsAt, 0), lines.Length);
            } else {
                lineNumber = 0;
                lines = File.ReadAllLines(fileName);
            }
            if (catOptions.showLineNumbers) {
                padLen = lines.Length.ToString().Length;
            } else {
                padLen = 0;
            }

            foreach (string l in lines) {
                lineNumber++;

                if (catOptions.showLineNumbers) {
                    Console.BackgroundColor = catOptions.lineNumBackColor;
                    Console.ForegroundColor = catOptions.lineNumForeColor;
                    Console.Write("{0," + padLen + "}", lineNumber);
                    Console.BackgroundColor = catOptions.defaultBackColor;
                    Console.ForegroundColor = catOptions.defaultForeColor;
                }

                if (catOptions.wrapText) {
                    Console.WriteLine(l.Length > 0 ? Bricksoft.PowerCode.Text.Wrap(l.TrimEnd(), winWidth, 0, padLen) : " ");
                } else {
                    Console.WriteLine(l.Length > 0 ? l.TrimEnd() : " ");
                }
            }

            return true;
        }