Skip to content

Commit

Permalink
fix: shrink, change colors via wizard
Browse files Browse the repository at this point in the history
Shrink now shows both the bytes saved by realloc and reported bytes saved by /proc/self/smaps
Switched to optimized build for makefile
  • Loading branch information
ikozyris committed Sep 4, 2024
1 parent 227359f commit 64b091b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 28 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:"
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion headers/gapbuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion headers/vars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ std::list<gap_buf>::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;
Expand Down
30 changes: 20 additions & 10 deletions utils/key_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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);
Expand Down
17 changes: 5 additions & 12 deletions utils/sizes.c
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -35,6 +27,7 @@ unsigned sizeofline(unsigned y) {
return i + 2;
}

// in kB
long memusg()
{
size_t memusg = 0, tmp;
Expand Down
2 changes: 1 addition & 1 deletion wizard.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand Down

0 comments on commit 64b091b

Please sign in to comment.