mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-29 22:27:13 +09:00
Begin rewrite
This commit is contained in:
131
src/view.d
131
src/view.d
@@ -1,30 +1,61 @@
|
||||
import termbox;
|
||||
import cursor;
|
||||
import buffer;
|
||||
import clipboard;
|
||||
import cursor;
|
||||
|
||||
import std.array: join;
|
||||
import std.regex;
|
||||
import std.conv: to;
|
||||
import std.stdio;
|
||||
import std.utf: count;
|
||||
|
||||
class View {
|
||||
int topline;
|
||||
int xOffset;
|
||||
uint topline;
|
||||
|
||||
int width;
|
||||
int height;
|
||||
uint width;
|
||||
uint height;
|
||||
|
||||
Buffer buf;
|
||||
Cursor cursor;
|
||||
|
||||
this(Buffer buf, int topline = 0, int width = termbox.width(), int height = termbox.height()-2) {
|
||||
this(Buffer buf, Cursor cursor, uint topline = 0, uint width = termbox.width(), uint height = termbox.height() - 2) {
|
||||
this.topline = topline;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
this.buf = buf;
|
||||
this.cursor = new Cursor(buf);
|
||||
this.cursor = cursor;
|
||||
}
|
||||
|
||||
uint toCharNumber(int x, int y) {
|
||||
int loc;
|
||||
foreach (i; 0 .. y) {
|
||||
loc += buf.lines[i].count + 1;
|
||||
}
|
||||
loc += x;
|
||||
return loc;
|
||||
}
|
||||
|
||||
int[] fromCharNumber(uint value) {
|
||||
int x, y;
|
||||
int loc;
|
||||
foreach (lineNum, l; buf.lines) {
|
||||
if (loc + l.count+1 > value) {
|
||||
y = cast(int) lineNum;
|
||||
x = value - loc;
|
||||
return [x, y];
|
||||
} else {
|
||||
loc += l.count+1;
|
||||
}
|
||||
}
|
||||
return [-1, -1];
|
||||
}
|
||||
|
||||
uint cursorLoc() {
|
||||
return toCharNumber(cursor.x, cursor.y);
|
||||
}
|
||||
|
||||
void setCursorLoc(uint charNum) {
|
||||
int[] xy = fromCharNumber(charNum);
|
||||
cursor.x = xy[0];
|
||||
cursor.y = xy[1];
|
||||
}
|
||||
|
||||
void cursorUp() {
|
||||
@@ -62,14 +93,13 @@ class View {
|
||||
}
|
||||
|
||||
void update(Event e) {
|
||||
uint cloc = cursorLoc();
|
||||
if (e.key == Key.mouseWheelUp) {
|
||||
if (topline > 0) {
|
||||
if (topline > 0)
|
||||
topline--;
|
||||
}
|
||||
} else if (e.key == Key.mouseWheelDown) {
|
||||
if (topline < buf.lines.length - height) {
|
||||
if (topline < buf.lines.length - height)
|
||||
topline++;
|
||||
}
|
||||
} else {
|
||||
if (e.key == Key.arrowUp) {
|
||||
cursorUp();
|
||||
@@ -80,53 +110,54 @@ class View {
|
||||
} else if (e.key == Key.arrowLeft) {
|
||||
cursorLeft();
|
||||
} else if (e.key == Key.mouseLeft) {
|
||||
cursor.x = e.x - xOffset;
|
||||
if (cursor.x < 0) {
|
||||
cursor.x = 0;
|
||||
}
|
||||
cursor.y = e.y + topline;
|
||||
cursor.x = e.x;
|
||||
cursor.y = e.y;
|
||||
cursor.lastX = cursor.x;
|
||||
if (cursor.x > buf.lines[cursor.y].length) {
|
||||
cursor.x = cast(int) buf.lines[cursor.y].length;
|
||||
}
|
||||
} else if (e.key == Key.ctrl_s) {
|
||||
buf.save();
|
||||
} else if (e.key == Key.ctrl_v) {
|
||||
} else if (e.key == Key.ctrlS) {
|
||||
if (buf.path != "") {
|
||||
buf.save();
|
||||
}
|
||||
} else if (e.key == Key.ctrlV) {
|
||||
if (Clipboard.supported) {
|
||||
buf.insert(cursor.loc, Clipboard.read());
|
||||
buf.insert(cloc, Clipboard.read());
|
||||
}
|
||||
} else {
|
||||
if (e.ch != 0) {
|
||||
buf.insert(cursor.loc, to!string(to!dchar(e.ch)));
|
||||
buf.insert(cloc, to!string(to!dchar(e.ch)));
|
||||
cursorRight();
|
||||
} else if (e.key == Key.space) {
|
||||
buf.insert(cursor.loc, " ");
|
||||
buf.insert(cursorLoc(), " ");
|
||||
cursorRight();
|
||||
} else if (e.key == Key.enter) {
|
||||
buf.insert(cursor.loc, "\n");
|
||||
cursor.loc = cursor.loc + 1;
|
||||
buf.insert(cloc, "\n");
|
||||
cursorDown();
|
||||
cursor.x = 0;
|
||||
cursor.lastX = cursor.x;
|
||||
} else if (e.key == Key.backspace2) {
|
||||
if (cursor.loc != 0) {
|
||||
cursor.loc = cursor.loc - 1;
|
||||
buf.remove(cursor.loc, cursor.loc + 1);
|
||||
if (cloc > 0) {
|
||||
buf.remove(cloc-1, cloc);
|
||||
setCursorLoc(cloc - 1);
|
||||
cursor.lastX = cursor.x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (cursor.y < topline) {
|
||||
topline--;
|
||||
topline = cursor.y;
|
||||
}
|
||||
|
||||
if (cursor.y > topline + height-1) {
|
||||
topline++;
|
||||
topline = cursor.y - height-1;
|
||||
}
|
||||
buf.update();
|
||||
}
|
||||
}
|
||||
|
||||
void display() {
|
||||
int x, y;
|
||||
uint x, y;
|
||||
|
||||
string[] lines;
|
||||
if (topline + height > buf.lines.length) {
|
||||
@@ -134,45 +165,13 @@ class View {
|
||||
} else {
|
||||
lines = buf.lines[topline .. topline + height];
|
||||
}
|
||||
ulong maxLength = to!string(buf.lines.length).length;
|
||||
xOffset = cast(int) maxLength + 1;
|
||||
|
||||
int charNum;
|
||||
string bufSrc = lines.join("\n");
|
||||
auto r = regex("\".*?\"");
|
||||
auto matches = bufSrc.matchAll(r);
|
||||
Color[ulong] colors;
|
||||
foreach (m; matches) {
|
||||
colors[m.pre.length] = Color.blue;
|
||||
colors[m.pre.length + m.hit.length] = Color.default_;
|
||||
}
|
||||
foreach (i, line; lines) {
|
||||
string lineNum = to!string(i + topline + 1);
|
||||
foreach (_; 0 .. maxLength - lineNum.length) {
|
||||
setCell(cast(int) x++, cast(int) y, ' ', Color.default_, Color.black);
|
||||
}
|
||||
foreach (dchar ch; lineNum) {
|
||||
setCell(cast(int) x++, cast(int) y, ch, Color.default_, Color.black);
|
||||
}
|
||||
setCell(cast(int) x++, cast(int) y, ' ', Color.default_ | Attribute.bold, Color.black);
|
||||
|
||||
Color c;
|
||||
foreach (dchar ch; line) {
|
||||
if (charNum in colors) {
|
||||
c = colors[charNum];
|
||||
}
|
||||
setCell(cast(int) x++, cast(int) y, ch, c, Color.default_);
|
||||
charNum += to!string(ch).length;
|
||||
setCell(x++, y, ch, Color.basic, Color.basic);
|
||||
}
|
||||
charNum++;
|
||||
y++;
|
||||
x = 0;
|
||||
}
|
||||
|
||||
if (cursor.y - topline < 0 || cursor.y - topline > height-1) {
|
||||
hideCursor();
|
||||
} else {
|
||||
setCursor(cursor.x + xOffset, cursor.y - topline);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user