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

namespace ShiftOS.Main.ShiftOS.Apps
{
	public partial class Terminal : UserControl
	{
		public string DefaulttextBefore = "user> ";
		string DefaulttextResult = "user@shiftos> "; // NOT YET IMPLEMENTED!!!
		bool DoClear = false;

		// 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!
		}

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

		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)
			{
				Print(
					TerminalBackend.RunCommand(
						termmain.Text.Substring(
							TrackingPosition,
							termmain.Text.Length - TrackingPosition))); // The most horrific line in the entire application!
				e.Handled = true;
			}
		}

		void termmain_TextChanged(object sender, EventArgs e)
		{
			if (termmain.SelectionStart < TrackingPosition)
			{
				if (DoClear) return;
				
				termmain.Text = OldText;
				termmain.Select(termmain.Text.Length, 0);
			}
			else
			{
				OldText = termmain.Text;
			}
		}

		void termmain_SelectionChanged(object sender, EventArgs e)
		{
			if (termmain.SelectionStart >= TrackingPosition) return;
			
			termmain.Text = OldText;
			termmain.Select(termmain.Text.Length, 0);
		}

		void Terminal_Load(object sender, EventArgs e)
		{
			Print("\n");
		}
	}
}