-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
322 lines (278 loc) · 11.8 KB
/
Program.cs
File metadata and controls
322 lines (278 loc) · 11.8 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
/*
MIT License
Copyright (c) 2023 Monzu77
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.
*/
/*
,----,
,/ .`|
,` .' : ,--,
; ; / ,--.'|
.'___,/ ,' | | :
| : | : : '
; |.'; ; ,--.--. | ' | ,---.
`----' | | / \ ' | | / \
' : ;.--. .-. || | : / / |
| | ' \__\/: . .' : |__ . ' / |
' : | ," .--.; || | '.'|' ; /|
; |.' / / ,. |; : ;' | / |
'---' ; : .' \ , / | : |
| , .-./---`-' \ \ /
`--`---' `----'
A story-writer for your terminal.
*/
using Fclp;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.ComponentModel.Design.Serialization;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Numerics;
using System.Net.Mime;
namespace Tale
{
public class ApplicationArguments
{
public string? create { get; set; }
public bool list { get; set; } = false;
public string? delete { get; set; }
public string? date { get; set; }
public bool commands { get; set; } = false;
public string? view { get; set; }
public string? edit { get; set; }
}
public static class MainRenderer
{
public static async Task Main(string[] args)
{
// CommandLineParser
var p = new FluentCommandLineParser<ApplicationArguments>();
p.Setup(arg => arg.create)
.As('c', "create");
p.Setup(arg => arg.list)
.As('l', "list");
p.Setup(arg => arg.delete)
.As('d', "delete");
p.Setup(arg => arg.view)
.As('v', "view");
p.Setup(arg => arg.edit)
.As('e', "edit");
p.Setup(arg => arg.date)
.As("date");
p.Setup(arg => arg.commands)
.As("cmd");
var result = p.Parse(args);
// Journal
string? createName = p.Object.create??" ";
string? deleteName= p.Object.delete??" ";
bool? list = p.Object.list;
// The path where StreamWriter/Reader will write or read a .txt file
string path = Environment.CurrentDirectory+@$"\journals";
// If Directory does not exist.
if(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
// Help command
try
{
// If --command == true
if (p.Object.commands == true)
{
Console.WriteLine(" Here's a list of all the commands and a brief explanation on what they do: ");
Console.WriteLine("");
Console.WriteLine(" cmd => Shows the list of all commands");
Console.WriteLine(" create => Creates a new Journal. Example: tale.cs --create {name}");
Console.WriteLine(" delete => Deletes a mentioned Journal. Example: tale.cs --delete {name}");
Console.WriteLine(" date => Adds date to a Journal if mentioned. Example: tale.cs --create {name} --date {givenDate}");
Console.WriteLine(" list => Shows the list of all existing Journals. Example: tale.cs --list");
Console.WriteLine("");
}
}
catch{
}
try
{
// Create Journal
//If --create does not == null
if (result.HasErrors == false && !createName.Equals(" "))
{
Console.WriteLine("");
Console.WriteLine(" Write your Journal below: ");
Console.WriteLine("");
var includedParts = new info();
includedParts.date = p.Object.date;
includedParts.content = Console.ReadLine();
// Serializing into Json
var serializeJournal = Newtonsoft.Json.JsonConvert.SerializeObject(includedParts);
// Writing file
using (StreamWriter writer = new StreamWriter(path+$@"\{createName}.txt"))
{
writer.Write(serializeJournal);
// Return message
Console.WriteLine("");
Console.WriteLine(" Your Journal has been saved!");
Console.WriteLine("");
}
}
}catch (Exception e) {
Console.WriteLine("\n An error has occured! Are you sure that your file name is mentioned properly?\n\n"+e+"\n");
}
// Show Journal List
try
{
// If --list command == true
if(p.Object.list == true)
{
// Read files in directory
var files = Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly);
int amount = files.Count();
Console.WriteLine("");
Console.WriteLine($" You have {amount} Journal(s) in your library!");
Console.WriteLine("");
// Display files
foreach (string file in files)
{
//Deserializing content for Date
string fileContent;
using(StreamReader reader = new StreamReader(file))
{
fileContent = reader.ReadToEnd();
}
var DeserializedFile = Newtonsoft.Json.JsonConvert.DeserializeObject<info>(fileContent);
// If date exists
if(String.IsNullOrEmpty(DeserializedFile?.date) == false)
{;
Console.WriteLine(Path.GetFileNameWithoutExtension(" "+file)+ $" [{DeserializedFile.date}]");
}
// If date does not exist
if(String.IsNullOrEmpty(DeserializedFile?.date) == true)
{
Console.WriteLine(Path.GetFileNameWithoutExtension(" "+file));
}
}
Console.WriteLine("");
}
}catch (Exception e){
Console.WriteLine("\n An error has occured! Are you sure you have any Journals in your library?");
Console.WriteLine("");
Console.WriteLine(e);
}
//Delete Journal
// If --deleteName does not == null && File existss
if (result.HasErrors == false && !deleteName.Equals(" ") && File.Exists(Path.Combine(path, deleteName+".txt")))
{
var files = Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly);
// If exists
if(File.Exists(Path.Combine(path, deleteName+".txt")))
{
File.Delete(Path.Combine(path, deleteName+".txt"));
Console.WriteLine("");
Console.WriteLine("The file has been deleted!");
Console.WriteLine("");
}
}
// If --deleteName does not == null && File existss
if(result.HasErrors == false && !deleteName.Equals(" ") && !File.Exists(Path.Combine(path, deleteName+".txt")))
{
Console.WriteLine("");
Console.WriteLine("The file does not exist.");
Console.WriteLine("");
}
// View Journal
string? viewJournal = p.Object.view??" ";
if (!viewJournal.Equals(" "))
{
string fileContent;
using(StreamReader reader = new StreamReader(path+$@"\{viewJournal}.txt"))
{
fileContent = reader.ReadToEnd();
}
var DeserializedFile = Newtonsoft.Json.JsonConvert.DeserializeObject<info>(fileContent);
if(String.IsNullOrEmpty(DeserializedFile?.date) == false)
{
Console.WriteLine("");
Console.WriteLine("Name: "+viewJournal+" Date: "+DeserializedFile?.date);
Console.WriteLine("");
Console.WriteLine("Content:");
Console.WriteLine(DeserializedFile?.content);
Console.WriteLine("");
}
if(String.IsNullOrEmpty(DeserializedFile?.date) == true)
{
Console.WriteLine("");
Console.WriteLine("Name: "+viewJournal );
Console.WriteLine("");
Console.WriteLine("Content:");
Console.WriteLine(DeserializedFile?.content);
Console.WriteLine("");
}
}
// Edit Journal
string? editJournal = p.Object.edit??" ";
if (!editJournal.Equals(" "))
{
string fileContent;
using(StreamReader reader = new StreamReader(path+$@"\{editJournal}.txt"))
{
fileContent = reader.ReadToEnd();
}
var DeserializedFile = Newtonsoft.Json.JsonConvert.DeserializeObject<info>(fileContent);
Console.WriteLine("");
Console.WriteLine("Please write the part that you want to edit: ");
Console.WriteLine("");
string? subject = Console.ReadLine();
// If not null
if(!String.IsNullOrEmpty(subject))
{
Console.WriteLine("You want to replace it with:");
Console.WriteLine("");
string? replacer = Console.ReadLine();
// If subject exists in DeserializedFile.content
if(DeserializedFile.content.Contains(subject))
{
// Replace the text
string fileRead = File.ReadAllText(path+$@"\{editJournal}.txt");
fileRead = fileRead.Replace(subject, replacer);
// Write the file
File.WriteAllText(path+$@"\{editJournal}.txt", fileRead);
Console.WriteLine("Your Journal has been edited succesfully!");
Console.WriteLine("");
}
// If subject doesn't exist in DeserializedFile.content
if(!DeserializedFile.content.Contains(subject))
{
Console.WriteLine("The part you wanted to edit does not exist!");
Console.WriteLine("");
}
}
// If null
if(String.IsNullOrEmpty(subject))
{
Console.WriteLine("You have entered a null value!");
}
}
}
}
public class info
{
public string? content { get; set; }
public string? date { get; set; }
}
}