-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
119 lines (106 loc) · 4.17 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
namespace InSysOut
{
class Program
{
static void Main(string[] args)
{
try
{
string targ = string.Join("", args).ToLower(); //Quick hack to trim and lower the trolls (typos).
if (string.IsNullOrWhiteSpace(targ))
{
Console.WriteLine(Constants.helpMsg);
}
else
{
if (targ.Equals("-h") || targ.Equals("--help"))
{
Console.WriteLine(Constants.helpMsg);
}
else if (targ.Equals("-i") || targ.Equals("--info"))
{
Console.WriteLine(Constants.infoMsg);
}
else if (targ.Equals("-v") || targ.Equals("--version"))
{
Console.WriteLine(Constants.versionMsg);
//int i = 0; int f = i / 0;//Emulate Exception.
}
else if (targ.Equals("ls") || targ.Equals("dir"))
{
string curdir = Directory.GetCurrentDirectory();
string path = Path.Join(curdir, Path.DirectorySeparatorChar.ToString());
string[] files = Directory.GetFiles(curdir);
FilesObj fo = new FilesObj();
fo.Path = path;
fo.Files = new string[files.Length];
for(int i=0;i<files.Length;i++)
{
string fname = files[i].Replace(path, "");
fo.Files[i]=fname;
}
var jsOpt = new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
string jsonString = JsonSerializer.Serialize(fo, jsOpt);
Console.WriteLine(jsonString);
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(Constants.unknownInputMsg);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(Constants.helpMsg);
}
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);
Console.ForegroundColor = ConsoleColor.Gray;
}
//ToDos:
//Support pipes (| oj) and (< f.txt). ref: https://stackoverflow.com/questions/199528/c-sharp-console-receive-input-with-pipe?answertab=oldest#tab-top
//Remark: its Million dollar mistake to create json manually using StringBuilder! avoid Serialization issues by using objs.
}
static string FormatJsonText(string jsonString)
{
using var doc = JsonDocument.Parse(
jsonString,
new JsonDocumentOptions
{
AllowTrailingCommas = true
}
);
MemoryStream memoryStream = new MemoryStream();
using (
var utf8JsonWriter = new Utf8JsonWriter(
memoryStream,
new JsonWriterOptions
{
Indented = true
}
)
)
{
doc.WriteTo(utf8JsonWriter);
}
return new UTF8Encoding()
.GetString(memoryStream.ToArray());
}
}
public class FilesObj
{
public string Path { get; set; }
public string[] Files { get; set; }
}
}