-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormConsole.cs
179 lines (157 loc) · 6.16 KB
/
FormConsole.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using Suconbu.Mobile;
using Suconbu.Toolbox;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Suconbu.Sumacon
{
public partial class FormConsole : FormBase
{
Sumacon sumacon;
Dictionary<string, CommandContext> contexts = new Dictionary<string, CommandContext>();
LruCache<string, string> commandHistory = new LruCache<string, string>(10);
public FormConsole(Sumacon sumacon)
{
Trace.TraceInformation(Util.GetCurrentMethodName());
InitializeComponent();
this.sumacon = sumacon;
this.sumacon.WriteConsoleRequested += this.Sumacon_WriteConsoleRequested;
this.sumacon.DeviceManager.DeviceConnected += this.DeviceManager_DeviceConnected;
this.sumacon.DeviceManager.DeviceDisconnecting += this.DeviceManager_DeviceDisconnecting;
this.uxInputCombo.KeyDown += this.UxInputText_KeyDown;
this.uxInputCombo.PreviewKeyDown += this.UxInputCombo_PreviewKeyDown;
this.uxInputCombo.AutoCompleteMode = AutoCompleteMode.Append;
}
protected override void OnLoad(EventArgs e)
{
Trace.TraceInformation(Util.GetCurrentMethodName());
base.OnLoad(e);
this.uxInputCombo.Font = new Font(Properties.Resources.MonospaceFontName, this.uxInputCombo.Font.Size);
this.uxInputCombo.BackColor = Color.Black;
this.uxInputCombo.ForeColor = Color.White;
this.uxOutputText.Font = new Font(Properties.Resources.MonospaceFontName, this.uxOutputText.Font.Size);
this.uxOutputText.BackColor = Color.Black;
this.uxOutputText.ForeColor = Color.White;
this.uxOutputText.AppendText($"{Util.GetApplicationName()} v{Util.GetVersionString(3)}" + Environment.NewLine);
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
foreach(var context in this.contexts.Values)
{
context.Cancel();
}
this.sumacon.WriteConsoleRequested -= this.Sumacon_WriteConsoleRequested;
this.sumacon.DeviceManager.DeviceConnected -= this.DeviceManager_DeviceConnected;
this.sumacon.DeviceManager.DeviceDisconnecting -= this.DeviceManager_DeviceDisconnecting;
}
private void Sumacon_WriteConsoleRequested(string s)
{
this.SafeInvoke(() => this.Output(s));
}
void DeviceManager_DeviceConnected(object sender, Device device)
{
this.SafeInvoke(() => this.Output($"Connected: '{device.ToString(Properties.Resources.DeviceLabelFormat)}'"));
}
void DeviceManager_DeviceDisconnecting(object sender, Device device)
{
this.SafeInvoke(() =>
{
this.CancelShellCommand(device.Serial);
this.Output($"Disconnected: '{device.ToString(Properties.Resources.DeviceLabelFormat)}'");
});
}
private void UxInputCombo_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
foreach (var c in this.commandHistory.GetValues())
{
if(c.StartsWith(this.uxInputCombo.Text))
{
this.uxInputCombo.Text = c;
break;
}
}
}
}
void UxInputText_KeyDown(object sender, KeyEventArgs e)
{
var device = this.sumacon.DeviceManager.ActiveDevice;
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
if (device == null) return;
var command = this.uxInputCombo.Text;
if(command == "clear")
{
this.ClearBuffer();
this.uxInputCombo.Text = string.Empty;
}
else if (this.ExecuteShellCommand(device, command))
{
this.PushCommandHistory(command);
this.uxInputCombo.Text = string.Empty;
}
}
else if(e.KeyCode == Keys.C && e.Modifiers.HasFlag(Keys.Control))
{
e.SuppressKeyPress = true;
this.CancelShellCommand(device?.Serial);
}
}
void Output(string s)
{
this.uxOutputText.AppendText(Environment.NewLine + s);
}
void ClearBuffer()
{
this.uxOutputText.Clear();
}
bool ExecuteShellCommand(Device device, string command)
{
if (device == null) return false;
if (!this.contexts.TryGetValue(device.Serial, out var context))
{
// まだshellを開いてなかったら開く
context = device.RunCommandAsync("shell",
output => this.SafeInvoke(() =>
{
if (output != null)
this.Output(output);
else
this.contexts.Remove(device.Serial);
}),
error => this.SafeInvoke(() => this.Output(error)));
this.contexts.Add(device.Serial, context);
}
context.PushInput($"echo '> {command}'");
context.PushInput(command);
return true;
}
void CancelShellCommand(string deviceId)
{
if (this.contexts.TryGetValue(deviceId, out var context))
{
this.contexts.Remove(deviceId);
context.Cancel();
}
}
void PushCommandHistory(string command)
{
this.commandHistory.Add(command, command);
this.uxInputCombo.Items.Clear();
foreach(var c in this.commandHistory.GetValues())
{
this.uxInputCombo.Items.Add(c);
}
}
}
}