-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
155 lines (132 loc) · 5.4 KB
/
Program.cs
File metadata and controls
155 lines (132 loc) · 5.4 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
using System;
using System.Text;
using System.IO;
namespace maple
{
class Program
{
static void Main(string[] args)
{
// initialize logger
// NOTE: maple directory will never be relative, since flag is set later
Log.InitializeLogger();
PrepareWindow();
Log.Write("Parsing arguments", "program");
// turn args into string for parser
string argString = "";
foreach(string s in args)
argString += s + " ";
argString.Trim(' ');
CommandParser.CommandInfo runInfo = CommandParser.Parse(
argString,
defaultPrimaryCommand: "load",
combineArgs: true
);
Log.Write("Loading settings", "program");
// load settings
Settings.LoadSettings();
Log.Write("Loading settings from switches", "program");
// load settings from switches
foreach(string sw in runInfo.Switches)
{
switch(sw)
{
case "--debug-tokens":
Settings.Properties.DebugTokens = true;
break;
case "--no-highlight":
Settings.Properties.NoHighlight = true;
break;
case "--no-tokenize":
Settings.Properties.NoTokenize = true;
break;
case "--cli-no-highlight":
Settings.Properties.CliNoHighlight = true;
break;
case "-ro":
case "--readonly":
Settings.Properties.ReadOnly = true;
break;
case "--relative-path":
Settings.Properties.RelativePath = true;
break;
case "-log":
case "--enable-logging":
Settings.Properties.EnableLogging = true;
break;
case "--summarize-log":
Settings.Properties.EnableLogging = true;
break;
default:
Log.Write("Encountered unknown switch '" + sw + "'", "program", important: true);
break;
}
}
// delete log file and pretend nothing happened if logging is actually disabled
if (!Settings.Properties.EnableLogging)
{
Log.DisableLogging();
}
// load file
if (args.Length > 0)
{
Log.Write("Initializing editor with file '" + runInfo.Args[0] + "'", "program");
Editor.Initialize(runInfo.Args[0]);
}
else // no argument provided
{
Log.Write("No file provided in args, creating Document with no filename", "program", important: true);
Editor.Initialize("");
}
// send control to editor
Log.Write("Entering editor input loop", "program");
Editor.BeginInputLoop();
}
static void PrepareWindow()
{
Printer.Initialize();
Printer.Clear();
Input.Initialize();
Console.Title = "maple";
Console.OutputEncoding = Encoding.UTF8;
Printer.Resize();
}
public static void ThrowFatalError(string message)
{
Log.Write("Throw fatal error: " + message, "program", important: true);
Input.RestorePreviousConsoleMode();
Console.ForegroundColor = Settings.Theme.ErrorColor;
Console.WriteLine("A fatal error occurred: " + message);
Console.ResetColor();
Environment.Exit(1);
}
public static void Close()
{
Log.Write("Session ended, cleaning up", "program");
Input.RestorePreviousConsoleMode();
Console.Clear();
Console.ForegroundColor = Settings.Theme.AccentColor;
Console.WriteLine("maple session ended");
if (Editor.CurrentDoc.GetAllLines().Count == 1 && Editor.CurrentDoc.GetLine(0).Length == 0 && Editor.CurrentDoc.NewlyCreated)
{
File.Delete(Editor.CurrentDoc.Filepath);
Log.Write("Cleaned empty, newly-created file '" + Editor.CurrentDoc.Filepath + "'", "program", important: true);
}
if (Settings.Properties.SummarizeLog)
{
Console.ForegroundColor = ConsoleColor.Cyan;
if (Settings.Properties.EnableLogging)
Console.WriteLine("{0} important/unusual log event(s) occurred in the last session", Log.ImportantEvents);
else
Console.WriteLine("Logging was disabled during this session; however, an empty log file still exists");
#if DEBUG
Console.WriteLine("The last session was run in a debugger, {0} additional event(s) are available", Log.DebugEvents);
#endif
Console.ResetColor();
Console.WriteLine("Log file is available at: {0}", Log.LogPath);
}
Console.ResetColor();
Environment.Exit(0);
}
}
}