diff --git a/src/buffer.d b/src/buffer.d index 5f8719a6..b1acae52 100644 --- a/src/buffer.d +++ b/src/buffer.d @@ -14,6 +14,7 @@ class Buffer { this(string txt, string path) { text = new Rope(txt); + savedText = txt; this.path = path; update(); } diff --git a/src/cursor.d b/src/cursor.d index d1c699e0..7ee7172e 100644 --- a/src/cursor.d +++ b/src/cursor.d @@ -14,8 +14,4 @@ class Cursor { void hide() { x = y = -1; } - - void display() { - setCursor(x, y); - } } diff --git a/src/main.d b/src/main.d index 3eaf3bc5..94105dbf 100644 --- a/src/main.d +++ b/src/main.d @@ -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); diff --git a/src/statusline.d b/src/statusline.d new file mode 100644 index 00000000..f3bb0670 --- /dev/null +++ b/src/statusline.d @@ -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); + } + } + } +} diff --git a/src/view.d b/src/view.d index a5eb506f..e711f871 100644 --- a/src/view.d +++ b/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(); } }