Exemplo n.º 1
0
        private void OnCMDCommand(object sender, ConsoleCommandArgs e)
        {
            if (e.Arguments == null || !e.Arguments.ContainsKey("/c"))
            {
                e.Result.Add($"The cmd command must have the /c argument, use the help command for more information.");
                return;
            }
            var command = e.Arguments["/c"];
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                WindowStyle            = ProcessWindowStyle.Hidden,
                FileName               = "cmd.exe",
                Arguments              = $"/C {command}",
                RedirectStandardOutput = true,
                CreateNoWindow         = true,
                UseShellExecute        = false
            };

            Process process = new Process
            {
                StartInfo = startInfo
            };

            process.Start();
            process.WaitForExit();
            e.Result.Add($"CMD output: {process.StandardOutput.ReadToEnd()}");
        }
Exemplo n.º 2
0
 private void OnSetResolutionCommand(object sender, ConsoleCommandArgs e)
 {
     UISynchronizationContext.ExecuteOnUIContext(() =>
     {
         var window = Resolve <IShell>() as Window;
         if (!e.Arguments.ContainsKey("/r"))
         {
             e.Result.Add("You must specify the /r argument.");
             e.Handled = false;
             return;
         }
         var res = e.Arguments["/r"];
         if (res.ToLower() == "fullscreen")
         {
             window.WindowState = WindowState.Maximized;
         }
         else
         {
             var resWH          = res.Split('x');
             window.WindowState = WindowState.Normal;
             window.Height      = double.Parse(resWH[0]);
             window.Width       = double.Parse(resWH[1]);
         }
     });
     e.Handled = true;
 }
Exemplo n.º 3
0
        private void OnSetLanguageCommand(object sender, ConsoleCommandArgs e)
        {
            if (!e.Arguments.ContainsKey("/l"))
            {
                e.Result.Add("You must specify the /l argument.");
                e.Handled = false;
                return;
            }

            LocalizationManager.DefaultLocalizationManager.SetCulture(e.Arguments["/l"]);
        }
Exemplo n.º 4
0
        private void OnHelpCommand(object sender, ConsoleCommandArgs e)
        {
            var maxName = _commands.Select(c => c.Name.Length).Max();
            var builder = new StringBuilder();

            builder.Append(Environment.NewLine);
            builder.Append(Environment.NewLine);
            foreach (var command in _commands)
            {
                builder.Append('-', 70);
                builder.Append(Environment.NewLine);
                builder.Append(command.Name);
                for (int i = 0; i < maxName - command.Name.Length; i++)
                {
                    builder.Append(" ");
                }
                builder.Append("\t");
                builder.Append(command.Description);
                builder.Append(Environment.NewLine);
                if (command.SupportedArguments == null || command.SupportedArguments.Count == 0)
                {
                    builder.Append("Doesn't have any arguments.");
                }
                else
                {
                    builder.Append("Arguments: ");
                    builder.Append(Environment.NewLine);
                    var maxArg = command.SupportedArguments.Select(a => a.Key.Length).Max();
                    foreach (var arg in command.SupportedArguments)
                    {
                        builder.Append($"{arg.Key}");
                        for (int i = 0; i < maxArg - command.Name.Length; i++)
                        {
                            builder.Append(" ");
                        }
                        builder.Append("\t");
                        builder.Append(arg.Value);
                    }
                }
                builder.Append(Environment.NewLine);
            }
            builder.Append('-', 70);

            e.Result = new List <string>
            {
                builder.ToString()
            };
        }
Exemplo n.º 5
0
        private void OnSetScreenCommand(object sender, ConsoleCommandArgs e)
        {
            if (!e.Arguments.ContainsKey("/i"))
            {
                e.Result.Add("You must specify the /i argument.");
                e.Handled = false;
                return;
            }
            var indexStr = e.Arguments["/i"];

            if (!int.TryParse(indexStr, out var index))
            {
                e.Result
                .Add($"The value '{indexStr}' is invalid for the /i argument, you need to specify an integer value.");
                e.Handled = false;
                return;
            }
            var temp = System.Windows.Forms.Screen.AllScreens.ElementAtOrDefault(index);

            if (temp == null)
            {
                e.Result.Add($"The value '{indexStr}' is invalid for the /i argument.");
                e.Handled = false;
                return;
            }
            var shell = Resolve <IShell>() as Window;

            if (shell == null)
            {
                e.Result.Add("Shell not found.");
                e.Handled = false;
                return;
            }

            var oldState = shell.WindowState;

            shell.WindowState = WindowState.Normal;
            shell.Left        = temp.WorkingArea.Left;
            shell.WindowState = oldState;

            e.Handled = true;
        }
Exemplo n.º 6
0
 private void OnClearCommand(object sender, ConsoleCommandArgs e)
 {
     ConsoleContent = string.Empty;
 }
Exemplo n.º 7
0
 private void OnExitCommand(object sender, ConsoleCommandArgs e)
 {
     ((WPFApplication)_app).GetSystemApplication().Shutdown();
 }
Exemplo n.º 8
0
 //Handlers
 private void OnRestartCommand(object sender, ConsoleCommandArgs e)
 {
     Process.Start(Assembly.GetEntryAssembly().GetName().CodeBase);
     ((WPFApplication)_app).GetSystemApplication().Shutdown();
 }
Exemplo n.º 9
0
        /// <summary>
        /// Execute a command
        /// </summary>
        public void Execute(string commandString)
        {
            if (string.IsNullOrEmpty(commandString))
            {
                return;
            }

            _executedCommands.Add(commandString);
            _executedCommandIndex = _executedCommands.Count;

            WriteToConsole(commandString);
            WriteToConsole(Environment.NewLine);
            CommandParseResult command;

            try
            {
                command = _parser.ParseCommand(commandString);
            }
            catch (CommandParseException e)
            {
                WriteToConsole($"An error occurred while parsing command: {e.Message}\n");
                return;
            }
            if (command == null)
            {
                WriteToConsole($"Invalid command '{commandString}'.\n");
                return;
            }
            var commandName = command.Command.Name.ToLower();
            var args        = new ConsoleCommandArgs
            {
                Arguments = command.Arguments,
                Command   = command.Command,
                Result    = new List <string>(),
            };


            var handlers        = _commandHandlers.ContainsKey(commandName) ? _commandHandlers[commandName] : null;
            var defaultHandlers = _defaultCommandHandlers.ContainsKey(commandName)
                ? _defaultCommandHandlers[commandName]
                : null;

            if (handlers == null && defaultHandlers == null)
            {
                WriteToConsole($"Command '{commandString}' is not supported.\n");
                return;
            }

            if (handlers != null)
            {
                foreach (var handler in handlers)
                {
                    if (args.Handled)
                    {
                        break;
                    }
                    handler?.Invoke(this, args);
                }
            }
            if (!args.Handled)
            {
                if (defaultHandlers != null)
                {
                    foreach (var handler in defaultHandlers)
                    {
                        if (args.Handled)
                        {
                            break;
                        }
                        handler?.Invoke(this, args);
                    }
                }
            }
            if (args.Result != null)
            {
                foreach (var result in args.Result)
                {
                    WriteToConsole(result);
                    WriteToConsole(Environment.NewLine);
                }
            }
        }