diff options
| author | Holden Rohrer <hr@hrhr.dev> | 2021-08-08 12:55:47 -0400 | 
|---|---|---|
| committer | Holden Rohrer <hr@hrhr.dev> | 2021-08-08 12:55:47 -0400 | 
| commit | c353004717da1ef7ba8be6e9a3ce10002c7d6d66 (patch) | |
| tree | 79155f8e6f545639d423b8715ac5d459fb47962e /source | |
| parent | 5f974d83677fc6d7ee229e6a2e2720a45677e4e0 (diff) | |
set up save and load w/o writes and reads
Diffstat (limited to 'source')
| -rw-r--r-- | source/app.d | 133 | 
1 files changed, 129 insertions, 4 deletions
| diff --git a/source/app.d b/source/app.d index 4006188..943b9d0 100644 --- a/source/app.d +++ b/source/app.d @@ -4,6 +4,7 @@ import core.stdc.locale;  import std.format;  import board;  import spots; +import std.stdio;  void main()  { @@ -19,10 +20,6 @@ void main()      scrollok(stdscr, true); // allows scrolling in up/down arrows      nonl(); // "return" goes through input -    int x; -    int y; -    getmaxyx(stdscr, y, x); -      auto overlay = new OverlaySource(new RandomSource());      auto board = new Board(overlay);      auto disp = new BoardDisplay(board, stdscr); @@ -73,8 +70,136 @@ void main()              break;          case 'q':              break outer; +        case 's': +            auto filename = stdscr.readquery("Save:"); +            try +            { +                auto file = File(filename, "wb"); + +            } +            catch (ErrnoException e) +            { +            } +            disp.print(); +            break; +        case 'l': +            auto filename = stdscr.readquery("Load:"); +            try +            { +                auto file = File(filename); +            } +            catch (ErrnoException e) +            { +            } +            disp.print(); +            break;          default:              continue;          }      }  } + +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; +    import std.conv : to; +    import std.array : appender; + +    int height; +    int width; +    getmaxyx(stdscr, height, width); + +    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 reader = new Reader(); + +    while (!reader.empty && reader.front != '\r') +    { +        build ~= reader.front; +        reader.popFront; +    } +    noecho(); +    scrollok(stdscr, true); + +    return build.data; +} | 
