Skip to content

Commit

Permalink
optimizations on ncurses refresh usage, change behavior of lines numb…
Browse files Browse the repository at this point in the history
…ering in RO, small various fixes in gapbuffer, rewrite command run
  • Loading branch information
ikozyris committed Jun 29, 2024
1 parent c93eaf1 commit e4e8a6c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ CC = g++ --std=c++20
# -DHIGHLIGHT Enable syntax highlighting
# -lncursesw Links to ncurses library for wide characters

#CXXFLAGS = -Og -g -Wall -Wextra -pedantic -fopenmp -DDEBUG -lncursesw # Debug only
#CXXFLAGS = -Og -g -Wall -Wextra -pedantic -fopenmp -DDEBUG -DHIGHLIGHT -lncursesw # Debug only
CXXFLAGS = -Ofast -fopenmp -march=native -flto -DHIGHLIGHT -lncursesw
#CXXFLAGS = -g -fopenmp -march=native -DHIGHLIGHT -lncursesw

Expand Down
14 changes: 5 additions & 9 deletions headers/gapbuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ struct gap_buf {
void init(gap_buf &a)
{
a.buffer = (char*)malloc(array_size);
a.len = 0;
a.gps = 0;
a.len = a.gps = 0;
a.gpe = array_size;
a.cpt = array_size;
}
Expand Down Expand Up @@ -93,7 +92,7 @@ void insert_c(gap_buf &a, const unsigned pos, const char ch)
a[pos] = ch;
++a.gps;
} else {
grow_gap(a, pos);
mv_curs(a, pos);
a[pos] = ch;
}
++a.len;
Expand Down Expand Up @@ -150,18 +149,15 @@ inline void eras(gap_buf &a)
}
--a.gps;
--a.len;

}

// TODO: this is a mess
char *data(const gap_buf &a, const unsigned from, const unsigned to)
{
if (a.len == 0)
return 0;
char *tmp = (char*)malloc(to - from + 10);
bzero(tmp, to - from);
if (a.len == 0) {
free(tmp);
return 0;
}
// try some special cases where 1 copy is required
if (a.gps == a.len && a.gpe == a.cpt) // gap ends at end so don't bother
memcpy(tmp, a.buffer + from, min(to, a.gps));
Expand All @@ -180,7 +176,7 @@ char *data(const gap_buf &a, const unsigned from, const unsigned to)
tmp[i - gaplen(a) - 1] = a[i];
//memcpy(tmp, a.buffer + a.gpe + 1, a.gps - to);
}
tmp[to - from + 1] = 0;
tmp[to - from + 2] = 0;
return tmp;
}

Expand Down
48 changes: 29 additions & 19 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ int main(int argc, char *argv[])
init_text();

getmaxyx(text_win, maxy, maxx);
wrefresh(ln_win);
wnoutrefresh(ln_win);
wnoutrefresh(header_win);
doupdate();

read:
if (argc > 1) {
Expand All @@ -58,16 +60,14 @@ int main(int argc, char *argv[])

if (argc > 2 && (strcmp(argv[2], "-ro") == 0 ||
strcmp(argv[2], "--read-only") == 0)) {
read_fread(fi);
print_text_w(0);
read_fread_sl(fi);
printed = print_text_w(0);
print2header("Read-Only", 3);
goto ro;
} else {
read_fread_b(fi);
//read_getc(fi);
read_fread(fi);
print_text();
}
//read_fgets(fi);
fclose(fi);
}
wmove(text_win, 0, 0);
Expand All @@ -80,16 +80,15 @@ int main(int argc, char *argv[])
if (x >= min(ry < curnum ? (it->len - 1 - ofx) : (it->len - ofx), maxx)) // if out of bounds: move (to avoid bugs)
wmove(text_win, y, min((ry != curnum ? it->len - ofx - 1 : it->len - ofx), maxx));
getyx(text_win, y, x);
ry = y + ofy; // calculate offset
ry = y + ofy; // calculate offsets
rx = x + ofx;
mv_curs(*it, rx);

