From 28ad86c6dcfacecc0d9dbfdd4dd24317fa98bb18 Mon Sep 17 00:00:00 2001 From: ikozyris <80053394+ikozyris@users.noreply.github.com> Date: Thu, 24 Oct 2024 18:18:47 +0300 Subject: [PATCH] new: use stack for wrap the stack is used as when a line is wrapped multiple times, the wrap for each line may be different than maxx - 1; maxx - TAB_WIDTH < wrap < maxx tab width = 8 actually deque, but only for the clear() function. As both are based on std:list, there is minimal overhead in performance --- headers/headers.h | 1 + headers/vars.hpp | 4 ++-- main.cpp | 16 +++++++++------- utils/key_func.cpp | 39 ++++++++++++++++++++++----------------- 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/headers/headers.h b/headers/headers.h index 493aa9e..cabe0ce 100644 --- a/headers/headers.h +++ b/headers/headers.h @@ -5,5 +5,6 @@ #include // for setting locale #include // for file size #include +#include #include #include "funcdecl.h" diff --git a/headers/vars.hpp b/headers/vars.hpp index c7c5b0c..c2961af 100644 --- a/headers/vars.hpp +++ b/headers/vars.hpp @@ -6,7 +6,7 @@ #else #define DEFAULT_LINES 16 #endif -unsigned txt_cpt = DEFAULT_LINES; // alloced nodes +unsigned txt_cpt = DEFAULT_LINES; // alloc'ed nodes std::list text(DEFAULT_LINES); std::list::iterator it; @@ -15,7 +15,7 @@ wchar_t s[6]; char s2[6]; unsigned short y, x, len; long ofy; // offset in y axis of text and screen, x axis is in gapbuffer -long wrap; // wrapped dchars TODO: make this a stack? +std::deque wrap; // wrapped dchars TODO: make this a stack? unsigned ry, rx; // x, y positions in buffer/list unsigned buf_indx, printed, previndx, ppi; unsigned maxy, maxx; // to store the maximum rows and columns diff --git a/main.cpp b/main.cpp index bcaaa6b..a2208f1 100755 --- a/main.cpp +++ b/main.cpp @@ -88,12 +88,13 @@ int main(int argc, char *argv[]) case DOWN: if (ry >= curnum) // do not scroll indefinetly break; - if (wrap != 0) { // revert wrap + if (!wrap.empty()) { // revert wrap wmove(text_win, y, 0); print_line(*it); } ++it; - wrap = ofx = 0; // invalidated + wrap.clear(); + ofx = 0; // invalidated if (y == (maxy - 1) && ry < curnum) scrolldown(); else { @@ -103,7 +104,7 @@ int main(int argc, char *argv[]) break; case UP: - if (wrap != 0) { // revert wrap + if (!wrap.empty()) { // revert wrap wmove(text_win, y, 0); print_line(*it); } if (y == 0 && ofy != 0) @@ -113,7 +114,7 @@ int main(int argc, char *argv[]) --it; ofx = calc_offset_dis(x, 0, *it); } - wrap = 0; + wrap.clear(); break; case LEFT: @@ -223,8 +224,8 @@ int main(int argc, char *argv[]) case KEY_RESIZE: case REFRESH: getmaxyx(text_win, maxy, maxx); - ofy = 0; - wrap = ofx = 0; + ofy = ofx = 0; + wrap.clear(); reset_header(); print_text(0); print_lines(); @@ -251,7 +252,8 @@ int main(int argc, char *argv[]) if (s[0] > 0 && s[0] < 32) // not a character break; if (x == maxx - 1) { // wrap line - ofx += (wrap = maxx - 1); + wrap.push_back(maxx - 1); + ofx += maxx - 1; rx = ofx; x = 0; wmove(text_win, y, 0); diff --git a/utils/key_func.cpp b/utils/key_func.cpp index 08dcb44..45bb974 100755 --- a/utils/key_func.cpp +++ b/utils/key_func.cpp @@ -6,10 +6,10 @@ void stats() unsigned sumlen = 0; for (auto &i : text) sumlen += i.len; - //snprintf(_tmp, min(maxx, 256), "maxx %u len %u ofx %ld wrap %ld x: %u | y: %u ", - // maxx, it->len, ofx, wrap, x, y); + //snprintf(_tmp, min(maxx, 256), "maxx %u len %u ofx %ld wrap %u x: %u | y: %u ", + // maxx, it->len, ofx, !wrap.empty() ? wrap.back() : 0, x, y); snprintf(_tmp, min(maxx, 256), "length %u y %u x %u sum len %u lines %lu ofx %ld ", - it->len, ry, x, sumlen, curnum, ofx); + it->len, ry, x, sumlen, curnum, ofx); print2header(_tmp, 1); free(_tmp); wmove(text_win, y, x); @@ -149,14 +149,14 @@ void eol() // it->len - ofx - vis = maxx - 1 (mul by wrapped times) unsigned bytes = dchar2bytes(it->len - ofx - vis, 0, *it); // preliminary calculation of displayed characters in line - wrap = bytes - calc_offset_act(bytes, 0, *it); + wrap.push_back(bytes - calc_offset_act(bytes, 0, *it)); // tab was cut in wrapping, go back to print - if (at(*it, bytes - 1) == '\t' && maxx - wrap > 0) { + if (at(*it, bytes - 1) == '\t' && maxx - wrap.back() > 0) { --bytes; vis += 8; } print_line(*it, bytes, it->len); - ofx = it->len - vis + maxx - 1 - wrap; + ofx = it->len - vis + maxx - 1 - wrap.back(); //ofx = calc_offset_dis(maxx - 1, 0, *it) + maxx - 1; //ofx += wrap; } @@ -165,7 +165,7 @@ void eol() void sol() { wmove(text_win, y, 0); - if (wrap != 0) { // line has been wrapped + if (!wrap.empty()) { // line has been wrapped wclrtoeol(text_win); print_line(*it); #ifdef HIGHLIGHT @@ -173,7 +173,8 @@ void sol() #endif wmove(text_win, y, 0); } - wrap = ofx = 0; + wrap.clear(); + ofx = 0; } void scrolldown() @@ -189,7 +190,8 @@ void scrolldown() apply(y); #endif wmove(text_win, y, 0); - wrap = ofx = 0; + wrap.clear(); + ofx = 0; } void scrollup() @@ -206,7 +208,8 @@ void scrollup() #endif wmove(text_win, 0, x); wrefresh(ln_win); - wrap = ofx = 0; + wrap.clear(); + ofx = 0; } // TODO: is all this complexity needed? @@ -215,9 +218,9 @@ void left() if (x == 0 && ofx > 0) { // line has been wrapped wmove(text_win, y, 0); wclrtoeol(text_win); - ofx -= wrap; - wrap = 0; - print_line(*it, ofx); + ofx -= wrap.back(); + wrap.pop_back(); + print_line(*it, x + ofx); #ifdef HIGHLIGHT apply(y); #endif @@ -233,7 +236,7 @@ void left() if (it->buffer[it->gps - 1] < 0) --ofx; } else if (y > 0) { // x = 0 - if (wrap != 0) // revert wrap + if (!wrap.empty()) // revert wrap print_line(*it); --it; --y; @@ -252,7 +255,8 @@ void right() wrap_line: wmove(text_win, y, 0); wclrtoeol(text_win); - ofx += (wrap += x); + ofx += x; + wrap.push_back(x); print_line(*it, ofx); wmove(text_win, y, 0); } else { @@ -264,11 +268,12 @@ void right() ++ofx; } else { wmove(text_win, y, 0); - if (wrap != 0) // revert wrap + if (!wrap.empty()) // revert wrap print_line(*it); wmove(text_win, y + 1, 0); ++it; - wrap = ofx = 0; + wrap.clear(); + ofx = 0; } } else if (ry == curnum && rx < it->len) goto right_key;