diff options
author | Holden Rohrer <hr@hrhr.dev> | 2021-08-14 17:37:56 -0400 |
---|---|---|
committer | Holden Rohrer <hr@hrhr.dev> | 2021-08-14 17:37:56 -0400 |
commit | ec9ce614d87a47137cfbbb0f6d13aa30e4a1239c (patch) | |
tree | da72922bf13fce52a1954121eaf27164cb51d6aa /source/app.d | |
parent | a770dd88e6c55ac7fd67c243ad30f04425e3e175 (diff) |
completed save/load feature
Diffstat (limited to 'source/app.d')
-rw-r--r-- | source/app.d | 109 |
1 files changed, 23 insertions, 86 deletions
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[]; } |