From ec9ce614d87a47137cfbbb0f6d13aa30e4a1239c Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Sat, 14 Aug 2021 17:37:56 -0400 Subject: completed save/load feature --- source/app.d | 109 +++++++++++++---------------------------------------------- 1 file changed, 23 insertions(+), 86 deletions(-) (limited to 'source/app.d') diff --git a/source/app.d b/source/app.d index 52074b1..1d04fc9 100644 --- a/source/app.d +++ b/source/app.d @@ -78,12 +78,11 @@ void main() file.writefln!"%s %s"(disp.y, disp.x); file.writefln!"%d"(rocks); overlay.save(file); + disp.print(); } catch (Exception e) { - // TODO: log the error in a user-visible way } - disp.print(); break; case 'l': auto filename = stdscr.readquery("Load:"); @@ -114,81 +113,6 @@ void main() } } -import std.range.interfaces : InputRange; - -class Reader : InputRange!char -{ - - private int _front; - private bool _empty; - // Might could be implemented with a larger getnstr - - this() - { - _front = getch(); - } - - char moveFront() - in - { - assert(!_empty); - } - do - { - auto ret = front(); - popFront(); - return ret; - } - - @property char front() - in - { - assert(!_empty); - } - do - { - return cast(char) _front; - } - - void popFront() - in - { - assert(!_empty); - } - do - { - _front = getch(); - if (_front == -1) - _empty = true; - } - - @property bool empty() - { - return _empty; - } - - int opApply(scope int delegate(char) run) - { - for (; !_empty; popFront()) - { - if (run(front())) - return 1; - } - - return 0; - } - - int opApply(scope int delegate(size_t, char) run) - { - for (size_t i = 0; !_empty; popFront(), i++) - { - if (run(i, front())) - return 1; - } - return 0; - } -} - string readquery(WINDOW* stdscr, string query) { import std.range : replicate; @@ -202,19 +126,32 @@ string readquery(WINDOW* stdscr, string query) mvprintw(height, 0, toStringz(query ~ " ".replicate(width - query.length))); move(height, cast(int) query.length + 1); - echo(); scrollok(stdscr, false); - auto build = appender!string; + auto build = appender!(char[]); - auto reader = new Reader(); - - while (!reader.empty && reader.front != '\r') + int p = cast(int) query.length + 1; + while (auto ch = getch()) { - build ~= reader.front; - reader.popFront; + if (ch == '\r' || ch == EOF) + break; + if (ch == '\b' || ch == KEY_BACKSPACE || ch == 127) + { + if (build[].length > 0) + { + p--; + mvaddch(height, p, ' '); + build.shrinkTo(build[].length - 1); + move(height, p); + } + } + else + { + build ~= cast(char) ch; + addch(ch); + p++; + } } - noecho(); scrollok(stdscr, true); - return build.data; + return cast(string) build[]; } -- cgit