micro/src/buffer.d

74 lines
1.4 KiB
D
Raw Normal View History

2016-03-12 20:43:24 +00:00
import rope;
import std.string, std.stdio;
2016-03-11 02:06:06 +00:00
class Buffer {
2016-03-12 20:43:24 +00:00
private Rope text;
2016-03-11 02:06:06 +00:00
2016-03-12 20:43:24 +00:00
string path;
2016-03-11 02:06:06 +00:00
string savedText;
2016-03-12 20:43:24 +00:00
private string value;
2016-03-11 02:06:06 +00:00
2016-03-12 20:43:24 +00:00
string[] lines;
2016-03-11 02:06:06 +00:00
2016-03-12 20:43:24 +00:00
this(string txt, string path) {
text = new Rope(txt);
savedText = txt;
2016-03-12 20:43:24 +00:00
this.path = path;
update();
2016-03-11 02:06:06 +00:00
}
2016-03-12 20:43:24 +00:00
void save() {
saveAs(path);
2016-03-11 02:06:06 +00:00
}
2016-03-12 20:43:24 +00:00
void saveAs(string filename) {
string bufTxt = text.toString();
File f = File(filename, "w");
f.write(bufTxt);
f.close();
savedText = bufTxt;
2016-03-11 02:06:06 +00:00
}
override
string toString() {
2016-03-12 20:43:24 +00:00
return value;
2016-03-11 02:06:06 +00:00
}
2016-03-12 20:43:24 +00:00
void update() {
value = text.toString();
if (value == "") {
lines = [""];
2016-03-11 02:06:06 +00:00
} else {
2016-03-12 20:43:24 +00:00
lines = value.split("\n");
2016-03-11 02:06:06 +00:00
}
}
2016-03-12 20:43:24 +00:00
@property ulong length() {
return text.length;
2016-03-11 02:06:06 +00:00
}
2016-03-12 20:43:24 +00:00
void remove(ulong start, ulong end) {
text.remove(start, end);
update();
2016-03-11 02:06:06 +00:00
}
2016-03-12 20:43:24 +00:00
void insert(ulong position, string value) {
text.insert(position, value);
update();
2016-03-11 02:06:06 +00:00
}
2016-03-12 20:43:24 +00:00
string substring(ulong start, ulong end = -1) {
if (end == -1) {
update();
2016-03-12 20:43:24 +00:00
return text.substring(start, text.length);
2016-03-11 02:06:06 +00:00
} else {
update();
2016-03-12 20:43:24 +00:00
return text.substring(start, end);
2016-03-11 02:06:06 +00:00
}
}
char charAt(ulong pos) {
update();
2016-03-12 20:43:24 +00:00
return text.charAt(pos);
2016-03-11 02:06:06 +00:00
}
}