25 create class for printing and debug printing #26
Merged@github-juk3n wants to merge
25-create-class-for-printing-and-debug-printing into mainMerged as
ddced2d1a13b.3 files changed
modified
app/include/database.hpp +8 -7 | @@ -4,10 +4,12 @@ | ||
| 4 | 4 | #include <string> |
| 5 | 5 | |
| 6 | 6 | #include <game.hpp> |
| 7 | +#include <output.hpp> | |
| 7 | 8 | |
| 8 | 9 | class Database |
| 9 | 10 | { |
| 10 | 11 | private: |
| 12 | + Output output{}; | |
| 11 | 13 | sqlite3 *database; |
| 12 | 14 | |
| 13 | 15 | std::string database_initialization_command = |
| @@ -19,7 +21,7 @@ private: | ||
| 19 | 21 | |
| 20 | 22 | auto initializeDatabase() -> void { |
| 21 | 23 | executeSqlCommand(database_initialization_command); |
| 22 | - //printMessage("Database Initialized"); | |
| 24 | + output.printDebugMessage("Database Initialized"); | |
| 23 | 25 | } |
| 24 | 26 | |
| 25 | 27 | public: |
| @@ -38,7 +40,8 @@ public: | ||
| 38 | 40 | exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError); |
| 39 | 41 | |
| 40 | 42 | if (exit != SQLITE_OK) { |
| 41 | - //printMessage("Error with command: " + command + " " + messageError); | |
| 43 | + | |
| 44 | + output.printDebugMessage("Error with command: " + command + " " + messageError); | |
| 42 | 45 | sqlite3_free(messageError); |
| 43 | 46 | throw std::exception(); |
| 44 | 47 | } |
| @@ -53,12 +56,11 @@ public: | ||
| 53 | 56 | exit = sqlite3_open(databasePath.c_str(), &database); |
| 54 | 57 | |
| 55 | 58 | if (exit) { |
| 56 | - std::cerr << "Error opening database" << sqlite3_errmsg(database) | |
| 57 | - << std::endl; | |
| 59 | + output.printDebugError("Error opening database" + std::string{sqlite3_errmsg(database)}); | |
| 58 | 60 | throw std::exception(); |
| 59 | 61 | } |
| 60 | 62 | |
| 61 | - //printMessage("Opened database successfully!"); | |
| 63 | + output.printDebugMessage("Opened database successfully!"); | |
| 62 | 64 | |
| 63 | 65 | if (newCreation) { |
| 64 | 66 | initializeDatabase(); |
| @@ -71,8 +73,7 @@ public: | ||
| 71 | 73 | exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr); |
| 72 | 74 | |
| 73 | 75 | if (exit) { |
| 74 | - std::cerr << "Error retrieving data" << sqlite3_errmsg(database) | |
| 75 | - << std::endl; | |
| 76 | + output.printDebugError("Error retrieving data" + std::string{sqlite3_errmsg(database)}); | |
| 76 | 77 | sqlite3_close(database); |
| 77 | 78 | return {}; |
| 78 | 79 | } |
| @@ -4,10 +4,12 @@ | |||
| 4 | #include <string> | 4 | #include <string> |
| 5 | 5 | ||
| 6 | #include <game.hpp> | 6 | #include <game.hpp> |
| 7 | +#include <output.hpp> | ||
| 7 | 8 | ||
| 8 | class Database | 9 | class Database |
| 9 | { | 10 | { |
| 10 | private: | 11 | private: |
| 12 | + Output output{}; | ||
| 11 | sqlite3 *database; | 13 | sqlite3 *database; |
| 12 | 14 | ||
| 13 | std::string database_initialization_command = | 15 | std::string database_initialization_command = |
| @@ -19,7 +21,7 @@ private: | |||
| 19 | 21 | ||
| 20 | auto initializeDatabase() -> void { | 22 | auto initializeDatabase() -> void { |
| 21 | executeSqlCommand(database_initialization_command); | 23 | executeSqlCommand(database_initialization_command); |
| 22 | - //printMessage("Database Initialized"); | 24 | + output.printDebugMessage("Database Initialized"); |
| 23 | } | 25 | } |
| 24 | 26 | ||
| 25 | public: | 27 | public: |
| @@ -38,7 +40,8 @@ public: | |||
| 38 | exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError); | 40 | exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError); |
| 39 | 41 | ||
| 40 | if (exit != SQLITE_OK) { | 42 | if (exit != SQLITE_OK) { |
| 41 | - //printMessage("Error with command: " + command + " " + messageError); | 43 | + |
| 44 | + output.printDebugMessage("Error with command: " + command + " " + messageError); | ||
| 42 | sqlite3_free(messageError); | 45 | sqlite3_free(messageError); |
| 43 | throw std::exception(); | 46 | throw std::exception(); |
| 44 | } | 47 | } |
| @@ -53,12 +56,11 @@ public: | |||
| 53 | exit = sqlite3_open(databasePath.c_str(), &database); | 56 | exit = sqlite3_open(databasePath.c_str(), &database); |
| 54 | 57 | ||
| 55 | if (exit) { | 58 | if (exit) { |
| 56 | - std::cerr << "Error opening database" << sqlite3_errmsg(database) | 59 | + output.printDebugError("Error opening database" + std::string{sqlite3_errmsg(database)}); |
| 57 | - << std::endl; | ||
| 58 | throw std::exception(); | 60 | throw std::exception(); |
| 59 | } | 61 | } |
| 60 | 62 | ||
| 61 | - //printMessage("Opened database successfully!"); | 63 | + output.printDebugMessage("Opened database successfully!"); |
| 62 | 64 | ||
| 63 | if (newCreation) { | 65 | if (newCreation) { |
| 64 | initializeDatabase(); | 66 | initializeDatabase(); |
| @@ -71,8 +73,7 @@ public: | |||
| 71 | exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr); | 73 | exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr); |
| 72 | 74 | ||
| 73 | if (exit) { | 75 | if (exit) { |
| 74 | - std::cerr << "Error retrieving data" << sqlite3_errmsg(database) | 76 | + output.printDebugError("Error retrieving data" + std::string{sqlite3_errmsg(database)}); |
| 75 | - << std::endl; | ||
| 76 | sqlite3_close(database); | 77 | sqlite3_close(database); |
| 77 | return {}; | 78 | return {}; |
| 78 | } | 79 | } |
added
app/include/output.hpp +21 -0 | new file mode 100644 | ||
| @@ -0,0 +1,21 @@ | ||
| 1 | +# pragma once | |
| 2 | + | |
| 3 | +#include <iostream> | |
| 4 | +#include <string> | |
| 5 | + | |
| 6 | +class Output { | |
| 7 | +private: | |
| 8 | + bool debugRun = false; | |
| 9 | +public: | |
| 10 | + auto printDebugMessage(std::string message) -> void { | |
| 11 | + if (debugRun) | |
| 12 | + std::cout << message << std::endl; | |
| 13 | + } | |
| 14 | + auto printDebugError(std::string message) -> void { | |
| 15 | + if (debugRun) | |
| 16 | + std::cerr << message << std::endl; | |
| 17 | + } | |
| 18 | + auto printMessage(std::string message) -> void { | |
| 19 | + std::cout << message << std::endl; | |
| 20 | + } | |
| 21 | +}; | |
| new file mode 100644 | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | +# pragma once | ||
| 2 | + | ||
| 3 | +#include <iostream> | ||
| 4 | +#include <string> | ||
| 5 | + | ||
| 6 | +class Output { | ||
| 7 | +private: | ||
| 8 | + bool debugRun = false; | ||
| 9 | +public: | ||
| 10 | + auto printDebugMessage(std::string message) -> void { | ||
| 11 | + if (debugRun) | ||
| 12 | + std::cout << message << std::endl; | ||
| 13 | + } | ||
| 14 | + auto printDebugError(std::string message) -> void { | ||
| 15 | + if (debugRun) | ||
| 16 | + std::cerr << message << std::endl; | ||
| 17 | + } | ||
| 18 | + auto printMessage(std::string message) -> void { | ||
| 19 | + std::cout << message << std::endl; | ||
| 20 | + } | ||
| 21 | +}; | ||
modified
app/src/main.cpp +13 -17 | @@ -18,18 +18,14 @@ | ||
| 18 | 18 | |
| 19 | 19 | #include <game.hpp> |
| 20 | 20 | #include <database.hpp> |
| 21 | +#include <output.hpp> | |
| 21 | 22 | |
| 22 | 23 | using namespace ftxui; |
| 23 | 24 | |
| 25 | +Output output{}; | |
| 26 | +std::string version{ "v0.1.2" }; | |
| 24 | 27 | std::vector<Game> games; |
| 25 | 28 | |
| 26 | -bool debugRun = false; | |
| 27 | - | |
| 28 | -auto printMessage(std::string message) -> void { | |
| 29 | - if (debugRun) { | |
| 30 | - std::cout << message << std::endl; | |
| 31 | - } | |
| 32 | -} | |
| 33 | 29 | |
| 34 | 30 | auto addGame(Database& database, std::string name, std::string path) -> void { |
| 35 | 31 | std::string command = |
| @@ -85,17 +81,17 @@ auto handleCommand(const std::string& command, const std::vector<std::string>& a | ||
| 85 | 81 | runGame(gameName); |
| 86 | 82 | } |
| 87 | 83 | else if (command == "--version") { |
| 88 | - std::cout << "fluffy v0.1.1" << std::endl; | |
| 84 | + output.printMessage("fluffy " + version); | |
| 89 | 85 | } |
| 90 | 86 | else if (command == "--help") { |
| 91 | - std::cout << "fluffy commands:" << std::endl; | |
| 92 | - std::cout << " play <game_name> Runs a game" << std::endl; | |
| 93 | - std::cout << " add <game_name> <game_path> Add a game library" << std::endl; | |
| 94 | - std::cout << " show Show all games added to library" << std::endl; | |
| 95 | - std::cout << " rm <game_name> Remove game from a library" << std::endl; | |
| 87 | + output.printMessage("fluffy commands:\n \ | |
| 88 | + play <game_name> Runs a game\n \ | |
| 89 | + add <game_name> <game_path> Add a game library\n \ | |
| 90 | + show Show all games added to library\n \ | |
| 91 | + rm <game_name> Remove game from a library"); | |
| 96 | 92 | } |
| 97 | 93 | else { |
| 98 | - std::cout << "fluffy: '" << command << "' is not a fluffy command. See 'fluffy --help'" << std::endl; | |
| 94 | + output.printMessage("fluffy: '" + command + "' is not a fluffy command. See 'fluffy --help'"); | |
| 99 | 95 | } |
| 100 | 96 | } |
| 101 | 97 | |
| @@ -140,7 +136,7 @@ auto runConsoleApp() -> void { | ||
| 140 | 136 | }); |
| 141 | 137 | auto renderer = Renderer(container, [&] { |
| 142 | 138 | return vbox({ |
| 143 | - center(text(L" Fluffy v0.1.1 ")), | |
| 139 | + center(text("Fluffy " + version)), | |
| 144 | 140 | appContainer->Render(), |
| 145 | 141 | }) | |
| 146 | 142 | border | size(WIDTH, LESS_THAN, 120); |
| @@ -152,10 +148,10 @@ auto runConsoleApp() -> void { | ||
| 152 | 148 | auto handleFlags(std::vector<std::string> flags) -> void { |
| 153 | 149 | for (const auto& flag : flags) { |
| 154 | 150 | if (flag == "-test") { |
| 155 | - std::cout << "hello!" << std::endl; | |
| 151 | + output.printMessage("hello!"); | |
| 156 | 152 | } |
| 157 | 153 | else { |
| 158 | - std::cout << "unkown option: '" << flag << "'" << std::endl; | |
| 154 | + output.printMessage("unkown option: '" + flag + "'"); | |
| 159 | 155 | } |
| 160 | 156 | } |
| 161 | 157 | } |
| @@ -18,18 +18,14 @@ | |||
| 18 | 18 | ||
| 19 | #include <game.hpp> | 19 | #include <game.hpp> |
| 20 | #include <database.hpp> | 20 | #include <database.hpp> |
| 21 | +#include <output.hpp> | ||
| 21 | 22 | ||
| 22 | using namespace ftxui; | 23 | using namespace ftxui; |
| 23 | 24 | ||
| 25 | +Output output{}; | ||
| 26 | +std::string version{ "v0.1.2" }; | ||
| 24 | std::vector<Game> games; | 27 | std::vector<Game> games; |
| 25 | 28 | ||
| 26 | -bool debugRun = false; | ||
| 27 | - | ||
| 28 | -auto printMessage(std::string message) -> void { | ||
| 29 | - if (debugRun) { | ||
| 30 | - std::cout << message << std::endl; | ||
| 31 | - } | ||
| 32 | -} | ||
| 33 | 29 | ||
| 34 | auto addGame(Database& database, std::string name, std::string path) -> void { | 30 | auto addGame(Database& database, std::string name, std::string path) -> void { |
| 35 | std::string command = | 31 | std::string command = |
| @@ -85,17 +81,17 @@ auto handleCommand(const std::string& command, const std::vector<std::string>& a | |||
| 85 | runGame(gameName); | 81 | runGame(gameName); |
| 86 | } | 82 | } |
| 87 | else if (command == "--version") { | 83 | else if (command == "--version") { |
| 88 | - std::cout << "fluffy v0.1.1" << std::endl; | 84 | + output.printMessage("fluffy " + version); |
| 89 | } | 85 | } |
| 90 | else if (command == "--help") { | 86 | else if (command == "--help") { |
| 91 | - std::cout << "fluffy commands:" << std::endl; | 87 | + output.printMessage("fluffy commands:\n \ |
| 92 | - std::cout << " play <game_name> Runs a game" << std::endl; | 88 | + play <game_name> Runs a game\n \ |
| 93 | - std::cout << " add <game_name> <game_path> Add a game library" << std::endl; | 89 | + add <game_name> <game_path> Add a game library\n \ |
| 94 | - std::cout << " show Show all games added to library" << std::endl; | 90 | + show Show all games added to library\n \ |
| 95 | - std::cout << " rm <game_name> Remove game from a library" << std::endl; | 91 | + rm <game_name> Remove game from a library"); |
| 96 | } | 92 | } |
| 97 | else { | 93 | else { |
| 98 | - std::cout << "fluffy: '" << command << "' is not a fluffy command. See 'fluffy --help'" << std::endl; | 94 | + output.printMessage("fluffy: '" + command + "' is not a fluffy command. See 'fluffy --help'"); |
| 99 | } | 95 | } |
| 100 | } | 96 | } |
| 101 | 97 | ||
| @@ -140,7 +136,7 @@ auto runConsoleApp() -> void { | |||
| 140 | }); | 136 | }); |
| 141 | auto renderer = Renderer(container, [&] { | 137 | auto renderer = Renderer(container, [&] { |
| 142 | return vbox({ | 138 | return vbox({ |
| 143 | - center(text(L" Fluffy v0.1.1 ")), | 139 | + center(text("Fluffy " + version)), |
| 144 | appContainer->Render(), | 140 | appContainer->Render(), |
| 145 | }) | | 141 | }) | |
| 146 | border | size(WIDTH, LESS_THAN, 120); | 142 | border | size(WIDTH, LESS_THAN, 120); |
| @@ -152,10 +148,10 @@ auto runConsoleApp() -> void { | |||
| 152 | auto handleFlags(std::vector<std::string> flags) -> void { | 148 | auto handleFlags(std::vector<std::string> flags) -> void { |
| 153 | for (const auto& flag : flags) { | 149 | for (const auto& flag : flags) { |
| 154 | if (flag == "-test") { | 150 | if (flag == "-test") { |
| 155 | - std::cout << "hello!" << std::endl; | 151 | + output.printMessage("hello!"); |
| 156 | } | 152 | } |
| 157 | else { | 153 | else { |
| 158 | - std::cout << "unkown option: '" << flag << "'" << std::endl; | 154 | + output.printMessage("unkown option: '" + flag + "'"); |
| 159 | } | 155 | } |
| 160 | } | 156 | } |
| 161 | } | 157 | } |