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 --- include/reader.h | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 include/reader.h (limited to 'include') diff --git a/include/reader.h b/include/reader.h new file mode 100644 index 0000000..8b61e6c --- /dev/null +++ b/include/reader.h @@ -0,0 +1,72 @@ +/* reader.h */ + +#define CTRL_KEY(k) ((k) & 0x1f) + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +using editor_cfg = struct editor_cfg { + int screenrows; + int screencols; + termios termios_inst {}; +}; + +class append_buffer { + private: + char *m_b{nullptr}; + uint32_t m_len{0}; + public: + append_buffer() {} + ~append_buffer() = default; + + void append(const char *s, int len) { + char *n = (char *)realloc(m_b, m_len + len); + + if (n == nullptr) + return; + memcpy(&n[m_len], s, len); + m_b = n; + m_len += len; + } + + static append_buffer &instance() { + static append_buffer obj; + return obj; + } + + void remove() { + free(m_b); + m_b = nullptr; + m_len = 0; + } + + void write_output() { + write(STDOUT_FILENO, m_b, m_len); + } +}; + +void init_editor(editor_cfg *cfg); +void die(const char *s); +void disable_raw(editor_cfg *cfg); +void enable_raw(editor_cfg *cfg); +void editor_refresh_scrn(editor_cfg *cfg); + +char editor_readkey(); + +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); + + -- 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 'include') 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