From 00597c0aaa8605a271f8256a81af635e30c8892e Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 6 Aug 2024 01:02:08 -0400 Subject: [PATCH] A bit of restructuring and some color output for fun. --- watchgit.cpp | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/watchgit.cpp b/watchgit.cpp index 5141869..e7e4268 100644 --- a/watchgit.cpp +++ b/watchgit.cpp @@ -1,38 +1,49 @@ #include -#include #include #include +#include #include -#include #include -#include -#include #include "dbc.h" #include #include #include #include #include -#include #include +#include using namespace std; using namespace fmt; namespace fs = std::filesystem; +const auto ERROR = fmt::emphasis::bold | fg(fmt::color::red); + #define BUF_MAX 1024 class GameEngine { int hit_points = 0; + map damage_types{ + {"error", 4}, {"warning", 1}}; public: GameEngine(int hp) : hit_points(hp) {}; - bool hit(unsigned int damage) { + int determine_damage(string &type) { + try { + return damage_types.at(type); + } catch(std::out_of_range &err) { + print(ERROR, "BAD DAMAGE TYPE {}\n", type); + return 1000; + } + } + + bool hit(string &type) { + int damage = determine_damage(type); hit_points -= damage; if(is_dead()) { - println("YOU DIED!"); + print(ERROR, "YOU DIED!\n"); } else { println("DAMAGE {}, HP: {}", damage, hit_points); } @@ -121,12 +132,7 @@ void run_build(GameEngine &game, const char* command) { stats_out << result; println("{} @ {}:{}:{} {}", type, file_name, lnumber, col, message); - - if(type == "error") { - game.hit(4); - } else { - game.hit(1); - } + game.hit(type); } }