From 64b091b48c207a82e2a672e056f008f1670ab429 Mon Sep 17 00:00:00 2001 From: ikozyris <80053394+ikozyris@users.noreply.github.com> Date: Thu, 5 Sep 2024 00:59:08 +0300 Subject: [PATCH] fix: shrink, change colors via wizard Shrink now shows both the bytes saved by realloc and reported bytes saved by /proc/self/smaps Switched to optimized build for makefile --- CONTRIBUTING.md | 3 ++- Makefile | 4 ++-- headers/gapbuffer.hpp | 2 +- headers/vars.hpp | 2 +- utils/key_func.cpp | 30 ++++++++++++++++++++---------- utils/sizes.c | 17 +++++------------ wizard.sh | 2 +- 7 files changed, 32 insertions(+), 28 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ac2bed7..92b4f92 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -42,9 +42,10 @@ For pull/merge requests (PR/MR): fix: bug when something, regression | perf: x9999 boost in writing The regression exists since [commit number] -More optional information about commit +[More optional information about commit] ``` - If the change is just a one-liner create an issue not a PR/MR - Try to benchmark any optimizations +- Split large commits, and try make commit titles <70 characters Make sure to check any "TODO:" \ No newline at end of file diff --git a/Makefile b/Makefile index 69ce6d2..55d5036 100755 --- a/Makefile +++ b/Makefile @@ -12,8 +12,8 @@ CC = g++ --std=c++20 OPTIM = -Ofast -flto -march=native DEBUG = -g #-DDEBUG -CXXFLAGS = -Wall -Wextra -pedantic-errors $(DEBUG) -DHIGHLIGHT -lncursesw # Debug only -#CXXFLAGS = -Wall -Wextra -pedantic $(OPTIM) -DHIGHLIGHT -lncursesw +#CXXFLAGS = -Wall -Wextra -pedantic-errors $(DEBUG) -DHIGHLIGHT -lncursesw # Debug only +CXXFLAGS = -Wall -Wextra -pedantic $(OPTIM) -DHIGHLIGHT -lncursesw # the build target executable: TARGET = yocto diff --git a/headers/gapbuffer.hpp b/headers/gapbuffer.hpp index bcd1f05..bf79223 100644 --- a/headers/gapbuffer.hpp +++ b/headers/gapbuffer.hpp @@ -204,6 +204,6 @@ unsigned shrink(gap_buf &a) { unsigned bytes = a.cpt; mv_curs(a, a.len); - resize(a, a.len + 1); + resize(a, a.len + 2); return bytes - a.cpt; } diff --git a/headers/vars.hpp b/headers/vars.hpp index 1d8f815..f16aa39 100644 --- a/headers/vars.hpp +++ b/headers/vars.hpp @@ -13,7 +13,7 @@ std::list::iterator it; WINDOW *header_win, *ln_win, *text_win; wchar_t s[6]; char s2[6]; -unsigned char y, x, len; +unsigned short y, x, len; long ofy; // offset in y axis of text and screen, x axis is in gapbuffer long wrap; // last offset due to wrap (if 0; line has not been wrapped) unsigned ry, rx; diff --git a/utils/key_func.cpp b/utils/key_func.cpp index 234e4ed..8e8e41a 100755 --- a/utils/key_func.cpp +++ b/utils/key_func.cpp @@ -6,9 +6,9 @@ void stats() unsigned sumlen = 0; for (auto &i : text) sumlen += i.len; - //snprintf(_tmp, maxx, "st %u | end %u | cpt %u | len %u | maxx %u | ofx %ld wrap %ld x: %u | y: %u ", + //snprintf(_tmp, min(maxx, 256), "st %u | end %u | cpt %u | len %u | maxx %u ofx %ld wrap %ld x: %u | y: %u ", // it->gps, it->gpe, it->cpt, it->len, maxx, ofx, wrap, x, y); - snprintf(_tmp, maxx, "length %u y %u x %u sum len %u lines %lu ofx %ld ", + 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); print2header(_tmp, 1); free(_tmp); @@ -21,7 +21,7 @@ void command() if (strcmp(tmp, "resetheader") == 0) reset_header(); else if (strcmp(tmp, "shrink") == 0) { - size_t prev = memusg(); + size_t prev = memusg(); // RAM usage before // shrink line buffer lnbf_cpt = 16; @@ -30,12 +30,20 @@ void command() txt_cpt = curnum + 1; text.resize(txt_cpt); //shrink each line + size_t bytes_saved = 0; for (auto &i : text) - shrink(i); + bytes_saved += shrink(i); + char *bytes_saved_str = (char*)malloc(24); + hrsize(bytes_saved, bytes_saved_str, 24); - size_t curr = memusg(); - char buffer[1024] = ""; - sprintf(buffer, "saved: %s", hrsize((prev-curr) * 1000)); + size_t curr = memusg(); // RAM usage after + char *prev_curr_str = (char*)malloc(24); + hrsize((prev - curr) * 1000, prev_curr_str, 24); + + char buffer[64] = ""; + snprintf(buffer, 64, "saved: %s | %s", bytes_saved_str, prev_curr_str); + free(bytes_saved_str); + free(prev_curr_str); clear_header(); print2header(buffer, 1); } else if (strcmp(tmp, "usage") == 0) { @@ -51,7 +59,9 @@ void command() break; } fclose(file); - sprintf(buffer, "RAM: %s PID: %lu", hrsize(memusg() * 1000), pid); + char ram_usg[24]; + hrsize(memusg() * 1000, ram_usg, 24); + sprintf(buffer, "RAM: %s PID: %lu", ram_usg, pid); clear_header(); print2header(buffer, 1); } else if (strcmp(tmp, "stats") == 0) @@ -61,9 +71,9 @@ void command() clear(); refresh(); int res = system(_tmp); - if (res != 0) - print2header(itoa(res), 1); free(_tmp); + printw("\nreturned: %d", res); + getch(); reset_header(); print_text(0); diff --git a/utils/sizes.c b/utils/sizes.c index ccdf601..41195b7 100644 --- a/utils/sizes.c +++ b/utils/sizes.c @@ -1,16 +1,9 @@ #include "../headers/vars.hpp" -const char *itoa(long a) -{ - static char b[12]; - sprintf(b, "%ld", a); - return b; -} - // convert bytes to base-10 (SI) human-readable string e.g 1000B = 1kB -const char *hrsize(size_t bytes) +char hrsize(size_t bytes, char *dest, unsigned short dest_cpt) { - const char *suffix[] = {"B", "kB", "MB", "GB", "TB"}; + const char suffix[] = {0, 'k', 'M', 'G', 'T'}; unsigned char length = sizeof(suffix) / sizeof(suffix[0]); double dblBytes = bytes; @@ -19,9 +12,8 @@ const char *hrsize(size_t bytes) for (i = 0; (bytes / 1000) > 0 && i < (unsigned)length - 1; i++, bytes /= 1000) dblBytes = bytes / 1000.0; - static char output[16]; - sprintf(output, "%.02lf %s", dblBytes, suffix[i]); - return output; + snprintf(dest, dest_cpt, "%.02lf %cB", dblBytes, suffix[i]); + return suffix[i]; } // get length of line y @@ -35,6 +27,7 @@ unsigned sizeofline(unsigned y) { return i + 2; } +// in kB long memusg() { size_t memusg = 0, tmp; diff --git a/wizard.sh b/wizard.sh index c662414..7f0f633 100644 --- a/wizard.sh +++ b/wizard.sh @@ -73,7 +73,7 @@ key_config() { } color_config() { - mapfile -t colors < <(sed "s/.*COLOR_//g" utils/highlight.c | head -n 15 | tail -n 6) + mapfile -t colors < <(sed "s/.*COLOR_//g" utils/highlight.c | head -n 28 | tail -n 6) exec 3>&1 # open stdout fd OUTPUT=$(DIALOG --title "Edit Colors" \