aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.Main/ShiftOS/Apps/Terminal.cs
blob: 28e301c2263957326d3ed2666c5c01f67e7ec6d4 (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
using System;
using System.Windows.Forms;
using ShiftOS.Engine;
using ShiftOS.Main.Terminal;

namespace ShiftOS.Main.ShiftOS.Apps
{
    public partial class Terminal : UserControl
    {
        public int TerminalID = TerminalBackend.trmTopID++; // Used so that we can have multiple instances of the terminal whilst the command begin run knowing what terminal to send the text to - very complicated ;)
        public string defaulttextBefore = "user> ";
        public string defaulttextResult = "[user@shiftos~]$> "; // NOT YET IMPLEMENTED!!!
        public bool DoClear = false;
        public bool RunningCommand = false;
        public bool WaitingResponse = false;
        public string InputReturnText = "";

		// The below variables makes the terminal... a terminal!
		string OldText = "";

		int TrackingPosition;

		public Terminal()
		{
			InitializeComponent();

			termmain.ContextMenuStrip = new ContextMenuStrip(); // Disables the right click of a richtextbox!

            TerminalBackend.trm.Add(this);
        }

        void Print()
        {
            termmain.AppendText($"\n {defaulttextResult}");
            TrackingPosition = termmain.Text.Length;
        }

        void Print(string text)
		{
			termmain.AppendText($"\n {text} \n {defaulttextResult}");
			TrackingPosition = termmain.Text.Length;
        }

        //if (e.Control && e.KeyCode == Keys.V)
        //    {
        //        //if (Clipboard.ContainsText())
        //        //    termmain.Paste(DataFormats.GetFormat(DataFormats.Text));
        //        e.Handled = true;
        //    } else if (e.KeyCode == Keys.Enter) {
        //        RunningCommand = true;
        //        TerminalBackend.RunCommand(termmain.Text.Substring(TrackingPosition, termmain.Text.Length - TrackingPosition), TerminalID); // The most horrific line in the entire application!
        //        RunningCommand = false;
        //        termmain.AppendText($"\n {defaulttextResult}");
        //        TrackingPosition = termmain.Text.Length;
        //        e.Handled = true;
        //    }
        //}

        private void termmain_TextChanged(object sender, EventArgs e)
        {
            if (!RunningCommand)
            {
                if (termmain.SelectionStart < TrackingPosition)
                {
                    if (!DoClear) // If it's not clearing the terminal
                    {
                        termmain.Text = OldText;
                        termmain.Select(termmain.Text.Length, 0);
                    }
                }
                else
                {
                    OldText = termmain.Text;
                }
            }
        }

        private void termmain_SelectionChanged(object sender, EventArgs e)
        {
            if (!RunningCommand)
            {
                if (termmain.SelectionStart < TrackingPosition)
                {
                    termmain.Text = OldText;
                    termmain.Select(termmain.Text.Length, 0);
                }
            }
        }

        private void Terminal_Load(object sender, EventArgs e)
        {
            termmain.Text = $"\n {defaulttextResult}";
            TrackingPosition = termmain.Text.Length;
            termmain.Select(termmain.TextLength, 1);
        }

        public void Input(string request)
        {
            InputReturnText = "";
            RunningCommand = false;

            termmain.AppendText($"\n {request} ");
            TrackingPosition = termmain.Text.Length;
        }

        public void Clear()
        {
            DoClear = true;
            termmain.Text = $"\n {defaulttextResult}";
            TrackingPosition = termmain.Text.Length;
            DoClear = false;
        }

		void termmain_KeyDown(object sender, KeyEventArgs e)
		{
			// The below code disables the ability to paste anything other then text...

			if (e.Control && e.KeyCode == Keys.V)
			{
				//if (Clipboard.ContainsText())
				//    termmain.Paste(DataFormats.GetFormat(DataFormats.Text));
				e.Handled = true;
			}
			else if (e.KeyCode == Keys.Enter)
			{
				TerminalBackend.RunCommand(termmain.Text.Substring(TrackingPosition, termmain.Text.Length - TrackingPosition), TerminalID); // The most horrific line in the entire application!
                Print();
				e.Handled = true;
			}
		}
	}
}