Example #1
0
 public CommandVoidResult Edit(
     CommandEvaluationContext context,
     [Parameter("path of an existing or of a new file. the path directory must exists", true)] FilePath filePath
     )
 {
     Context = context;
     Init(context);
     if (filePath == null || filePath.CheckPathExists(context))
     {
         InitEditor();
         if (filePath != null && filePath.FileSystemInfo.Exists)
         {
             LoadFile(filePath);
         }
         else
         {
             _filePath = filePath;
         }
     }
     DisplayEditor();
     WaitAndProcessKeyPress();
     return(new CommandVoidResult());
 }
Example #2
0
        public CommandVoidResult History(
            CommandEvaluationContext context,
            [Option("i", "invoke the command at the entry number in the history list", true, true)] int num,
            [Option("c", "clear the loaded history list")] bool clear,
            [Option("w", "write history lines to the history file (content of the file is replaced)")]
            [OptionRequireParameter("file")]  bool writeToFile,
            [Option("a", "append history lines to the history file")]
            [OptionRequireParameter("file")]  bool appendToFile,
            [Option("r", "read the history file and append the content to the history list")]
            [OptionRequireParameter("file")]  bool readFromFile,
            [Option("n", "read the history file and append the content not already in the history list to the history list")]
            [OptionRequireParameter("file")] bool appendFromFile,
            [Parameter(1, "file", true)] FilePath file
            )
        {
            var hist = context.CommandLineProcessor.CmdsHistory.History;
            var max  = hist.Count().ToString().Length;
            int i    = 1;
            var f    = DefaultForegroundCmd;

            if (num > 0)
            {
                if (num < 1 || num > hist.Count)
                {
                    Errorln($"history entry number out of range (1..{hist.Count})");
                    return(new CommandVoidResult(ReturnCode.Error));
                }
                var h = hist[num - 1];
                context.CommandLineProcessor.CommandLineReader.SendNextInput(h);
                return(new CommandVoidResult());
            }

            if (clear)
            {
                context.CommandLineProcessor.CmdsHistory.ClearHistory();
                return(new CommandVoidResult());
            }

            if (appendToFile || readFromFile || appendFromFile || writeToFile)
            {
                file ??= context.CommandLineProcessor.CmdsHistory.FilePath;
                if (file.CheckPathExists())
                {
                    if (writeToFile)
                    {
                        File.Delete(context.CommandLineProcessor.CmdsHistory.FilePath.FullName);
                        File.AppendAllLines(file.FullName, hist);
                    }
                    if (appendToFile)
                    {
                        File.AppendAllLines(file.FullName, hist);
                    }
                    if (readFromFile)
                    {
                        var lines = File.ReadAllLines(file.FullName);
                        foreach (var line in lines)
                        {
                            context.CommandLineProcessor.CmdsHistory.HistoryAppend(line);
                        }
                        context.CommandLineProcessor.CmdsHistory.HistorySetIndex(-1, false);
                    }
                    if (appendFromFile)
                    {
                        var lines = File.ReadAllLines(file.FullName);
                        foreach (var line in lines)
                        {
                            if (!context.CommandLineProcessor.CmdsHistory.HistoryContains(line))
                            {
                                context.CommandLineProcessor.CmdsHistory.HistoryAppend(line);
                            }
                        }
                        context.CommandLineProcessor.CmdsHistory.HistorySetIndex(-1, false);
                    }
                }
                return(new CommandVoidResult());
            }

            foreach (var h in hist)
            {
                if (context.CommandLineProcessor.CancellationTokenSource.IsCancellationRequested)
                {
                    break;
                }
                var hp = $"  {ColorSettings.Numeric}{i.ToString().PadRight(max + 2, ' ')}{f}";
                context.Out.Echo(hp);
                Out.ConsolePrint(h, true);
                i++;
            }
            return(new CommandVoidResult());
        }