Skip to content

Commit

Permalink
refactor: rename header files and improve error handling in storage
Browse files Browse the repository at this point in the history
  • Loading branch information
2giosangmitom committed Jan 24, 2025
1 parent 1696837 commit 3007633
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 139 deletions.
28 changes: 15 additions & 13 deletions src/cli/table.hpp → src/cli/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@
#include <list>
#include <string>

#include "../cli/todo.hpp"
#include "../model.h"

inline void print_border(const std::array<size_t, 3> &col_wids) {
for (const auto &wid : col_wids) {
constexpr size_t kDefaultTitleWidth = 7;
constexpr size_t kPriorityWidth = 10;

inline void print_border(const std::array<size_t, 3>& col_wids) {
for (const auto& wid : col_wids) {
fmt::print("+{}", std::string(wid, '-'));
}
fmt::println("+");
}

inline void print_row(const Todo &todo, const std::array<size_t, 3> &col_wids) {
std::string priority;
inline void print_row(const Todo& todo, const std::array<size_t, 3>& col_wids) {
std::string_view priority;
fmt::color priority_color;

switch (todo.priority) {
Expand All @@ -60,8 +63,8 @@ inline void print_row(const Todo &todo, const std::array<size_t, 3> &col_wids) {
priority_color = fmt::color::coral;
break;
default:
priority_color = fmt::color::aquamarine;
priority = "Unknown"; // Handle unexpected priorities
priority_color = fmt::color::aquamarine;
break;
}

Expand All @@ -73,18 +76,17 @@ inline void print_row(const Todo &todo, const std::array<size_t, 3> &col_wids) {
std::string(col_wids[2] - 2 - priority.size(), ' '));
}

inline void print_table(const std::list<Todo> &content) {
inline void print_table(const std::list<Todo>& content) {
const size_t fcol_wid = std::max(std::to_string(content.size()).size() + 2,
static_cast<size_t>(4));
size_t crcol_wid = 7;
const size_t lcol_wid = 10;
size_t crcol_wid = kDefaultTitleWidth;

// Calculate the maximum width for the title column
for (const auto &todo : content) {
for (const auto& todo : content) {
crcol_wid = std::max(todo.title.size() + 2, crcol_wid);
}

std::array<size_t, 3> col_wids = {fcol_wid, crcol_wid, lcol_wid};
std::array<size_t, 3> col_wids = {fcol_wid, crcol_wid, kPriorityWidth};

print_border(col_wids);

Expand All @@ -93,11 +95,11 @@ inline void print_table(const std::list<Todo> &content) {
fmt::styled("ID", fmt::emphasis::bold | fmt::emphasis::underline),
std::string(fcol_wid - 4, ' '),
fmt::styled("Title", fmt::emphasis::bold | fmt::emphasis::underline),
std::string(crcol_wid - 7, ' '),
std::string(crcol_wid - kDefaultTitleWidth, ' '),
fmt::styled("Priority", fmt::emphasis::bold | fmt::emphasis::underline));
print_border(col_wids);

for (const auto &todo : content) {
for (const auto& todo : content) {
print_row(todo, col_wids);
}

Expand Down
60 changes: 29 additions & 31 deletions src/io/storage.hpp → src/fs/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,73 +30,71 @@
#include <cstdint>
#include <fstream>
#include <list>
#include <sstream>
#include <string>

#include "../cli/todo.hpp"
#include "../utils/fmt.hpp"
#include "../model.h"
#include "../utils/fmt.h"

class Storage {
private:
std::fstream file;
std::string file_path;

public:
Storage(const std::string &path) : file_path{path} {
// Open the file for reading and writing, create it if it doesn't exist
file.open(file_path, std::ios::in | std::ios::out | std::ios::app);

if (!file.is_open()) {
print_error("Failed to open file: " + file_path);
}
}
Storage(const std::string &path) : file_path{path} {}

std::list<Todo> read_todos() {
std::ifstream file(file_path, std::ios::in | std::ios::binary);
std::string line;
std::list<Todo> res;

if (!file.is_open()) {
print_error("File is not open for reading: " + file_path);
print_error("Failed to open file for reading: " + file_path);
return res; // Return empty list
}

while (getline(file, line)) {
while (std::getline(file, line)) {
if (line.empty()) continue; // Skip empty lines

uint32_t priority = line.back() - '0';
std::string title = line.substr(0, line.size() - 2);
std::istringstream iss(line);
std::string title;
uint32_t priority;

// Validate priority
if (priority < 1 || priority > 3) {
priority = 0; // Set to 0 for unknown
if (std::getline(iss, title, '|') && iss >> priority) {
if (priority < 1 || priority > 3) {
priority = 0; // Set to 0 for unknown
}
Todo t{uint32_t(res.size() + 1), title, priority};
res.push_back(t);
}

Todo t{uint32_t(res.size() + 1), title, priority};
res.push_back(t);
}

return res;
}

void add_todo(const std::string &title, const uint32_t &priority) {
std::ofstream file(file_path,
std::ios::out | std::ios::app | std::ios::binary);

if (!file.is_open()) {
print_error("File is not open for appending: " + file_path);
print_error("Failed to open file for appending: " + file_path);
return;
}

file << title << "|" << priority << std::endl;
}

void write_todos(std::list<Todo> &new_todos) {
file.close();
file.open(file_path, std::ios::out | std::ios::trunc);
void write_todos(const std::list<Todo> &new_todos) {
std::ofstream file(file_path,
std::ios::out | std::ios::trunc | std::ios::binary);

for (const Todo &t : new_todos) {
add_todo(t.title, t.priority);
if (!file.is_open()) {
print_error("Failed to open file for writing: " + file_path);
return;
}
}

~Storage() {
if (file.is_open()) {
file.close();
for (const Todo &t : new_todos) {
file << t.title << "|" << t.priority << std::endl;
}
}
};
Expand Down
Loading

0 comments on commit 3007633

Please sign in to comment.