terminal_helper.cxx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //
  2. // terminal_helper.cxx
  3. // simple_dice
  4. //
  5. // Created by Sam Jaffe on 12/18/18.
  6. // Copyright © 2018 Sam Jaffe. All rights reserved.
  7. //
  8. #include "dice-roll/terminal_helper.h"
  9. #include <iostream>
  10. #include "dice-roll/die.h"
  11. #include "dice-roll/exception.h"
  12. #include "dice-roll/roll.h"
  13. namespace terminal { namespace {
  14. void print(dice::dice_roll const & r) {
  15. std::cout << int(r) << " (" << r << ")\n";
  16. }
  17. void print(std::vector<dice::dice_roll> const & rs) {
  18. if (rs.size() != 1) {
  19. std::cout << '\n';
  20. for (int i = 0; i < rs.size(); ++i) {
  21. std::cout << " Result/" << i << ": ";
  22. print(rs[i]);
  23. }
  24. } else {
  25. print(rs[0]);
  26. }
  27. }
  28. }}
  29. namespace terminal {
  30. void process_dice_string(std::string const & str) {
  31. auto d = dice::from_string(str);
  32. auto rs = dice::roller()(d);
  33. std::cout << "Result of '" << d << "': ";
  34. print(rs);
  35. }
  36. void print_error_message(std::string const & str,
  37. dice::unexpected_token const & ut) {
  38. std::cerr << "Error in roll: '" << str << "': " << ut.what() << "\n";
  39. std::cerr << " " << ut.pointer(str.size() + 1) << std::endl;
  40. }
  41. }