Skip to content

Commit

Permalink
new: use stack for wrap
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ikozyris committed Oct 24, 2024
1 parent d28b970 commit 28ad86c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
1 change: 1 addition & 0 deletions headers/headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
#include <locale.h> // for setting locale
#include <sys/stat.h> // for file size
#include <list>
#include <deque>
#include <time.h>
#include "funcdecl.h"
4 changes: 2 additions & 2 deletions headers/vars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<gap_buf> text(DEFAULT_LINES);
std::list<gap_buf>::iterator it;

Expand All @@ -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<unsigned> 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
Expand Down
16 changes: 9 additions & 7 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down
39 changes: 22 additions & 17 deletions utils/key_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -165,15 +165,16 @@ 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
apply(y);
#endif
wmove(text_win, y, 0);
}
wrap = ofx = 0;
wrap.clear();
ofx = 0;
}

void scrolldown()
Expand All @@ -189,7 +190,8 @@ void scrolldown()
apply(y);
#endif
wmove(text_win, y, 0);
wrap = ofx = 0;
wrap.clear();
ofx = 0;
}

void scrollup()
Expand All @@ -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?
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit 28ad86c

Please sign in to comment.