#ifdef DEBUG
print_text(); // debug only
char tmp[128];
sprintf(tmp, "st %u | end %u | cpt %u | len %u | gapLen %u | x %u | currCh %d",
sprintf(tmp, "st %u | end %u | cpt %u | len %u | gapLen %u | x %u | currCh %d",
it->gps, it->gpe, it->cpt, it->len, it->gpe-it->gps, x, it->buffer[x-1]);
clear_header();
print2header(tmp, 1);
wmove(text_win, y, x);
#endif
Expand Down Expand Up @@ -242,11 +241,7 @@ int main(int argc, char *argv[])
bzero(temp, 256 * sizeof(wchar_t));
if (winwstr(text_win, temp) == ERR)
break;
ofx = 0;
for (ofx = maxx; ofx > 0; --ofx)
if (temp[ofx] != ' ' && temp[ofx] != 0)
break;
ofx += 2;
ofx = sizeofline(y);
x = ofx;
print2header(itoa(ofx), 1);
ofx = (long)it->len - (long)ofx;
Expand All @@ -264,9 +259,11 @@ int main(int argc, char *argv[])
reset_header();
print_text();
print_lines();
wrefresh(text_win);
wrefresh(ln_win);
wrefresh(header_win);
wnoutrefresh(text_win);
wnoutrefresh(ln_win);
wnoutrefresh(header_win);
doupdate();
getmaxyx(text_win, maxy, maxx);
wmove(text_win, 0, 0);
it = text.begin();
break;
Expand Down Expand Up @@ -295,7 +292,8 @@ int main(int argc, char *argv[])
}
//*/
ro:
while ((ch = wgetch(text_win))) {
wclear(ln_win);
do {
switch (ch) {
case UP:
if (ppi >= previndx || ppi >= buf_indx)
Expand All @@ -315,7 +313,19 @@ 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
mvwprintw(ln_win, 1, 0, "%3u", buf_indx);
mvwprintw(ln_win, 3, 0, "%3u", printed);
mvwprintw(ln_win, 5, 0, "%3u", previndx);
mvwprintw(ln_win, 7, 0, "%3u", ppi);
wrefresh(ln_win);

} while ((ch = wgetch(text_win)));
stop:
delwin(text_win);
delwin(ln_win);
Expand Down
2 changes: 0 additions & 2 deletions utils/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@ void clear_header()
wmove(header_win, 0, 0);
for (short i = maxx; i != 0; --i)
waddch(header_win, ' ');
wrefresh(header_win);
}

inline void print_header_title()
{
mvwprintw(header_win, 0, maxx / 2 - sizeof(name)/2, "%s", name);
wrefresh(header_win);
}

inline void reset_header()
Expand Down
5 changes: 3 additions & 2 deletions utils/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,16 @@ inline void ins_b(char ch)
}
}

void read_fread(FILE *fi)
// read in single line
void read_fread_sl(FILE *fi)
{
char tmp[SZ];
unsigned a = 0; // bytes read
while ((a = fread(tmp, sizeof(tmp[0]), SZ, fi)))
apnd_s(*it, tmp, a);
}

void read_fread_b(FILE *fi)
void read_fread(FILE *fi)
{
char tmp[SZ];
unsigned a;
Expand Down
19 changes: 16 additions & 3 deletions utils/key_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,21 @@ void command()
} else if (strcmp(tmp, "stats") == 0)
stats();
else if (strcmp(tmp, "run") == 0) {
char *_tmp = input_header("Enter command");
system(_tmp);
char *_tmp = input_header("Enter command: ");
clear();
refresh();
int res = system(_tmp);
if (res != 0)
print2header(itoa(res), 1);
free(_tmp);
getch();
reset_header();
print_text();
print_lines();
wnoutrefresh(ln_win);
wnoutrefresh(header_win);
doupdate();
wmove(text_win, y, x);
} else if (strcmp(tmp, "help") == 0)
print2header("resetheader, shrink, usg, stats, run, setgap", 1);
else if (strcmp(tmp, "setgap") == 0)
Expand Down Expand Up @@ -125,7 +137,8 @@ void enter()
// somewhere below iterator is invalidated
++it;
++curnum;
++ofy;
if (ofy != 0)
++ofy;
text.insert(it, *t);
free(t);
// print text again (or find a better way)
Expand Down

0 comments on commit e4e8a6c

Please sign in to comment.