juk3n/fluffypublic Fork
⑂ ddced2d
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.

Merge pull request #26 from Juk3n/25-create-class-for-printing-and-debug-printingUnverified

25 create class for printing and debug printing
Adrian Duraj committed 2024-07-08T12:29:59+02:00 Browse files
ddced2d parents: ffd2745 abdac1e
modified app/include/database.hpp +8 -7
@@ -4,10 +4,12 @@
44 #include <string>
55
66 #include <game.hpp>
7+#include <output.hpp>
78
89 class Database
910 {
1011 private:
12+ Output output{};
1113 sqlite3 *database;
1214
1315 std::string database_initialization_command =
@@ -19,7 +21,7 @@ private:
1921
2022 auto initializeDatabase() -> void {
2123 executeSqlCommand(database_initialization_command);
22- //printMessage("Database Initialized");
24+ output.printDebugMessage("Database Initialized");
2325 }
2426
2527 public:
@@ -38,7 +40,8 @@ public:
3840 exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError);
3941
4042 if (exit != SQLITE_OK) {
41- //printMessage("Error with command: " + command + " " + messageError);
43+
44+ output.printDebugMessage("Error with command: " + command + " " + messageError);
4245 sqlite3_free(messageError);
4346 throw std::exception();
4447 }
@@ -53,12 +56,11 @@ public:
5356 exit = sqlite3_open(databasePath.c_str(), &database);
5457
5558 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)});
5860 throw std::exception();
5961 }
6062
61- //printMessage("Opened database successfully!");
63+ output.printDebugMessage("Opened database successfully!");
6264
6365 if (newCreation) {
6466 initializeDatabase();
@@ -71,8 +73,7 @@ public:
7173 exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr);
7274
7375 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)});
7677 sqlite3_close(database);
7778 return {};
7879 }
@@ -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 Database9 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 @@
1818
1919 #include <game.hpp>
2020 #include <database.hpp>
21+#include <output.hpp>
2122
2223 using namespace ftxui;
2324
25+Output output{};
26+std::string version{ "v0.1.2" };
2427 std::vector<Game> games;
2528
26-bool debugRun = false;
27-
28-auto printMessage(std::string message) -> void {
29- if (debugRun) {
30- std::cout << message << std::endl;
31- }
32-}
3329
3430 auto addGame(Database& database, std::string name, std::string path) -> void {
3531 std::string command =
@@ -85,17 +81,17 @@ auto handleCommand(const std::string& command, const std::vector<std::string>& a
8581 runGame(gameName);
8682 }
8783 else if (command == "--version") {
88- std::cout << "fluffy v0.1.1" << std::endl;
84+ output.printMessage("fluffy " + version);
8985 }
9086 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");
9692 }
9793 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'");
9995 }
10096 }
10197
@@ -140,7 +136,7 @@ auto runConsoleApp() -> void {
140136 });
141137 auto renderer = Renderer(container, [&] {
142138 return vbox({
143- center(text(L" Fluffy v0.1.1 ")),
139+ center(text("Fluffy " + version)),
144140 appContainer->Render(),
145141 }) |
146142 border | size(WIDTH, LESS_THAN, 120);
@@ -152,10 +148,10 @@ auto runConsoleApp() -> void {
152148 auto handleFlags(std::vector<std::string> flags) -> void {
153149 for (const auto& flag : flags) {
154150 if (flag == "-test") {
155- std::cout << "hello!" << std::endl;
151+ output.printMessage("hello!");
156152 }
157153 else {
158- std::cout << "unkown option: '" << flag << "'" << std::endl;
154+ output.printMessage("unkown option: '" + flag + "'");
159155 }
160156 }
161157 }
@@ -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 }