| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- //
- // main.cxx
- // curses_dice
- //
- // Created by Sam Jaffe on 7/23/24.
- // Copyright © 2024 Sam Jaffe. All rights reserved.
- //
- #include <sstream>
- #include <ncurses-wrapper/cli.h>
- #include <ncurses-wrapper/color.h>
- #include <ncurses-wrapper/window.h>
- #include "dice-roll/die.h"
- #include "dice-roll/exception.h"
- #include "dice-roll/io.h"
- #include "dice-roll/roll.h"
- using namespace curses;
- ColorPair color(bool good, bool bad) {
- if (good) {
- return ColorPair{Color::GREEN, Color::DEFAULT};
- } else if (bad) {
- return ColorPair{Color::RED, Color::DEFAULT};
- } else {
- return ColorPair{Color::DEFAULT, Color::DEFAULT};
- }
- }
- ColorPair color(bool good) { return color(good, !good); }
- void print(curses::Window & window, int roll, int sides) {
- if (auto scope = window.with(color(roll == sides, roll == 1))) {
- window.printf("%d", roll);
- }
- }
- namespace dice {
- curses::Window & operator<<(curses::Window & window, outcome o) {
- if (auto scope = window.with(color(o == outcome::PASS))) {
- window.printf("%s", o == outcome::PASS ? "PASS" : "FAIL");
- }
- return window;
- }
- curses::Window & operator<<(curses::Window & window, die_outcome const & r) {
- if (auto scope = window.with(color(r.roll == r.sides, r.roll == 1))) {
- auto roll = std::to_string(r.roll);
- std::string strikethrough = "\u0336";
- for (size_t i = 1; i <= roll.size() && r.dropped;
- i += 1 + strikethrough.length()) {
- roll.insert(i, strikethrough);
- }
- window.printf("%s", roll.c_str());
- }
- return window;
- }
- }
- void eval(curses::Window & window, std::string line) try {
- using ::dice::operator<<;
- dice::dice const d = dice::from_string(line);
- std::vector<dice::dice_roll> const rs = dice::roller()(d);
- window << std::make_pair(d, rs) << '\n';
- } catch (dice::unexpected_token const & ut) {
- window.printf("Error in roll: '%s': %s\n %s\n", line.c_str(),
- ut.what(), ut.pointer(line.size() + 1).c_str());
- }
- int main(int, const char **) {
- Cli("> ", WithColor).loop(&eval);
- return 0;
- }
|