From 8798eafd4cc4b32c590a40c276c09b830c5d62da Mon Sep 17 00:00:00 2001 From: seivarya Date: Sat, 20 Jun 2026 18:25:02 +0530 Subject: [refactor]: restructured the project --- src/main.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/main.cpp (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..a0c60cb --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,71 @@ +#include "reader.h" + +int main(int argc, char *argv[]) { + try { + editor_cfg *cfg = (editor_cfg *)malloc(sizeof(editor_cfg)); + if (cfg == nullptr) { + die("[err]: malloc failed\n"); + } + + enable_raw(cfg); + init_editor(cfg); + + int speed { 250000 }; + + if (argc < 2) + die("[err]: no file provided!\n"); + + if (argc > 3) + die("[err]: too many arguments!\n"); + + if (argc == 3) { + if (!is_number(argv[2])) + die("[err]: invalid speed interval provided!\n"); + speed = std::stoi(argv[2]); + } + + FILE *fp = fopen(argv[1], "r"); + + if (fp == nullptr) + die("[err]: fopen failed\n"); + + fseek(fp, 0, SEEK_END); + long size = ftell(fp); + rewind(fp); + + std::string content(size, '\0'); + fread(content.data(), 1, size, fp); + + fclose(fp); + + std::vector data; + split(content, data, ' '); + + while(1) { + for (std::string &k: data) { + if (k.empty()) continue; + editor_refresh_scrn(cfg); + int mid = k.size() / 2; + + write(STDOUT_FILENO, k.substr(0, mid).c_str(), mid); + write(STDOUT_FILENO, "\x1b[31m", 5); + write(STDOUT_FILENO, k.substr(mid, 1).c_str(), 1); + write(STDOUT_FILENO, "\x1b[m", 3); + + int remaining = k.size() - mid - 1; + + write(STDOUT_FILENO, k.substr(mid + 1, remaining).c_str(), remaining); + usleep(speed); + } + } + + free(cfg); + + } catch (const std::exception &e) { + die(e.what()); + } catch (...) { + die("[err]: Unknown exception occurred\n"); + } + + return 0; +} /* main.cpp */ -- cgit v1.2.3 From b5186a5a724649e9eb71fdfbaf3c2a84b36d7c9c Mon Sep 17 00:00:00 2001 From: seivarya Date: Sat, 20 Jun 2026 18:46:10 +0530 Subject: [fix]: fixed incorrect rendering of words due to missing delimeters --- include/reader.h | 2 +- src/main.cpp | 2 +- src/reader.cpp | 14 ++++++-------- 3 files changed, 8 insertions(+), 10 deletions(-) (limited to 'src/main.cpp') diff --git a/include/reader.h b/include/reader.h index 8b61e6c..27b7a90 100644 --- a/include/reader.h +++ b/include/reader.h @@ -67,6 +67,6 @@ int get_cursor_pos(int *rows, int *cols); int get_window_size(int *rows, int *cols); int is_number(char *strnum); -size_t split(const std::string &txt, std::vector &strings, char ch); +size_t split(const std::string &txt, std::vector &strings, const std::string &delims); diff --git a/src/main.cpp b/src/main.cpp index a0c60cb..10c3df6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,7 +39,7 @@ int main(int argc, char *argv[]) { fclose(fp); std::vector data; - split(content, data, ' '); + split(content, data, " \t\r\n,;:!?"); while(1) { for (std::string &k: data) { diff --git a/src/reader.cpp b/src/reader.cpp index dd293db..508205d 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -115,17 +115,15 @@ void editor_refresh_scrn(editor_cfg *cfg) { append_buffer::instance().remove(); } -size_t split(const std::string &txt, std::vector &strings, char ch) { - size_t pos = txt.find(ch); - size_t initial_pos = 0; +size_t split(const std::string &txt, std::vector &strings, const std::string &delims) { + size_t initial_pos = txt.find_first_not_of(delims, 0); + size_t pos = txt.find_first_of(delims, initial_pos); - while (pos != std::string::npos) { + while (initial_pos != std::string::npos) { strings.push_back(txt.substr(initial_pos, pos - initial_pos)); - initial_pos = pos + 1; - pos = txt.find(ch, initial_pos); + initial_pos = txt.find_first_not_of(delims, pos); + pos = txt.find_first_of(delims, initial_pos); } - - strings.push_back(txt.substr(initial_pos)); return strings.size(); } -- cgit v1.2.3