aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.PPCConsole/ViewModels/ConsoleViewVM.cs
blob: 4c6eb99587d49965dc512f035abc7c9c52bd66df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Console;
using Tango.Console.Network;
using Tango.Core.DI;
using Tango.FSE.Common;
using Tango.FSE.Common.Console;

namespace Tango.FSE.PPCConsole.ViewModels
{
    public class ConsoleViewVM : FSEViewModel
    {
        private List<ConsoleSuggestion> _lastSuggestions;

        [TangoInject]
        public IConsoleService ConsoleService { get; set; }

        private ConsoleControlVM _consoleVM;
        public ConsoleControlVM ConsoleVM
        {
            get { return _consoleVM; }
            set { _consoleVM = value; RaisePropertyChangedAuto(); }
        }

        public ConsoleViewVM()
        {
            _lastSuggestions = new List<ConsoleSuggestion>();
            ConsoleVM = new ConsoleControlVM();
            ConsoleVM.CommandExecuting += ConsoleVM_CommandExecuting;
            ConsoleVM.Clear();
        }

        public override void OnApplicationStarted()
        {
            ConsoleService.Initialized += ConsoleService_Initialized1;
        }

        private void ConsoleService_Initialized1(object sender, GetCurrentDirectoryResponse e)
        {
            ConsoleVM.Clear();
            ConsoleVM.CurrentCommand.WorkingFolder = e.CurrentDirectory;
            ConsoleVM.AppendSuggestions(e.Suggestions);
        }

        private async void ConsoleVM_CommandExecuting(object sender, ConsoleCommandExecutingEventArgs e)
        {
            try
            {
                var response = await ConsoleService.ExecuteCommand(new ConsoleCommandRequest()
                {
                    Command = e.Command.CommandText,
                    WorkingFolder = e.Command.WorkingFolder,
                });

                _lastSuggestions = response.Suggestions;
                e.Complete(new ConsoleCommandExecutionResult()
                {
                    Output = response.Output,
                    Suggestions = response.Suggestions,
                    WorkingFolder = response.WorkingFolder
                });
            }
            catch (Exception ex)
            {
                e.Complete(new ConsoleCommandExecutionResult()
                {
                    Output = ex.FlattenMessage(),
                    WorkingFolder = e.Command.WorkingFolder,
                    Suggestions = _lastSuggestions
                });
            }
        }
    }
}