Create empty file if input doesn't exist

This commit is contained in:
Zachary Yedidia
2016-03-12 16:07:04 -05:00
parent 8072756990
commit 934634507d
4 changed files with 22 additions and 3 deletions

View File

@@ -50,18 +50,23 @@ class Buffer {
void remove(ulong start, ulong end) {
text.remove(start, end);
update();
}
void insert(ulong position, string value) {
text.insert(position, value);
update();
}
string substring(ulong start, ulong end = -1) {
if (end == -1) {
update();
return text.substring(start, text.length);
} else {
update();
return text.substring(start, end);
}
}
char charAt(ulong pos) {
update();
return text.charAt(pos);
}
}

View File

@@ -12,12 +12,24 @@ void main(string[] args) {
if (args.length > 1) {
filename = args[1];
fileTxt = readText(filename);
if (!exists(filename)) {
File file = File(filename, "w");
file.close();
} else {
if (isDir(filename)) {
writeln(filename, " is a directory");
return;
}
fileTxt = readText(filename);
if (fileTxt is null) {
fileTxt = "";
}
}
}
Buffer buf = new Buffer(fileTxt, filename);
init();
Buffer buf = new Buffer(fileTxt, filename);
Cursor cursor = new Cursor();
View v = new View(buf, cursor);

View File

@@ -51,6 +51,9 @@ class Rope {
void remove(ulong start, ulong end) {
if (value !is null) {
value = to!string(value.to!dstring[0 .. start] ~ value.to!dstring[end .. $]);
if (value is null) {
value = "";
}
length = value.count;
} else {
auto leftStart = min(start, left.length);

View File

@@ -152,7 +152,6 @@ class View {
if (cursor.y > topline + height-1) {
topline = cursor.y - height-1;
}
buf.update();
}
}