Add statusline and accept pipes from stdin
This commit is contained in:
parent
934634507d
commit
757986ae3f
5 changed files with 57 additions and 7 deletions
|
@ -14,6 +14,7 @@ class Buffer {
|
|||
|
||||
this(string txt, string path) {
|
||||
text = new Rope(txt);
|
||||
savedText = txt;
|
||||
this.path = path;
|
||||
update();
|
||||
}
|
||||
|
|
|
@ -14,8 +14,4 @@ class Cursor {
|
|||
void hide() {
|
||||
x = y = -1;
|
||||
}
|
||||
|
||||
void display() {
|
||||
setCursor(x, y);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,12 @@ void main(string[] args) {
|
|||
fileTxt = "";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (stdin.size != 0) {
|
||||
foreach (line; stdin.byLine()) {
|
||||
fileTxt ~= line ~ "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Buffer buf = new Buffer(fileTxt, filename);
|
||||
|
@ -41,7 +47,6 @@ void main(string[] args) {
|
|||
clear();
|
||||
|
||||
v.display();
|
||||
cursor.display();
|
||||
|
||||
flush();
|
||||
pollEvent(&e);
|
||||
|
|
29
src/statusline.d
Normal file
29
src/statusline.d
Normal file
|
@ -0,0 +1,29 @@
|
|||
import termbox;
|
||||
import view;
|
||||
|
||||
class StatusLine {
|
||||
View view;
|
||||
|
||||
this(View v) {
|
||||
this.view = v;
|
||||
}
|
||||
|
||||
void display() {
|
||||
int y = view.height;
|
||||
string file = view.buf.path;
|
||||
if (file == "") {
|
||||
file = "untitled";
|
||||
}
|
||||
if (view.buf.toString != view.buf.savedText) {
|
||||
file ~= " +";
|
||||
}
|
||||
file ~= " (" ~ to!string(view.cursor.y + 1) ~ "," ~ to!string(view.cursor.x + 1) ~ ")";
|
||||
foreach (x; 0 .. view.width) {
|
||||
if (x >= 1 && x < 1 + file.length) {
|
||||
setCell(x, y, cast(uint) file[x - 1], Color.black, Color.blue);
|
||||
} else {
|
||||
setCell(x, y, ' ', Color.black, Color.blue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
23
src/view.d
23
src/view.d
|
@ -2,6 +2,7 @@ import termbox;
|
|||
import buffer;
|
||||
import clipboard;
|
||||
import cursor;
|
||||
import statusline;
|
||||
|
||||
import std.conv: to;
|
||||
import std.utf: count;
|
||||
|
@ -14,6 +15,7 @@ class View {
|
|||
|
||||
Buffer buf;
|
||||
Cursor cursor;
|
||||
StatusLine sl;
|
||||
|
||||
this(Buffer buf, Cursor cursor, uint topline = 0, uint width = termbox.width(), uint height = termbox.height() - 2) {
|
||||
this.topline = topline;
|
||||
|
@ -22,6 +24,7 @@ class View {
|
|||
|
||||
this.buf = buf;
|
||||
this.cursor = cursor;
|
||||
this.sl = new StatusLine(this);
|
||||
}
|
||||
|
||||
uint toCharNumber(int x, int y) {
|
||||
|
@ -111,7 +114,13 @@ class View {
|
|||
cursorLeft();
|
||||
} else if (e.key == Key.mouseLeft) {
|
||||
cursor.x = e.x;
|
||||
cursor.y = e.y;
|
||||
cursor.y = e.y + topline;
|
||||
if (cursor.y - topline > height-1) {
|
||||
cursor.y = height + topline-1;
|
||||
}
|
||||
if (cursor.y > buf.lines.length) {
|
||||
cursor.y = cast(int) buf.lines.length-1;
|
||||
}
|
||||
cursor.lastX = cursor.x;
|
||||
if (cursor.x > buf.lines[cursor.y].length) {
|
||||
cursor.x = cast(int) buf.lines[cursor.y].length;
|
||||
|
@ -155,7 +164,7 @@ class View {
|
|||
}
|
||||
}
|
||||
|
||||
void display() {
|
||||
void display(bool drawCursor = true) {
|
||||
uint x, y;
|
||||
|
||||
string[] lines;
|
||||
|
@ -172,5 +181,15 @@ class View {
|
|||
y++;
|
||||
x = 0;
|
||||
}
|
||||
|
||||
if (drawCursor) {
|
||||
if (cursor.y - topline < 0 || cursor.y - topline > height-1) {
|
||||
hideCursor();
|
||||
} else {
|
||||
setCursor(cursor.x, cursor.y - topline);
|
||||
}
|
||||
}
|
||||
|
||||
sl.display();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue