Repository navigation
Expand file tree
/
Copy pathFormScripts.cs
More file actions
226 lines (202 loc) · 8.87 KB
/
Copy pathFormScripts.cs
File metadata and controls
226 lines (202 loc) · 8.87 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SharpTagManager
{
partial class FormScripts : Form
{
public FormScripts()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
PopulateGrid();
}
private void PopulateGrid()
{
// Collect a list of active scripts
List<ScriptInstance> scripts = new List<ScriptInstance>(GetActiveScripts(AppResources.Scripts));
// Add rows for scripts that don't yet have one
foreach (ScriptInstance script in scripts)
{
if (FindScriptRow(script) == null)
{
var row = dataGridView1.RowTemplate.Clone() as DataGridViewRow;
row.Tag = script;
row.CreateCells(dataGridView1);
row.Cells[colName.Index].Value = script.Name;
row.Cells[colType.Index].Value = script.Descriptor != null ? "Descriptor" : "Plain";
row.Cells[colStatus.Index].Value = "New";
dataGridView1.Rows.Add(row);
}
}
// Update the rows
List<DataGridViewRow> rowsToDelete = new List<DataGridViewRow>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
try
{
ScriptInstance script = row.Tag as ScriptInstance;
if (script?.ProcessObj != null)
{
if (script.ErrorMessage != null)
{
row.Cells[colRAM.Index].Value = "N/A";
row.Cells[colCPU.Index].Value = "N/A";
row.Cells[colPID.Index].Value = "N/A";
row.Cells[colStatus.Index].Value = script.ErrorMessage;
row.Cells[colStatus.Index].Style.BackColor = Color.FromArgb(255, 50, 50);
}
else
{
row.Cells[colPID.Index].Value = script.ProcessObj.Id;
if (script.ProcessObj.HasExited)
{
row.Cells[colRAM.Index].Value = "N/A";
row.Cells[colCPU.Index].Value = "N/A";
}
else
{
double cpuUsage = CpuUsageTracker.GetCpuUsage(script.ProcessObj);
row.Cells[colRAM.Index].Value = FormatBytes(script.ProcessObj.PrivateMemorySize64);
row.Cells[colCPU.Index].Value = cpuUsage.ToString("0.##") + " %";
}
if (script.ProcessObj.HasExited)
{
if (script.ProcessObj.ExitCode == 0)
{
row.Cells[colStatus.Index].Value = "Ended";
row.Cells[colStatus.Index].Style.BackColor = Color.FromArgb(252, 247, 217);
}
else
{
row.Cells[colStatus.Index].Value = $"Error {script.ProcessObj.ExitCode}";
row.Cells[colStatus.Index].Style.BackColor = Color.FromArgb(255, 147, 117);
}
}
else if (script.CurrentlyProcessing != null)
{
row.Cells[colStatus.Index].Value = "Processing";
row.Cells[colStatus.Index].Style.BackColor = Color.FromArgb(209, 255, 253);
}
else
{
row.Cells[colStatus.Index].Value = "Running";
row.Cells[colStatus.Index].Style.BackColor = Color.FromArgb(228, 247, 233);
}
}
}
else
{
rowsToDelete.Add(row);
}
}
catch (Exception err)
{
row.Cells[colRAM.Index].Value = "N/A";
row.Cells[colCPU.Index].Value = "N/A";
row.Cells[colPID.Index].Value = "N/A";
row.Cells[colStatus.Index].Value = err.Message;
row.Cells[colStatus.Index].Style.BackColor = Color.FromArgb(255, 50, 50);
}
}
// Delete rows for scripts that went inactive
foreach (DataGridViewRow row in rowsToDelete)
dataGridView1.Rows.Remove(row);
}
public static string FormatBytes(long bytes)
{
string[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
if (bytes == 0)
return "0 B";
int magnitude = (int)Math.Floor(Math.Log(bytes, 1024));
double adjustedSize = bytes / Math.Pow(1024, magnitude);
return $"{adjustedSize:0.##} {sizeSuffixes[magnitude]}";
}
private DataGridViewRow FindScriptRow(ScriptInstance script)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Tag == script)
return row;
}
return null;
}
public static List<ScriptInstance> GetActiveScripts(Dictionary<string, ScriptInstance> collection)
{
List<ScriptInstance> results = new List<ScriptInstance>();
foreach (ScriptInstance script in collection.Values)
{
if (script.ProcessObj != null)
results.Add(script);
}
return results;
}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
// Get what cell was clicked
ScriptInstance script = null;
DataGridViewRow row = null;
var hitTestInfo = dataGridView1.HitTest(e.X, e.Y);
if (hitTestInfo.RowIndex >= 0 && hitTestInfo.ColumnIndex >= 0)
{
row = dataGridView1.Rows[hitTestInfo.RowIndex];
script = row.Tag as ScriptInstance;
}
// Process mouse clicks
if (e.Button == MouseButtons.Left)
{
if (e.Clicks >= 2)
{
// Double click
if (script != null)
ScriptShowOutputWindow(script);
}
}
if (e.Button == MouseButtons.Right)
{
// Show context menu on right click
dataGridView1_ContextMenu(row, script, e);
}
}
private void dataGridView1_ContextMenu(DataGridViewRow row, ScriptInstance script, MouseEventArgs e)
{
ContextMenuStrip menu = new ContextMenuStrip();
// Context sensitive items (only show on a valid script row)
if (script != null)
{
menu.Items.Add("Console").Click += (s, ea) => { FormScriptOutput.Show(script, toFront: true); };
menu.Items.Add("Restart process").Click += (s, ea) => { script.Restart(); };
menu.Items.Add("Kill process").Click += (s, ea) => { script.Stop(); };
}
// Populate Run menu
ToolStripMenuItem RunToolStripMenuItem = new ToolStripMenuItem("Run");
menu.Items.Add(RunToolStripMenuItem);
ScriptMenuHelper.PopulateScriptMenu(RunToolStripMenuItem, AppResources.Scripts, ScriptMenuItem_Click);
menu.Show(dataGridView1, e.Location);
}
private void ScriptMenuItem_Click(object sender, EventArgs ea)
{
// "-startup" tells the script it was launched manually with no work coming, so
// non-persistent scripts (single blocking stdin read, no timeout) know to exit
// immediately instead of hanging forever waiting for a request that never arrives.
ScriptInstance script = (sender as ToolStripMenuItem)?.Tag as ScriptInstance;
script?.Start("-startup");
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
dataGridView1.ClearSelection();
}
private void ScriptShowOutputWindow(ScriptInstance script)
{
FormScriptOutput.Show(script, toFront:true);
}
}
}