-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
153 lines (128 loc) · 5.37 KB
/
Copy pathProgram.cs
File metadata and controls
153 lines (128 loc) · 5.37 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
namespace locus;
public static class SysInfo
{
public static string Username = "user";
public static string Hostname = "locus";
public static bool setup = false;
}
public class Program
{
Node root = new Node("root", true);
Node currentFolder;
static void Main()
{
bool running = true;
Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true;
running = false;
Resonate.Current?.Dispose();
Jot.Current?.Dispose();
System.Environment.Exit(0);
};
Console.Clear();
Console.WriteLine("Hello, World! Welcome to my rudimentary file system.");
Console.WriteLine("Use the help command to see available commands.");
Program program = new Program();
program.currentFolder = program.root;
Node home = new Node("home", true, program.currentFolder, DateTime.Now);
program.currentFolder.Children.Add(home);
program.currentFolder = home;
while (running)
{
if (running)
{
Console.Write($"{SysInfo.Username}@{SysInfo.Hostname}:/\u001b[34m\u001b[1m" + program.root.FullPath + "\u001b[0m" + "> ");
}
program.console();
}
}
public class FileSystemEntry
{
public string Name { get; set; }
public bool IsDirectory { get; set; }
public List<byte> Content { get; set; } // only used if IsDirectory is false
public List<FileSystemEntry> Children { get; set; } // only used if IsDirectory is true
}
public void console(bool script = false, string input = "")
{
var commands = new Dictionary<string, string>
{
["help"] = "Displays a list of available commands.",
["ls"] = "Lists the contents of the current directory.",
["cd"] = "Changes the current directory.\n\nUsage: cd <directory_name> or cd .. to go up.",
["mkdir"] = "Creates a new directory.\n\nUsage: mkdir <directory_name>",
["touch"] = "Creates a new file.\n\nUsage: touch <file_name>",
["rm"] = "Removes a file or directory.\n\nUsage: rm <name>",
["echo"] = "Prints text to the console.\n\nUsage: echo <text>",
["cat"] = "Displays the contents of a file.\n\nUsage: cat <file_name>",
["write"] = "Edits the contents of a file using a line editor.\n\nUsage: write <file_name>",
["jot"] = "Edits the contents of a file using a TUI.\n\nUsage: jot <file_name>",
["coda"] = "Edits the raw hex contents of a file using a line editor.\n\nUsage: coda <file_name>",
["script"] = "Executes commands from a script file.\n\nUsage: script <file_name>",
["push"] = "Pushes a file from the virtual filesystem to the host filesystem.\n\nUsage: push <file_name> <absolute_host_path_to_file>",
["pull"] = "Pulls a file from the host filesystem into the virtual filesystem.\n\nUsage: pull <absolute_host_path_to_file> <file_name>",
["glyph"] = "Renders an image file as ASCII art in the console.\n\nUsage: glyph <file_name>",
["info"] = "Displays information about a file or directory.\n\nUsage: info <name>",
["man"] = "Hey... you already know how to use the manual!\n\nUsage: man <command>",
["resonate"] = "Plays an audio file using a TUI.\n\nUsage: resonate <file_name>",
["loom"] = "Executes a C# file.\n\nUsage: loom <file_name>",
["username"] = "Sets the username.\n\nUsage: username <new_username>",
["hostname"] = "Sets the hostname.\n\nUsage: hostname <new_hostname>",
["fitr"] = "Text-based choose your own adventure game.\n\nUsage: fitr <file_name>",
};
string command = string.Empty;
if (!script)
{
input = Console.ReadLine() ?? string.Empty;
}
if (!SysInfo.setup)
{
Commands.Init(ref root);
SysInfo.setup = true;
}
command = GetCommand(input);
if (string.IsNullOrEmpty(command))
{
return;
}
if (commands.ContainsKey(command))
{
Console.Write("\u001b[0m");
Commands.Run(command, input, commands, ref currentFolder, ref root, this, ref SysInfo.Username, ref SysInfo.Hostname);
}
else
{
Console.WriteLine("unknown command :(\ntype 'help' for a list of commands, and 'man <command>' for command-specific help");
}
}
public static string GetCommand(string input)
{
if (string.IsNullOrWhiteSpace(input))
return string.Empty;
input = input.Trim();
string[] parts = input.Split(
new char[] { ' ', '\t', '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries
);
if (parts.Length == 0)
return string.Empty;
string firstWord = new string(parts[0]
.SkipWhile(c => !char.IsLetterOrDigit(c))
.ToArray());
return firstWord;
}
public static string GetArgs(string input)
{
if (string.IsNullOrWhiteSpace(input))
{
return string.Empty;
}
int firstSpaceIndex = input.IndexOf(' ');
if (firstSpaceIndex == -1)
{
return string.Empty;
}
return input[(firstSpaceIndex + 1)..].Trim();
}
}