Пример #1
0
 /// <summary>
 /// Creates a new instance of the Process class
 /// </summary>
 /// <param name="options">Options specifying what to run</param>
 public Process(ProcessOptions options)
 {
     Options       = options;
     _result       = new ProcessResult(this, Options.CommandLine);
     _timeout      = Options.Timeout;
     StandardInput = new StandardInputWriter(this);
 }
Пример #2
0
        /// <summary>
        /// key down
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            // If StandardInput is closed return
            if (StandardInputWriter == null)
            {
                e.SuppressKeyPress = true;
                return;
            }

            switch (e.KeyCode)
            {
            // DOWN ARROW: Scroll Forward in History List
            case Keys.Down:
                if (scCommandLineHistory.Count > 0)
                {
                    if (!string.IsNullOrEmpty(historyCommand) && this.Text.EndsWith(historyCommand))
                    {
                        this.Text = this.Text.Substring(0, this.Text.Length - historyCommand.Length);
                    }

                    if (historyIndex < 0)
                    {
                        historyIndex = 0;
                    }
                    if (historyIndex > scCommandLineHistory.Count - 1)
                    {
                        historyIndex = scCommandLineHistory.Count - 1;
                    }


                    historyCommand = scCommandLineHistory[historyIndex];
                    this.AppendText(historyCommand);
                    historyIndex++;
                }
                e.SuppressKeyPress = true;
                break;

            // UP ARROW: Scroll Backward in History List
            case Keys.Up:
                if (scCommandLineHistory.Count > 0)
                {
                    if (historyIndex == -1)
                    {
                        historyIndex = scCommandLineHistory.Count - 1;
                    }

                    if (!string.IsNullOrEmpty(historyCommand) && this.Text.EndsWith(historyCommand))
                    {
                        this.Text = this.Text.Substring(0, this.Text.Length - historyCommand.Length);
                    }

                    if (historyIndex < 0)
                    {
                        historyIndex = 0;
                    }
                    if (historyIndex > scCommandLineHistory.Count - 1)
                    {
                        historyIndex = scCommandLineHistory.Count - 1;
                    }

                    historyCommand = scCommandLineHistory[historyIndex];
                    this.AppendText(historyCommand);
                    historyIndex--;
                }
                e.SuppressKeyPress = true;
                break;

            // LEFT ARROW: Don't allow this to passed as a character to the command line
            case Keys.Left:
                e.SuppressKeyPress = true;
                break;

            // RIGHT ARROW: Don't allow this to passed as a character to the command line
            case Keys.Right:
                e.SuppressKeyPress = true;
                break;

            case Keys.Home:
                e.SuppressKeyPress = true;
                break;

            case Keys.End:
                e.SuppressKeyPress = true;
                break;

            case Keys.Back:
                if (this.Text.Length == this.commandTextLength)
                {
                    e.SuppressKeyPress = true;
                }
                break;

            case Keys.Tab:
                string command = GetCommandLine().Trim();
                if (!string.IsNullOrWhiteSpace(command))
                {
                    var commands = command.Split(' ');
                    if (commands.Length > 0)
                    {
                        var fileName = commands[commands.Length - 1];
                        var name     = string.Empty;
                        if (!string.IsNullOrWhiteSpace(cmdDirectory))
                        {
                            var dic  = new DirectoryInfo(cmdDirectory);
                            var dics = dic.GetDirectories(fileName + "*");
                            if (dics.Length > 0)
                            {
                                name = dics[0].Name;
                            }
                            else
                            {
                                var files = dic.GetFiles(fileName + "*");
                                if (files.Length > 0)
                                {
                                    name = files[0].Name;
                                }
                            }

                            if (!string.IsNullOrWhiteSpace(name))
                            {
                                commands[commands.Length - 1] = name;
                                var newCommand = string.Join(" ", commands);
                                this.Text = this.Text.Substring(0, this.Text.Length - command.Length);

                                if (commandLineStartIndex == -1)
                                {
                                    commandLineStartIndex = this.Text.Length;
                                }

                                this.AppendText(newCommand);

                                e.SuppressKeyPress = true;
                                //StandardInputWriter.WriteLine(newCommand);
                                //if (!scCommandLineHistory.Contains(newCommand))
                                //    scCommandLineHistory.Add(newCommand);
                            }
                        }
                    }
                }
                break;

            // ENTER: Send the last command line to CMD.EXE via STDIN
            case Keys.Enter:
                // Get command line
                string commandLine = GetCommandLine();
                // Send the commandLine to standardInput or String.Empty for built-in commands (forces command prompt display)
                StandardInputWriter.WriteLine(commandLine);
                if (!string.IsNullOrWhiteSpace(commandLine))
                {
                    // Add this command line to the command line history
                    if (!scCommandLineHistory.Contains(commandLine))
                    {
                        scCommandLineHistory.Add(commandLine);
                    }
                }
                break;

            default:
                break;
            }
            base.OnKeyDown(e);
        }