-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCatOptions.cs
More file actions
445 lines (376 loc) · 18.5 KB
/
CatOptions.cs
File metadata and controls
445 lines (376 loc) · 18.5 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//
// Copyright (C) 2008-2013 Kody Brown (kody@bricksoft.com).
//
// MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Bricksoft.PowerCode;
namespace cat
{
public class CatOptions
{
//private Config config = null;
private static EnvironmentVariables envars = null;
public ConsoleColor defaultBackColor { get; set; }
public ConsoleColor defaultForeColor { get; set; }
public ConsoleColor lineNumBackColor { get; set; }
public ConsoleColor lineNumForeColor { get; set; }
public string appname = "cat";
public bool useStdIn { get; set; }
public List<string> files { get; set; }
public bool showHelp { get; set; }
public bool showPlugins { get; set; }
public bool showEnvVars { get; set; }
public bool showLineNumbers { get; set; }
public bool wrapText { get; set; }
public bool forcePlainText { get; set; }
public string forceSpecificPlugin { get; set; }
public bool pauseAfterEachPage { get; set; }
public bool pauseAtEnd { get; set; }
public string ignoreLines { get; set; }
public bool ignoreBlankLines { get; set; }
public bool ignoreWhitespaceLines { get; set; }
public int indentCharacters { get; set; }
public bool normalExpanded { get; set; }
public bool extraExpanded { get; set; }
public CatOptions()
{
envars = new EnvironmentVariables("cat");
appname = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);
showHelp = false;
showPlugins = false;
showEnvVars = false;
defaultBackColor = Console.BackgroundColor;
defaultForeColor = Console.ForegroundColor;
lineNumBackColor = ConsoleColor.Blue;
lineNumForeColor = ConsoleColor.Gray;
files = new List<string>();
showLineNumbers = false;
wrapText = false;
forcePlainText = false;
forceSpecificPlugin = "";
pauseAfterEachPage = false;
pauseAtEnd = false;
ignoreLines = "";
ignoreBlankLines = false;
ignoreWhitespaceLines = false;
indentCharacters = 0;
normalExpanded = true;
extraExpanded = true; // false
}
public void displayHeader()
{
int width = Console.WindowWidth;
//Console.WriteLine(appname + ".exe");
//Console.WriteLine(Text.WrapIf(wrapText,appname + ".exe - Displays the contents of the file(s) specified.", width, 0));
Console.WriteLine(Text.Wrap("Created by Kody Brown (kody@bricksoft.com)", width, 0));
//Console.WriteLine(Text.WrapIf(wrapText,"Released under the MIT License. https://github.com/kodybrown/cat", width, 0));
Console.WriteLine(Text.Wrap("https://github.com/kodybrown/cat", width, 0));
Console.WriteLine();
}
public void displaySynopsis()
{
int width = Console.WindowWidth,
ind = 2;
Console.WriteLine("SYNOPSIS:");
if (extraExpanded) { Console.WriteLine(); }
Console.WriteLine(Text.Wrap("Displays the contents of the file(s) specified.", width, ind));
if (normalExpanded) { Console.WriteLine(); }
}
public void displayUsage()
{
int width = Console.WindowWidth,
ind = 2,
ind2 = 12,
ind2b = 20;
displayHeader();
displaySynopsis();
Console.WriteLine("USAGE:");
if (extraExpanded) { Console.WriteLine(); }
Console.WriteLine(Text.Wrap(appname + ".exe [options] file [file...n]", width, ind));
if (normalExpanded) { Console.WriteLine(); }
Console.WriteLine(Text.Wrap("file The name of the file to display. Enclose file names within quotes if it includes a space.", width, ind, ind2));
Console.WriteLine(Text.Wrap("<stdin> Content can be piped via stdin, as well.", width, ind, ind2));
if (normalExpanded) { Console.WriteLine(); }
Console.WriteLine(Text.Wrap("-p Pauses after each screenful (applies -pp).", width, ind, ind2));
Console.WriteLine(Text.Wrap("-pp Pauses at the end.", width, ind, ind2));
if (normalExpanded) { Console.WriteLine(); }
Console.WriteLine(Text.Wrap("-l Include line numbers in the output.", width, ind, ind2));
Console.WriteLine(Text.Wrap("-w Nicely wrap lines longer than window width.", width, ind, ind2));
if (normalExpanded) { Console.WriteLine(); }
Console.WriteLine(Text.Wrap("-ib Ignore blank lines.", width, ind, ind2));
Console.WriteLine(Text.Wrap("-ibw Ignore blank and whitespace lines.", width, ind, ind2));
Console.WriteLine(Text.Wrap("-il:xyz Ignore lines starting with 'xyz'.", width, ind, ind2));
//if (normalExpanded) { Console.WriteLine(); }
Console.WriteLine(Text.Wrap("-i:n Lines that are wrapped are automatically indented `n` spaces. This affects every line after the first.", width, ind, ind2));
if (normalExpanded) { Console.WriteLine(); }
Console.WriteLine(Text.Wrap("-f Forces plain text display (ignores plugins).", width, ind, ind2));
Console.WriteLine(Text.Wrap("-force-plugin:name", width, ind, ind2));
Console.WriteLine(Text.Wrap(" Forces the plugin 'name' whether the plugin is registered for that file type or not.", width, ind, ind2));
if (normalExpanded) { Console.WriteLine(); }
//Console.WriteLine(Text.WrapIf(wrapText,"* Enclose file names within quotes if it includes a space.", width, ind, ind + 2));
Console.WriteLine(Text.Wrap("* You can reverse the effect of a flag, by prefixing it with a bang (!). This is useful when you need to override an environment variable.", width, ind, ind + 2));
if (normalExpanded) { Console.WriteLine(); }
Console.WriteLine("PLUGINS:");
if (extraExpanded) { Console.WriteLine(); }
Console.WriteLine(Text.Wrap("--show-plugins Displays all plugins that are available.", width, ind, ind2b));
if (normalExpanded) { Console.WriteLine(); }
Console.WriteLine(Text.Wrap(string.Format("Plugins must be in the same directory as {0}.exe and must match the file pattern: `{0}.*.dll`. See https://github.com/kodybrown/cat for more details.", appname), width, 2));
if (normalExpanded) { Console.WriteLine(); }
Console.WriteLine("ENVIRONMENT VARIABLES:");
if (extraExpanded) { Console.WriteLine(); }
Console.WriteLine(Text.Wrap("--show-envars Displays all environment variables.", width, ind, ind2b));
if (extraExpanded) { Console.WriteLine(); }
Console.WriteLine(Text.Wrap(string.Format("These values can be set in your environment so you don't have to type them into the command-line every time you run `{0}.exe`.", appname), width, ind));
if (normalExpanded) { Console.WriteLine(); }
Console.WriteLine(Text.Wrap(string.Format("To set a value, prefix the (short) command-line argument name with `{0}`. The values are the same as you would use for the command-line.", appname), width, ind));
if (normalExpanded) { Console.WriteLine(); }
Console.WriteLine(" Examples:");
if (extraExpanded) { Console.WriteLine(); }
Console.WriteLine(" > SET {0}_l=true", appname);
Console.WriteLine(" > SET {0}_w=true", appname);
Console.WriteLine(" > SET {0}_ib=true", appname);
Console.WriteLine(" > SET {0}_ibw=true", appname);
Console.WriteLine(" > SET {0}_il=xyz", appname);
Console.WriteLine(" > SET {0}_f=true", appname);
if (normalExpanded) { Console.WriteLine(); }
Console.WriteLine(Text.Wrap("* Only the examples displayed are considered valid environment variables.", width, ind, ind + ind));
Console.WriteLine(Text.Wrap("* Arguments specified on the command-line will always override environment variables.", width, ind, ind + ind));
Console.WriteLine(Text.Wrap("* Consult your operating system help for information on how to set environment variables for all sessions.", width, ind, ind + ind));
if (normalExpanded) { Console.WriteLine(); }
}
public void displayPlugins()
{
int width = Console.WindowWidth,
ind = 2;
List<string> files = new List<string>();
string exe = Assembly.GetEntryAssembly().Location;
string dir = Path.GetDirectoryName(exe);
Assembly driver_module;
ICataloger driver;
List<Type> types;
displayHeader();
//displaySynopsis();
Console.WriteLine(Text.Wrap("SHOWING AVAILABLE PLUGINS:", width, 0));
if (extraExpanded) { Console.WriteLine(); }
Console.WriteLine(Text.Wrap("Location: " + dir, width, ind, ind + 18));
files.Add(exe);
files.AddRange(Directory.GetFiles(dir, Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location) + ".*.dll", SearchOption.TopDirectoryOnly));
foreach (string f in files) {
if (normalExpanded) { Console.WriteLine(); }
Console.WriteLine(Text.Wrap(Path.GetFileName(f), width, ind));
if (extraExpanded) { Console.WriteLine(); }
// Instantiate each assembly and call Initialize on all enabled plugins.
try {
driver_module = Assembly.LoadFile(f);
} catch (Exception) {
continue;
}
if (driver_module == null) {
continue;
}
try {
types = new List<Type>(driver_module.GetTypes());
} catch (Exception) {
continue;
}
for (int j = 0; j < types.Count; j++) {
if (types[j].GetInterface("ICataloger") != null) {
try {
driver = Activator.CreateInstance(types[j]) as ICataloger;
if (driver != null) {
Console.WriteLine(Text.Wrap(driver.GetType().Name + ": " + driver.Description, width, ind + ind));
}
} catch (Exception ex) {
Console.WriteLine(ex.Message);
continue;
}
}
}
}
if (normalExpanded) { Console.WriteLine(); }
}
public void displayEnvVars()
{
int width = Console.WindowWidth,
ind = 2,
pad = (appname + "_ibw").Length + 1;
displayUsage();
Console.WriteLine(Text.Wrap("SHOWING ENVIRONMENT VARIABLES:", width, 0));
if (extraExpanded) { Console.WriteLine(); }
displayBoolEnvVar(appname + "_l", pad, width, ind);
displayBoolEnvVar(appname + "_w", pad, width, ind);
displayBoolEnvVar(appname + "_ib", pad, width, ind);
displayBoolEnvVar(appname + "_ibw", pad, width, ind);
displayStringEnvVar(appname + "_il", pad, width, ind);
displayBoolEnvVar(appname + "_f", pad, width, ind);
if (normalExpanded) { Console.WriteLine(); }
}
private void displayBoolEnvVar( string varname, int pad, int width, int ind )
{
if (envars.contains(varname)) {
Console.WriteLine(Text.Wrap(string.Format("{0,-" + pad + "} = {1}", varname, envars.attr<bool>(varname).ToString().ToLower()), width, ind));
} else {
Console.WriteLine(Text.Wrap(string.Format("{0,-" + pad + "} = <not set>", varname), width, ind));
}
}
private void displayStringEnvVar( string varname, int pad, int width, int ind )
{
if (envars.contains(varname)) {
Console.WriteLine(Text.Wrap(string.Format("{0,-" + pad + "} = '{1}'", varname, envars.attr<string>(varname)), width, ind));
} else {
Console.WriteLine(Text.Wrap(string.Format("{0,-" + pad + "} = <not set>", varname), width, ind));
}
}
public static CatOptions LoadOptions( string[] arguments )
{
CatOptions catOptions;
string appname;
catOptions = new CatOptions();
appname = catOptions.appname;
catOptions.useStdIn = StdInEx.IsInputRedirected;
// Load the environment variables..
if (envars.contains("l")) {
catOptions.showLineNumbers = envars.attr<bool>(appname + "_l");
}
if (envars.contains(appname + "_w")) {
catOptions.wrapText = envars.attr<bool>(appname + "_w");
}
if (envars.contains(appname + "_ib")) {
catOptions.ignoreBlankLines = envars.attr<bool>(appname + "_ib");
}
if (envars.contains(appname + "_ibw")) {
catOptions.ignoreWhitespaceLines = envars.attr<bool>(appname + "_ibw");
}
if (envars.contains(appname + "_il")) {
catOptions.ignoreLines = envars.attr<string>(appname + "_il");
}
if (envars.contains(appname + "_f")) {
catOptions.forcePlainText = envars.attr<bool>(appname + "_f");
}
if (envars.contains(appname + "_forcePlugin")) {
catOptions.forceSpecificPlugin = envars.attr<string>(appname + "_forcePlugin");
}
// Load the command-line arguments..
foreach (string a in arguments) {
if ((a.StartsWith("/") || a.StartsWith("-") || a.StartsWith("!")) && !a.StartsWith("/~/") && !a.StartsWith("/c/")) {
string arg = a;
while (arg.StartsWith("/") || arg.StartsWith("-")) {
arg = arg.Substring(1);
}
if (arg.Equals("?", StringComparison.CurrentCultureIgnoreCase) || arg.StartsWith("h", StringComparison.CurrentCultureIgnoreCase)) {
// ?, h, help
catOptions.showHelp = true;
} else if (arg.Equals("pp", StringComparison.CurrentCultureIgnoreCase) || arg.StartsWith("pause-", StringComparison.CurrentCultureIgnoreCase)) {
// pp, pause-at, pause-at-end
catOptions.pauseAtEnd = true;
} else if (arg.Equals("!pp", StringComparison.CurrentCultureIgnoreCase) || arg.StartsWith("!pause-", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.pauseAtEnd = false;
} else if (arg.StartsWith("p", StringComparison.CurrentCultureIgnoreCase)) {
// p, pause
catOptions.pauseAfterEachPage = true;
} else if (arg.StartsWith("!p", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.pauseAfterEachPage = false;
} else if (arg.StartsWith("l", StringComparison.CurrentCultureIgnoreCase)) {
// l, lines, line-numbers
catOptions.showLineNumbers = true;
} else if (arg.StartsWith("!l", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.showLineNumbers = false;
} else if (arg.StartsWith("w", StringComparison.CurrentCultureIgnoreCase)) {
// w, wrap, wrap-lines
catOptions.wrapText = true;
} else if (arg.StartsWith("!w", StringComparison.CurrentCultureIgnoreCase) || arg.Equals("no-wrap", StringComparison.CurrentCultureIgnoreCase)) {
// !w, !wrap, !wrap-lines, no-wrap
catOptions.wrapText = false;
} else if (arg.StartsWith("force-plugin:", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.forceSpecificPlugin = arg.Substring(13);
} else if (arg.StartsWith("!force-plugin:", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.forceSpecificPlugin = "";
} else if (arg.StartsWith("f", StringComparison.CurrentCultureIgnoreCase)) {
// f, force, force-text, force-plain-text
catOptions.forcePlainText = true;
} else if (arg.StartsWith("!f", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.forcePlainText = false;
} else if (arg.Equals("ib", StringComparison.CurrentCultureIgnoreCase) || arg.StartsWith("ignore-b", StringComparison.CurrentCultureIgnoreCase)) {
// ib, ignore-b, ignore-blank, ignore-blank-lines
catOptions.ignoreBlankLines = true;
} else if (arg.Equals("!ib", StringComparison.CurrentCultureIgnoreCase) || arg.StartsWith("!ignore-b", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.ignoreBlankLines = false;
} else if (arg.Equals("ibw", StringComparison.CurrentCultureIgnoreCase) || arg.StartsWith("ignore-w", StringComparison.CurrentCultureIgnoreCase)) {
// ibw, ignore-w, ignore-white, ignore-whitespace, ignore-whitespace-lines
catOptions.ignoreBlankLines = true;
catOptions.ignoreWhitespaceLines = true;
} else if (arg.Equals("!ibw", StringComparison.CurrentCultureIgnoreCase) || arg.StartsWith("!ignore-w", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.ignoreWhitespaceLines = false;
} else if (arg.StartsWith("il:", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.ignoreLines = arg.Substring(3);
} else if (arg.StartsWith("ignore:", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.ignoreLines = arg.Substring(7);
} else if (arg.StartsWith("ignore-lines:", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.ignoreLines = arg.Substring(13);
} else if (arg.Equals("!ib", StringComparison.CurrentCultureIgnoreCase) || arg.StartsWith("!ignore-b", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.ignoreBlankLines = false;
} else if (arg.StartsWith("i:", StringComparison.CurrentCultureIgnoreCase)) {
// i:n
int val;
if (int.TryParse(arg.Substring(2), out val)) {
catOptions.indentCharacters = val;
} else {
Console.WriteLine("Invalid value for `/i`: {0}", arg.Substring(2));
}
} else if (arg.StartsWith("indent:", StringComparison.CurrentCultureIgnoreCase)) {
// indent:n
int val;
if (int.TryParse(arg.Substring(7), out val)) {
catOptions.indentCharacters = val;
} else {
Console.WriteLine("Invalid value for `/indent`: {0}", arg.Substring(7));
}
} else if (arg.Equals("expand", StringComparison.CurrentCultureIgnoreCase) || arg.Equals("!compress", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.normalExpanded = true;
catOptions.extraExpanded = true;
} else if (arg.Equals("!expand", StringComparison.CurrentCultureIgnoreCase) || arg.Equals("compress", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.normalExpanded = false;
catOptions.extraExpanded = false;
} else if (arg.StartsWith("normal", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.normalExpanded = true;
} else if (arg.StartsWith("!normal", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.normalExpanded = false;
} else if (arg.StartsWith("extra", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.extraExpanded = true;
} else if (arg.StartsWith("!extra", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.extraExpanded = false;
} else if (arg.StartsWith("show-pl", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.showPlugins = true;
} else if (arg.StartsWith("show-env", StringComparison.CurrentCultureIgnoreCase)) {
catOptions.showEnvVars = true;
}
} else {
catOptions.files.Add(a);
}
}
return catOptions;
}
}
}