Skip to content

Commit

Permalink
fix viewing multiple times wrapped line | new: implement home/end for…
Browse files Browse the repository at this point in the history
… wrapped lines
  • Loading branch information
ikozyris committed Jul 12, 2024
1 parent 4d82521 commit b82d49d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 27 deletions.
43 changes: 31 additions & 12 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ int main(int argc, char *argv[])
wmove(text_win, y, 0);
wclrtoeol(text_win);
// printline() with custom start
char *tmp = data2(*it, maxx - 1, it->len);
waddnstr(text_win, tmp, it->len - maxx);
char *tmp = data2(*it, ofx + maxx - 1, ofx + maxx * 2);
waddnstr(text_win, tmp, maxx - 1);
free(tmp);

ofx += maxx - 1;
Expand Down Expand Up @@ -206,10 +206,31 @@ int main(int argc, char *argv[])

case HOME:
wmove(text_win, y, 0);
x = 0; rx = ofx;
if (x == 0 && rx >= maxx - 1) { // line has been wrapped
wclrtoeol(text_win);
print_line(*it);
#ifdef HIGHLIGHT
wmove(text_win, y, 0);
apply(y);
#endif
ofx = 0;
wmove(text_win, y, 0);
}
break;

case END:
wmove(text_win, y, ry != curnum ? it->len - 1 : it->len);
if (it->len > maxx) {
wmove(text_win, y, 0);
wclrtoeol(text_win);
// printline() with custom start
char *tmp = data2(*it, it->len - maxx, it->len);
waddnstr(text_win, tmp, maxx - 1);
free(tmp);

ofx = it->len - maxx;
}
break;

case SAVE:
Expand Down Expand Up @@ -237,10 +258,8 @@ int main(int argc, char *argv[])
wclear(text_win);
goto read;
} else if (ch == OFF) { // calculate x offset
wchar_t temp[256];
bzero(temp, 256 * sizeof(wchar_t));
if (winwstr(text_win, temp) == ERR)
break;
wchar_t *temp = (wchar_t*)malloc(256 * sizeof(wchar_t));
winwstr(text_win, temp);
x = sizeofline(y);
print2header(itoa(x), 1);
ofx = (long)it->len - (long)x;
Expand Down Expand Up @@ -319,12 +338,12 @@ int main(int argc, char *argv[])
case EXIT:
goto stop;
}
#ifdef HIGHLIGHT
for (unsigned i = 0; i < maxy-1; ++i) {
wmove(text_win, i, 0);
apply(i);
}
#endif
#ifdef HIGHLIGHT
for (unsigned i = 0; i < maxy - 1; ++i) {
wmove(text_win, i, 0);
apply(i);
}
#endif
mvwprintw(ln_win, 1, 0, "%3u", buf_indx);
mvwprintw(ln_win, 3, 0, "%3u", printed);
mvwprintw(ln_win, 5, 0, "%3u", previndx);
Expand Down
40 changes: 25 additions & 15 deletions utils/key_func.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
#include "io.cpp"

long memusg()
{
size_t memusg = 0, tmp;
char buffer[1024];
FILE *file = fopen("/proc/self/smaps", "r");
while (fscanf(file, " %1023s", buffer) == 1)
if (strcmp(buffer, "Pss:") == 0) {
fscanf(file, " %lu", &tmp);
memusg += tmp;
}
fclose(file);
return memusg;
}

void stats()
{
char *_tmp = (char*)malloc(256);
unsigned sumlen = 0;
for (auto &i : text)
sumlen += i.len;
snprintf(_tmp, maxx-2, "gap st %u g end %u buff sz %u len %u y %u x %u sum len %u ",
it->gps, it->gpe, it->cpt, it->len, ry, x, sumlen);
snprintf(_tmp, maxx-2, "length %u y %u x %u sum len %u lines %lu ",
it->len, ry, x, sumlen, curnum);
print2header(_tmp, 1);
free(_tmp);
_tmp = 0;
Expand All @@ -20,34 +34,30 @@ void command()
if (strcmp(tmp, "resetheader") == 0)
reset_header();
else if (strcmp(tmp, "shrink") == 0) {
size_t prev = memusg();
txt_cpt = curnum + 1;
text.resize(txt_cpt);
for (auto &i : text)
shrink(i);
size_t curr = memusg();
char buffer[1024] = "";
sprintf(buffer, "saved: %s", hrsize((prev-curr) * 1000));
clear_header();
print2header(buffer, 1);
} else if (strcmp(tmp, "usage") == 0) {
size_t memusg = 0, tmp, pid;
size_t pid;
// stores each word in status file
char buffer[1024] = "";

// Linux-only
FILE *file = fopen("/proc/self/status", "r");
if (!file)
return;

while (fscanf(file, " %1023s", buffer) == 1)
if (strcmp(buffer, "Pid:") == 0) {
fscanf(file, " %lu", &pid);
break;
}
fclose(file);
file = fopen("/proc/self/smaps", "r");
while (fscanf(file, " %1023s", buffer) == 1)
if (strcmp(buffer, "Pss:") == 0) {
fscanf(file, " %lu", &tmp);
memusg += tmp;
}
fclose(file);
sprintf(buffer, "RAM: %s PID: %lu", hrsize(memusg * 1000), pid);
sprintf(buffer, "RAM: %s PID: %lu", hrsize(memusg() * 1000), pid);
clear_header();
print2header(buffer, 1);
} else if (strcmp(tmp, "stats") == 0)
Expand Down Expand Up @@ -118,7 +128,7 @@ void enter()

gap_buf *t = (gap_buf*)malloc(sizeof(gap_buf));
init(*t);
if ((it->cpt != it->gpe)) { // newline is not at the end
if (it->cpt != it->gpe) { // newline is not at the end
char *tmp = data2(*it, rx + 1, it->len + 1);
apnd_s(*t, tmp, it->len - rx - 1);
free(tmp);
Expand Down

0 comments on commit b82d49d

Please sign in to comment.