-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgramUI.cs
345 lines (312 loc) · 9.91 KB
/
ProgramUI.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using System.Data;
using System.Text;
using AppCompare.Analyzers;
using Terminal.Gui;
namespace AppCompare;
class ProgramUI {
static readonly MenuBar menu = new (new MenuBarItem [] {
new ("_File", new MenuItem? [] {
new ("Open App _A...", "", FileOpenApp1, null, null, Key.F1),
new ("Open App _B...", "", FileOpenApp2, null, null, Key.F2),
null,
new ("_Open File Mappings...", "", FileOpenMappings, null, null, Key.CtrlMask | Key.O),
new ("_Save File Mappings...", "", FileSaveMappings, AreMappingsPresent, null, Key.CtrlMask | Key.S),
null,
new ("_Export Markdown...", "", FileExport, null, null, Key.CtrlMask | Key.E),
null,
new ("_Quit", "", FileQuit, null, null, Key.CtrlMask | Key.Q),
}),
new ("_Edit", new MenuItem? [] {
new ("_Pair files...", "", EditPairFiles, AnyFilesPresent, null, Key.CtrlMask | Key.P),
new ("_Unpair files", "", EditUnpairFiles, IsPaired, null, Key.CtrlMask | Key.U),
}),
new ("_View", new MenuItem? [] {
new ("_Gist App Compare Report...", "", ViewGist, null, null, Key.CtrlMask | Key.G),
null,
new ("_Refresh", "", ViewRefresh, null, null, Key.F5),
}),
new ("_Analyze", new MenuItem? [] {
new ("Diff Custom _Tool Output...", "", AnalyzeDiffCustomTool, BothFilesPresent, null, Key.ShiftMask | Key.T),
null,
new ("_Compare Selected File", "", AnalyzeCompareSelected, BothFilesPresent, null, Key.ShiftMask | Key.C),
new ("_Identify All Files", "", AnalyzeIdentifyAll, null, null, Key.ShiftMask | Key.I),
new ("Identify _Selected File", "", AnalyzeIdentifySelected, AnyFilesPresent, null, Key.ShiftMask | Key.J),
}),
new ("_Help", new MenuItem [] {
new ("About...", "", HelpAbout),
}),
});
static readonly Label app1label = new ("App A:") {
X = 1,
Y = 2,
Width = 6,
Height = 1,
};
static readonly TextField tf1 = new () {
X = Pos.Right (app1label) + 1,
Y = 2,
Width = Dim.Fill () - 1,
Height = 1,
ReadOnly = true,
};
static readonly Label app2label = new ("App B:") {
X = 1,
Y = 3,
Width = 6,
Height = 1,
};
static readonly TextField tf2 = new () {
X = Pos.Right (app2label) + 1,
Y = 3,
Width = Dim.Fill () - 1,
Height = 1,
Text = app2_path ?? "",
ReadOnly = true,
};
static readonly CompareTableView tv = new () {
Y = Pos.Bottom (tf2) + 1,
Height = Dim.Fill () - 1,
Width = Dim.Fill (),
};
static readonly StatusBar statusBar = new (new StatusItem [] {
new (Key.F1, "~F1~ Open A", FileOpenApp1),
new (Key.F2, "~F2~ Open B", FileOpenApp2),
new (Key.F5, "~F5~ Refresh", ViewRefresh),
new (Key.CtrlMask | Key.E, "~Ctrl+E~ Export", FileExport),
new (Key.CtrlMask | Key.G, "~Ctrl+G~ Gist", ViewGist),
new (Key.CtrlMask | Key.Q, "~Ctrl+Q~ Quit", FileQuit),
});
static string? app1_path;
static string? app2_path;
static Dictionary<string, string> mappings = new ();
public static int Start (string [] args, Dictionary<string, string> fileMappings)
{
mappings = fileMappings;
tf1.Text = app1_path = args.Length > 0 ? Path.GetFullPath (args [0]) : Environment.CurrentDirectory;
tf1.MouseClick += (e) => {
if (e.MouseEvent.Flags == MouseFlags.Button1Clicked) {
FileOpenApp1 ();
e.Handled = true;
}
};
tf2.Text = app2_path = args.Length > 1 ? Path.GetFullPath (args [1]) : Environment.CurrentDirectory;
tf2.MouseClick += (e) => {
if (e.MouseEvent.Flags == MouseFlags.Button1Clicked) {
FileOpenApp2 ();
e.Handled = true;
}
};
Application.Top.ColorScheme = Colors.Base;
Application.Top.Add (menu, app1label, tf1, app2label, tf2, tv, statusBar);
if ((app1_path is not null) && (app2_path is not null))
ViewRefresh ();
Application.Run ();
return 0;
}
static string? FileOpen (string? currentDirectory)
{
using OpenDialog d = new ("Open Application / Directory", "", null, OpenDialog.OpenMode.Directory);
d.DirectoryPath = currentDirectory ?? Environment.CurrentDirectory;
Application.Run (d);
return d.Canceled ? null : Path.GetFullPath (d.FilePath.ToString ()!);
}
static void FileOpenApp1 ()
{
var selected = FileOpen (app1_path);
if ((selected is not null) && (selected != app1_path)) {
tf1!.Text = app1_path = selected;
ViewRefresh ();
}
}
static void FileOpenApp2 ()
{
var selected = FileOpen (app2_path);
if ((selected is not null) && (selected != app2_path)) {
tf2!.Text = app2_path = selected;
ViewRefresh ();
}
}
static void FileOpenMappings ()
{
using OpenDialog d = new ("Open File Mappings", "", null, OpenDialog.OpenMode.File);
d.DirectoryPath = Environment.CurrentDirectory;
Application.Run (d);
if (!d.Canceled) {
mappings = Mappings.ReadFromFile (d.FilePath.ToString ()!, out var result);
if (result == 0)
ViewRefresh ();
}
}
static bool AreMappingsPresent ()
{
return mappings.Count > 0;
}
static void FileSaveMappings ()
{
using SaveDialog d = new ("Save File Mappings", "", new () { ".map" });
d.FilePath = Path.Combine (Environment.CurrentDirectory, "mappings.map");
Application.Run (d);
if (!d.Canceled) {
using StreamWriter writer = new (d.FilePath.ToString ()!);
foreach (var kvp in mappings)
writer.WriteLine ($"{kvp.Value}={kvp.Key}");
}
}
static void FileExport ()
{
using SaveDialog d = new ("Export Table", "", new () { ".md" });
Application.Run (d);
if (!d.Canceled && (d.FilePath is not null)) {
File.WriteAllText (d.FilePath.ToString ()!, Comparer.ExportMarkdown (new List<DataTable> { tv.Table }));
}
}
static void FileQuit ()
{
Application.Top.Running = false;
}
static void EditPairFiles ()
{
(FileInfo? fa, FileInfo? fb) = CurrentSelection;
if ((fa is null) && (fb is null))
return;
List<string> files = new ();
if (fa is null) {
// don't include matched files (paired or not)
foreach (DataRow row in tv.Table.Rows) {
(FileInfo? file1, long _) = ((FileInfo?, long)) row [1];
if (file1 is null)
continue;
(FileInfo? file2, long _) = ((FileInfo?, long)) row [2];
if (file2 is not null)
continue;
files.Add (file1.Name);
}
}
if (fb is null) {
foreach (DataRow row in tv.Table.Rows) {
(FileInfo? file2, long _) = ((FileInfo?, long)) row [2];
if (file2 is null)
continue;
(FileInfo? file1, long _) = ((FileInfo?, long)) row [1];
if (file1 is not null)
continue;
files.Add (file2.Name);
}
}
MappingsDialog d = new (fa?.Name, fb?.Name, files);
Application.Run (d);
if ((d.FileA is not null) && (d.FileB is not null)) {
mappings.TryAdd (d.FileB, d.FileA);
ViewRefresh ();
}
}
static bool IsPaired ()
{
var current = tv.Table.Rows [tv.SelectedRow];
return (current [0] as string)!.Contains (" -> ");
}
static void EditUnpairFiles ()
{
if (!IsPaired ())
return;
(FileInfo? _, FileInfo? fb) = CurrentSelection;
mappings.Remove (fb!.Name);
ViewRefresh ();
}
static void ViewGist ()
{
Comparer.Gist (new List<DataTable> { tv.Table });
}
static void ViewRefresh ()
{
if ((app1_path is not null) && (app2_path is not null)) {
tv.Table = Comparer.GetAppCompareTable (app1_path, app2_path, mappings);
if (tv.Table.ExtendedProperties ["Exception"] is Exception ex) {
MessageBox.ErrorQuery (60, 13, "Error", ex.ToString (), "_Ok");
}
tv.Refresh ();
}
}
static (FileInfo?, FileInfo?) CurrentSelection {
get {
var row = tv.Table.Rows [tv.SelectedRow];
(FileInfo? file1, long _) = ((FileInfo?, long)) row [1];
(FileInfo? file2, long _) = ((FileInfo?, long)) row [2];
return (file1, file2);
}
}
static bool BothFilesPresent ()
{
(FileInfo? fa, FileInfo? fb) = CurrentSelection;
return fa is not null && fb is not null;
}
static bool AnyFilesPresent ()
{
(FileInfo? fa, FileInfo? fb) = CurrentSelection;
return fa is not null || fb is not null;
}
static void AnalyzeCompareSelected ()
{
(FileInfo? fa, FileInfo? fb) = CurrentSelection;
if (fa is not null && fb is not null) {
tv.Table.Rows [tv.SelectedRow] [5] = CompareFiles.Compare (fa, fb);
tv.SetNeedsDisplay ();
}
}
public static void AnalyzeIdentifyAll ()
{
List<FileInfo> files = new (tv.Table.Rows.Count - 8);
foreach (DataRow row in tv.Table.Rows) {
(FileInfo? file1, long _) = ((FileInfo?, long)) row [1];
if (file1 is null) {
(FileInfo? file2, long _) = ((FileInfo?, long)) row [2];
if (file2 is not null)
files.Add (file2);
} else {
files.Add (file1);
}
}
int n = 0;
foreach (var filetype in Identifier.Identify (files)) {
tv.Table.Rows [n++] [5] = filetype;
}
tv.SetNeedsDisplay ();
}
public static void AnalyzeIdentifySelected ()
{
string filetype = "";
(FileInfo? fa, FileInfo? fb) = CurrentSelection;
if (fa is not null) {
filetype = Identifier.Identify (fa);
} else if (fb is not null) {
filetype = Identifier.Identify (fb);
}
if (filetype.Length > 0) {
tv.Table.Rows [tv.SelectedRow] [5] = filetype;
tv.SetNeedsDisplay ();
}
}
static void AnalyzeDiffCustomTool ()
{
(FileInfo? fa, FileInfo? fb) = CurrentSelection;
if (fa is not null && fb is not null) {
DiffCustomToolDialog dialog = new (fa, fb);
Application.Run (dialog);
}
}
static void HelpAbout ()
{
StringBuilder aboutMessage = new ();
aboutMessage.AppendLine ("An app bundle/directory comparison tool");
aboutMessage.AppendLine (@" __ _ _ __ _ __ ___ ___ _ __ ___ _ __ __ _ _ __ ___ ");
aboutMessage.AppendLine (@" / _` | '_ \| '_ \ / __/ _ \| '_ ` _ \| '_ \ / _` | '__/ _ \");
aboutMessage.AppendLine (@"| (_| | |_) | |_) | (_| (_) | | | | | | |_) | (_| | | | __/");
aboutMessage.AppendLine (@" \__,_| .__/| .__/ \___\___/|_| |_| |_| .__/ \__,_|_| \___|");
aboutMessage.AppendLine (@" | | | | | | ");
aboutMessage.AppendLine (@" |_| |_| |_| ");
aboutMessage.AppendLine ($"Version: {typeof (Program).Assembly.GetName ().Version}");
aboutMessage.AppendLine ("Copyright 2022 Sebastien Pouliot");
aboutMessage.AppendLine ("");
MessageBox.Query (62, 13, "About AppCompare", aboutMessage.ToString (), "_Ok");
}
}