-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmMain.cs
156 lines (126 loc) · 5.35 KB
/
frmMain.cs
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
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace TermSim
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
ApplicationVariables.pwd = Environment.CurrentDirectory.ToLower(); //set present working directory
ActiveControl = txtCmd; //focus cursor on console
}
public class ApplicationVariables
{
//terminal variables
public static string pwd, lastCommand = "";
//application config variables
public static int onTop, minToTray;
}
public static FrmMain Instance { get; private set; }
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
Instance = this;
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
Instance = null;
}
private void FrmMain_Load(object sender, EventArgs e)
{
TerminalWriteLine("System", "TermSim initialized...", false, Color.DodgerBlue);
}
//--- command input events ---
private void txtCmd_KeyDown(object sender, KeyEventArgs e) //command input (enter key)
{
switch (e.KeyCode)
{
case Keys.Enter:
if (txtCmd.Text.Trim() != "")
{
TerminalInputString(txtCmd.Text); //write command string to terminal
ApplicationVariables.lastCommand = txtCmd.Text; //store last inputted command
CommandLine.InputCommand(txtCmd.Text); //pass command string to processor method
e.Handled = true;
e.SuppressKeyPress = true;
//keep focus on most recent line
txtCmd.Clear();
txtCmd.Focus();
}
break;
case Keys.Up:
txtCmd.Text = ApplicationVariables.lastCommand; //TODO: figure out this stupid text length bullshit
break;
}
}
private void lblSubmit_Click(object sender, EventArgs e) //command input (button)
{
if (txtCmd.Text.Trim() == "") return;
TerminalInputString(txtCmd.Text); //write command string to terminal
ApplicationVariables.lastCommand = txtCmd.Text; //store last inputted command
CommandLine.InputCommand(txtCmd.Text); //pass command string to processor method
//keep focus on most recent line
txtCmd.Clear();
txtCmd.Focus();
}
//--- examples of rich text formatted visual stuff ---
public void TerminalInputString(string cmd) //generates the rich text formatted console input string for user commands
{
var userArray = Regex.Split(Environment.UserDomainName, @"-");
rtbConsole.AppendText("\n" + userArray[0]);
rtbConsole.SelectionColor = Color.MediumTurquoise;
rtbConsole.AppendText("@");
rtbConsole.SelectionColor = Color.White;
rtbConsole.AppendText(userArray[1] + ":~");
rtbConsole.SelectionColor = Color.MediumTurquoise;
rtbConsole.AppendText(TerminalPath());
rtbConsole.SelectionColor = Color.White;
rtbConsole.AppendText(" $ " + cmd);
}
public void TerminalWriteLine(string msgType, string message, bool newLine, Color msgColor) //write message to terminal screen
{
var time = DateTime.Now.ToString("h:mm") + DateTime.Now.ToString("tt"); //format time
if (newLine)
rtbConsole.AppendText("\n");
//timestamp
rtbConsole.AppendText("[");
rtbConsole.SelectionColor = Color.SlateGray;
rtbConsole.AppendText(time);
rtbConsole.SelectionColor = rtbConsole.ForeColor;
rtbConsole.AppendText("] ");
//message
rtbConsole.SelectionColor = msgColor;
rtbConsole.AppendText(msgType + " ");
rtbConsole.SelectionColor = rtbConsole.ForeColor;
rtbConsole.AppendText("» " + message);
rtbConsole.Focus(); //focus the log
rtbConsole.SelectionStart = rtbConsole.Text.Length; //keep focus on most recent line
}
public static string TerminalPath()
{
try
{
var pathArray = Regex.Split(ApplicationVariables.pwd, @"\\");
var drvRoot = Path.GetPathRoot(Environment.CurrentDirectory).Replace("\\", "").ToLower();
var wrkPath = "\\" + pathArray[pathArray.Count() - 1];
if (pathArray.Count() > 1)
wrkPath = "\\" + pathArray[pathArray.Count() - 2] + wrkPath;
if (pathArray.Count() > 2)
wrkPath = "\\" + pathArray[pathArray.Count() - 3] + wrkPath;
if (pathArray.Count() > 3)
wrkPath = "..." + wrkPath;
return wrkPath.Replace("\\" + drvRoot, "drv[" + drvRoot.Replace(":", "") + "]");
}
catch (Exception)
{
return "";
}
}
}
}