aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.Engine/Terminal/TerminalCommand.cs
blob: 110d1d21f959807b99325203379da012b5ba4307 (plain) (blame)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;

using System.Collections.Generic;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;



namespace ShiftOS.Main.Terminal
{

    public class TerminalCommand

    {

        public int TermID { get; set; }



        public virtual string Name { get; }

        public virtual string Summary { get; }

        public virtual string Usage { get; }

        public virtual bool Unlocked { get; set; }



        public virtual void Run(params string[] parameters) { }



        /// <summary>

        /// Writes a blank line in the terminal.

        /// </summary>

        public virtual void WriteLine()

        {

            WriteLine("");

        }



        /// <summary>

        /// Writes specified text in the terminal and starts a new line.

        /// </summary>

        /// <param name="value"><summary>The text to write before the new line is made.</summary></param>

        public virtual void WriteLine(string value)

        {

            Array.Find(TerminalBackend.trm.ToArray(), w => w.TerminalID == TermID).termmain.AppendText($"{value} \n");

        }



        /// <summary>

        /// Writes specified text in the terminal in the specified color and starts a new line.

        /// </summary>

        /// <param name="value"><summary>The text to write before the new line is made.</summary></param>

        /// <param name="textClr"><summary>The color the text is written in.</summary></param>

        public virtual void WriteLine(string value, Color textClr)

        {

            ShiftOS.Apps.Terminal trm = Array.Find(TerminalBackend.trm.ToArray(), w => w.TerminalID == TermID);



            int startPoint = trm.termmain.Text.Length;

            trm.termmain.AppendText($"{value} \n");

            trm.termmain.Select(startPoint, $"{value} \n".Length);

            trm.termmain.SelectionColor = textClr;

        }



        /// <summary>

        /// Writes specified text in the terminal.

        /// </summary>

        /// <param name="value"><summary>The text to write.</summary></param>

        /// <param name="textClr"><summary>The color the text is written in.</summary></param>

        public virtual void Write(string value, Color textClr)

        {



            ShiftOS.Apps.Terminal trm = Array.Find(TerminalBackend.trm.ToArray(), w => w.TerminalID == TermID);



            int startPoint = trm.termmain.Text.Length;

            trm.termmain.AppendText($"{value}");

            trm.termmain.Select(startPoint, $"{value}".Length);

            trm.termmain.SelectionColor = textClr;

        }



        /// <summary>

        /// Writes specified text in the terminal.

        /// </summary>

        /// <param name="value"><summary>The text to say before requesting text. </summary></param>

        public virtual Task<string> Input(string value = "")

        {

            ShiftOS.Apps.Terminal trm = Array.Find(TerminalBackend.trm.ToArray(), w => w.TerminalID == TermID);

            trm.Input(value);



            Task<string> Input = new Task<string>(() =>

            {              

                while (true)

                    if (trm.InputReturnText != "") break;



                // The terminal has finally decided!



                return trm.InputReturnText;

            });

            Input.Start();

            return Input;

        }

    }

}