aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Console/ConsoleControlVM.cs
blob: ed32bd321c312e4293355ad7ae19653a3f6bb9f1 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.SharedUI;

namespace Tango.Console
{
    public class ConsoleControlVM : ViewModel
    {
        private int _historyIndex;
        private List<ConsoleSuggestion> _knownSuggestions;

        public event EventHandler<ConsoleCommandExecutingEventArgs> CommandExecuting;

        public ObservableCollection<ConsoleCommand> Commands { get; private set; }

        private ConsoleCommand _currentCommand;
        public ConsoleCommand CurrentCommand
        {
            get { return _currentCommand; }
            set { _currentCommand = value; RaisePropertyChangedAuto(); }
        }

        private List<ConsoleSuggestion> _suggestions;
        public List<ConsoleSuggestion> Suggestions
        {
            get { return _suggestions; }
            set { _suggestions = value; RaisePropertyChangedAuto(); }
        }


        public RelayCommand ExecuteCommand { get; set; }

        public RelayCommand HistoryDownCommand { get; set; }

        public RelayCommand HistoryUpCommand { get; set; }

        public ConsoleControlVM()
        {
            Commands = new ObservableCollection<ConsoleCommand>();
            CurrentCommand = new ConsoleCommand()
            {
                WorkingFolder = Environment.CurrentDirectory
            };

            ExecuteCommand = new RelayCommand(Execute);

            HistoryDownCommand = new RelayCommand(HistoryDown);
            HistoryUpCommand = new RelayCommand(HistoryUp);

            _knownSuggestions = new List<ConsoleSuggestion>(ConsoleDictionary.GetKnownCommands().Select(x => new ConsoleSuggestion()
            {
                Type = ConsoleSuggestionType.Command,
                Name = x.Name,
                Description = x.Description
            }));

            Suggestions = new List<ConsoleSuggestion>(_knownSuggestions);
        }

        private void HistoryUp()
        {
            if (_historyIndex > 0)
            {
                _historyIndex--;
                CreateNew(CurrentCommand.WorkingFolder, false, GetDistinctCommands()[_historyIndex].CommandText, true);
            }
        }

        private void HistoryDown()
        {
            if (_historyIndex < GetDistinctCommands().Count - 1)
            {
                _historyIndex++;
                CreateNew(CurrentCommand.WorkingFolder, false, GetDistinctCommands()[_historyIndex].CommandText, true);
            }
        }

        private void Execute()
        {
            if (CurrentCommand != null)
            {
                if (!CurrentCommand.CommandText.IsNotNullOrEmpty())
                {
                    CreateNew(CurrentCommand.WorkingFolder, true);
                    return;
                }

                if (CurrentCommand.CommandText.ToLower() == "clear")
                {
                    Clear();
                    return;
                }

                CurrentCommand.IsExecuting = true;

                ConsoleCommandExecutingEventArgs args = new ConsoleCommandExecutingEventArgs(CurrentCommand, (result) =>
                {
                    CurrentCommand.IsExecuting = false;
                    CurrentCommand.Output = result.Output;

                    CreateNew(result.WorkingFolder, true);

                    Suggestions = _knownSuggestions.Concat(result.Suggestions).OrderBy(x => x.Name).ToList();
                });

                CommandExecuting?.Invoke(this, args);
            }
        }

        public void Clear()
        {
            Commands.Clear();
            CreateNew(CurrentCommand.WorkingFolder, false);
        }

        public void Clear(String workingFolder)
        {
            Commands.Clear();
            CreateNew(workingFolder, false);
        }

        private void CreateNew(String workingFolder, bool pushCurrent, String commandText = null, bool fromHistory = false)
        {
            if (pushCurrent)
            {
                Commands.Add(CurrentCommand);
                _historyIndex = GetDistinctCommands().Count;
            }

            CurrentCommand = new ConsoleCommand()
            {
                WorkingFolder = workingFolder != null ? workingFolder : CurrentCommand.WorkingFolder,
                CommandText = commandText,
                IsFromHistory = fromHistory
            };
        }

        private List<ConsoleCommand> GetDistinctCommands()
        {
            return Commands.Where(x => !x.IsFromHistory).ToList();
        }
    }
}