-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.cs
More file actions
191 lines (174 loc) · 6.94 KB
/
Helpers.cs
File metadata and controls
191 lines (174 loc) · 6.94 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
using Terminal.Gui;
using System.IO;
using System;
namespace win32editor
{
internal static class Helpers
{
private static int originalCopylen { get; set; }
public static void LoadFile()
{
try
{
if (Editor.fileName != null)
{
string s = "";
byte[] buffer = File.ReadAllBytes(Editor.fileName);
// BUGBUG: #452 TextView.LoadFile keeps file open and provides no way of closing it
//_textView.LoadFile(fileName);
for (int i = 0; i < buffer.Length; i++)
{
s += (char)buffer[i];
}
//FUTURE ME: Please set a config options for file types with tabs to spaces.
// big problem we on accident formatt peoples files.
// maybe the editor has it own formatt. and we save the users original formatting.
// then when we go save the file, we reinstate the original formatting.
string removeTabs = s.Replace("\t", " ");
string removeNewLines = removeTabs.Replace("\r\n", "\n");
Editor.textField.Text = removeNewLines;
originalCopylen = Editor.textField.Text.Length; // not set to the length of removeNewLines because the TextView Element adds to legnth.
Editor.wind.Title = Editor.fileName;
Editor._saved = true;
Editor.fileLoaded = true;
}
}
catch (IOException iOException)
{
MessageBox.ErrorQuery("Oh No!", iOException.ToString(), "ok");
}
catch (Exception err)
{
MessageBox.ErrorQuery("Oh No!", $"Unknown Exception {err}", "ok");
}
}
public static void Open()
{
var fileSaved = CheckTextState();
if (!fileSaved)
{
var s = MessageBox.Query(5, 5, "Save", "This document has changed since you Opened it. Would you like to save", "yes", "no");
if (s == 0)
{
try
{
File.WriteAllText(Editor.fileName, Editor.textField.Text.ToString());
Editor._saved = true;
Application.RequestStop();
}
catch (Exception)
{
MessageBox.ErrorQuery("Oh No!", "Error Opening File", "ok");
}
}
}
string checkedDir;
if (Path.GetDirectoryName(Editor.fileName) != null)
{
checkedDir = Path.GetDirectoryName(Editor.fileName);
}
else
{
checkedDir = Directory.GetCurrentDirectory();
}
var d = new OpenDialog("Open", "Open a file") { AllowsMultipleSelection = false, DirectoryPath = checkedDir };
Application.Run(d);
if (!d.Canceled)
{
try
{
Editor.fileName = d.FilePaths[0];
LoadFile();
}
catch (Exception)
{
MessageBox.ErrorQuery("Oh No!", "File you tried to access was a directory.", "ok");
LoadFile();
}
}
}
private static bool CheckTextState()
{
switch (Editor.fileLoaded)
{
// this first condition checks to see if there is a file in the context.
// checks to see if actual original textview length is greater than the current length of the TextView element.
case true when originalCopylen != Editor.textField.Text.Length:
return false;
case true:
return true;
default:
{
switch (Editor.textField.Text.Length > 1)
{
// TODO: try to create new file if user tries to save scratch pad file
// this works some how please do not touch -> scratch pad functionality
case true when !Editor.fileLoaded:
return false;
default:
return true;
}
}
}
}
public static void DisplayCursorPos()
{
MessageBox.Query(1,5, "CursorPosition", $"Row:{Editor.textField.CurrentRow}\n Column:{Editor.textField.CurrentColumn}","ok");
}
public static void Quit()
{
var fileSaved = CheckTextState();
if (!fileSaved)
{
var s = MessageBox.Query(5, 5, "Save", "This document has changed since you Opened it. Would you like to save", "yes", "no");
if (s == 0)
{
try
{
File.WriteAllText(Editor.fileName, Editor.textField.Text.ToString());
Editor._saved = true;
Application.RequestStop();
}
catch (Exception)
{
MessageBox.ErrorQuery("Oh No!", "Error Writing to file", "ok");
}
}
else if (s == 1)
{
{
Application.RequestStop();
}
}
}
}
public static void Save()
{
if (Editor.fileName != null)
{
var s = MessageBox.Query(5, 5, "Save", "Would you like to save", "yes", "no");
if (s == 0)
{
try
{
File.WriteAllText(Editor.fileName, Editor.textField.Text.ToString());
Editor._saved = true;
}
catch (Exception)
{
MessageBox.ErrorQuery("Oh No!", "Error Writing to file", "ok");
}
}
else
{
LoadFile();
}
// BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
// As a result files saved on Windows and then read back will show invalid chars.
} else
{
MessageBox.ErrorQuery("save","There is no file to save.", "ok");
}
}
}
}