juk3n/fluffypublic Fork
⑂ e100cdf
Commits
⬇ Clone ▾
git clone https://git.rickub.com/juk3n/fluffy.git
git clone ssh://git@rickub.com/juk3n/fluffy.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

27 add handling debug flag (#28)Unverified

* added sharing the same output between main and database class

* added message when turning debug on
Adrian Duraj committed 2024-08-02T17:41:20+02:00 Browse files
e100cdf parent: e9cce8b
modified app/include/database.hpp +9 -7
@@ -2,6 +2,7 @@
22
33 #include <sqlite3.h>
44 #include <string>
5+#include <memory>
56
67 #include <game.hpp>
78 #include <output.hpp>
@@ -9,7 +10,7 @@
910 class Database
1011 {
1112 private:
12- Output output{};
13+ std::shared_ptr<Output> output{};
1314 sqlite3 *database;
1415
1516 std::string database_initialization_command =
@@ -21,12 +22,13 @@ private:
2122
2223 auto initializeDatabase() -> void {
2324 executeSqlCommand(database_initialization_command);
24- output.printDebugMessage("Database Initialized");
25+ output->printDebugMessage("Database Initialized");
2526 }
2627
2728 public:
2829
29- Database(std::filesystem::path databasePath) {
30+ Database(std::filesystem::path databasePath, std::shared_ptr<Output> output) {
31+ this->output = output;
3032 loadDatabase(databasePath);
3133 }
3234
@@ -41,7 +43,7 @@ public:
4143
4244 if (exit != SQLITE_OK) {
4345
44- output.printDebugMessage("Error with command: " + command + " " + messageError);
46+ output->printDebugMessage("Error with command: " + command + " " + messageError);
4547 sqlite3_free(messageError);
4648 throw std::exception();
4749 }
@@ -56,11 +58,11 @@ public:
5658 exit = sqlite3_open(databasePath.c_str(), &database);
5759
5860 if (exit) {
59- output.printDebugError("Error opening database" + std::string{sqlite3_errmsg(database)});
61+ output->printDebugError("Error opening database" + std::string{sqlite3_errmsg(database)});
6062 throw std::exception();
6163 }
6264
63- output.printDebugMessage("Opened database successfully!");
65+ output->printDebugMessage("Opened database successfully!");
6466
6567 if (newCreation) {
6668 initializeDatabase();
@@ -73,7 +75,7 @@ public:
7375 exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr);
7476
7577 if (exit) {
76- output.printDebugError("Error retrieving data" + std::string{sqlite3_errmsg(database)});
78+ output->printDebugError("Error retrieving data" + std::string{sqlite3_errmsg(database)});
7779 sqlite3_close(database);
7880 return {};
7981 }
@@ -2,6 +2,7 @@
2 2
3 #include <sqlite3.h>3 #include <sqlite3.h>
4 #include <string>4 #include <string>
5+#include <memory>
5 6
6 #include <game.hpp>7 #include <game.hpp>
7 #include <output.hpp>8 #include <output.hpp>
@@ -9,7 +10,7 @@
9 class Database10 class Database
10 {11 {
11 private:12 private:
12- Output output{};13+ std::shared_ptr<Output> output{};
13 sqlite3 *database;14 sqlite3 *database;
14 15
15 std::string database_initialization_command =16 std::string database_initialization_command =
@@ -21,12 +22,13 @@ private:
21 22
22 auto initializeDatabase() -> void {23 auto initializeDatabase() -> void {
23 executeSqlCommand(database_initialization_command);24 executeSqlCommand(database_initialization_command);
24- output.printDebugMessage("Database Initialized");25+ output->printDebugMessage("Database Initialized");
25 }26 }
26 27
27 public:28 public:
28 29
29- Database(std::filesystem::path databasePath) {30+ Database(std::filesystem::path databasePath, std::shared_ptr<Output> output) {
31+ this->output = output;
30 loadDatabase(databasePath);32 loadDatabase(databasePath);
31 }33 }
32 34
@@ -41,7 +43,7 @@ public:
41 43
42 if (exit != SQLITE_OK) {44 if (exit != SQLITE_OK) {
43 45
44- output.printDebugMessage("Error with command: " + command + " " + messageError);46+ output->printDebugMessage("Error with command: " + command + " " + messageError);
45 sqlite3_free(messageError);47 sqlite3_free(messageError);
46 throw std::exception();48 throw std::exception();
47 }49 }
@@ -56,11 +58,11 @@ public:
56 exit = sqlite3_open(databasePath.c_str(), &database);58 exit = sqlite3_open(databasePath.c_str(), &database);
57 59
58 if (exit) {60 if (exit) {
59- output.printDebugError("Error opening database" + std::string{sqlite3_errmsg(database)});61+ output->printDebugError("Error opening database" + std::string{sqlite3_errmsg(database)});
60 throw std::exception();62 throw std::exception();
61 }63 }
62 64
63- output.printDebugMessage("Opened database successfully!");65+ output->printDebugMessage("Opened database successfully!");
64 66
65 if (newCreation) {67 if (newCreation) {
66 initializeDatabase();68 initializeDatabase();
@@ -73,7 +75,7 @@ public:
73 exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr);75 exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr);
74 76
75 if (exit) {77 if (exit) {
76- output.printDebugError("Error retrieving data" + std::string{sqlite3_errmsg(database)});78+ output->printDebugError("Error retrieving data" + std::string{sqlite3_errmsg(database)});
77 sqlite3_close(database);79 sqlite3_close(database);
78 return {};80 return {};
79 }81 }
modified app/include/output.hpp +5 -0
@@ -7,6 +7,11 @@ class Output {
77 private:
88 bool debugRun = false;
99 public:
10+ auto turnOnDebug() -> void {
11+ debugRun = true;
12+ printDebugMessage("Debug mode on");
13+ }
14+
1015 auto printDebugMessage(std::string message) -> void {
1116 if (debugRun)
1217 std::cout << message << std::endl;
@@ -7,6 +7,11 @@ class Output {
7 private:7 private:
8 bool debugRun = false;8 bool debugRun = false;
9 public:9 public:
10+ auto turnOnDebug() -> void {
11+ debugRun = true;
12+ printDebugMessage("Debug mode on");
13+ }
14+
10 auto printDebugMessage(std::string message) -> void {15 auto printDebugMessage(std::string message) -> void {
11 if (debugRun)16 if (debugRun)
12 std::cout << message << std::endl;17 std::cout << message << std::endl;
modified app/src/main.cpp +14 -13
@@ -15,6 +15,7 @@
1515 #include <vector>
1616 #include <unistd.h>
1717 #include <limits.h>
18+#include <memory>
1819
1920 #include <game.hpp>
2021 #include <database.hpp>
@@ -22,11 +23,10 @@
2223
2324 using namespace ftxui;
2425
25-Output output{};
26+std::shared_ptr<Output> output{};
2627 std::string version{ "v0.1.2" };
2728 std::vector<Game> games;
2829
29-
3030 auto addGame(Database& database, std::string name, std::string path) -> void {
3131 std::string command =
3232 "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES (";
@@ -81,17 +81,17 @@ auto handleCommand(const std::string& command, const std::vector<std::string>& a
8181 runGame(gameName);
8282 }
8383 else if (command == "--version") {
84- output.printMessage("fluffy " + version);
84+ output->printMessage("fluffy " + version);
8585 }
8686 else if (command == "--help") {
87- output.printMessage("fluffy commands:\n \
87+ output->printMessage("fluffy commands:\n \
8888 play <game_name> Runs a game\n \
8989 add <game_name> <game_path> Add a game library\n \
9090 show Show all games added to library\n \
9191 rm <game_name> Remove game from a library");
9292 }
9393 else {
94- output.printMessage("fluffy: '" + command + "' is not a fluffy command. See 'fluffy --help'");
94+ output->printMessage("fluffy: '" + command + "' is not a fluffy command. See 'fluffy --help'");
9595 }
9696 }
9797
@@ -147,11 +147,11 @@ auto runConsoleApp() -> void {
147147
148148 auto handleFlags(std::vector<std::string> flags) -> void {
149149 for (const auto& flag : flags) {
150- if (flag == "-test") {
151- output.printMessage("hello!");
150+ if (flag == "-d") {
151+ output->turnOnDebug();
152152 }
153153 else {
154- output.printMessage("unkown option: '" + flag + "'");
154+ output->printMessage("unkown option: '" + flag + "'");
155155 }
156156 }
157157 }
@@ -189,15 +189,16 @@ auto parseCommand(int argc, char const* argv[]) {
189189
190190 auto main(int argc, char const *argv[]) -> int {
191191 try {
192+ output = std::make_shared<Output>();
193+ auto flags = parseFlags(argc, argv);
194+ handleFlags(flags);
195+
192196 auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")};
193- Database database{databasePath};
197+ Database database{databasePath, output};
194198 games = database.getGames();
195199
196- if (argc > 1) {
197- auto flags = parseFlags(argc, argv);
200+ if (argc - flags.size() > 1) {
198201 auto [command, arguments] = parseCommand(argc, argv);
199-
200- handleFlags(flags);
201202 if (command != "") handleCommand(command, arguments, database);
202203 }
203204 else {
@@ -15,6 +15,7 @@
15 #include <vector>15 #include <vector>
16 #include <unistd.h>16 #include <unistd.h>
17 #include <limits.h>17 #include <limits.h>
18+#include <memory>
18 19
19 #include <game.hpp>20 #include <game.hpp>
20 #include <database.hpp>21 #include <database.hpp>
@@ -22,11 +23,10 @@
22 23
23 using namespace ftxui;24 using namespace ftxui;
24 25
25-Output output{};26+std::shared_ptr<Output> output{};
26 std::string version{ "v0.1.2" };27 std::string version{ "v0.1.2" };
27 std::vector<Game> games;28 std::vector<Game> games;
28 29
29-
30 auto addGame(Database& database, std::string name, std::string path) -> void {30 auto addGame(Database& database, std::string name, std::string path) -> void {
31 std::string command =31 std::string command =
32 "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES (";32 "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES (";
@@ -81,17 +81,17 @@ auto handleCommand(const std::string& command, const std::vector<std::string>& a
81 runGame(gameName);81 runGame(gameName);
82 }82 }
83 else if (command == "--version") {83 else if (command == "--version") {
84- output.printMessage("fluffy " + version);84+ output->printMessage("fluffy " + version);
85 }85 }
86 else if (command == "--help") {86 else if (command == "--help") {
87- output.printMessage("fluffy commands:\n \87+ output->printMessage("fluffy commands:\n \
88 play <game_name> Runs a game\n \88 play <game_name> Runs a game\n \
89 add <game_name> <game_path> Add a game library\n \89 add <game_name> <game_path> Add a game library\n \
90 show Show all games added to library\n \90 show Show all games added to library\n \
91 rm <game_name> Remove game from a library");91 rm <game_name> Remove game from a library");
92 }92 }
93 else {93 else {
94- output.printMessage("fluffy: '" + command + "' is not a fluffy command. See 'fluffy --help'");94+ output->printMessage("fluffy: '" + command + "' is not a fluffy command. See 'fluffy --help'");
95 }95 }
96 }96 }
97 97
@@ -147,11 +147,11 @@ auto runConsoleApp() -> void {
147 147
148 auto handleFlags(std::vector<std::string> flags) -> void {148 auto handleFlags(std::vector<std::string> flags) -> void {
149 for (const auto& flag : flags) {149 for (const auto& flag : flags) {
150- if (flag == "-test") {150+ if (flag == "-d") {
151- output.printMessage("hello!");151+ output->turnOnDebug();
152 }152 }
153 else {153 else {
154- output.printMessage("unkown option: '" + flag + "'");154+ output->printMessage("unkown option: '" + flag + "'");
155 }155 }
156 }156 }
157 }157 }
@@ -189,15 +189,16 @@ auto parseCommand(int argc, char const* argv[]) {
189 189
190 auto main(int argc, char const *argv[]) -> int {190 auto main(int argc, char const *argv[]) -> int {
191 try {191 try {
192+ output = std::make_shared<Output>();
193+ auto flags = parseFlags(argc, argv);
194+ handleFlags(flags);
195+
192 auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")};196 auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")};
193- Database database{databasePath};197+ Database database{databasePath, output};
194 games = database.getGames();198 games = database.getGames();
195 199
196- if (argc > 1) {200+ if (argc - flags.size() > 1) {
197- auto flags = parseFlags(argc, argv);
198 auto [command, arguments] = parseCommand(argc, argv);201 auto [command, arguments] = parseCommand(argc, argv);
199-
200- handleFlags(flags);
201 if (command != "") handleCommand(command, arguments, database);202 if (command != "") handleCommand(command, arguments, database);
202 }203 }
203 else {204 else {