Commit 7775320b authored by mbionchi's avatar mbionchi

again bunch of WIP stuff:

  - index uses the new data format
  - add top/bot functionality
  - optimize file load
  - add winch handling
parent ffe26e8e
......@@ -21,6 +21,16 @@
#include <stdlib.h>
#include <stddef.h>
void free_anim_refs(struct window *window) {
struct anim_ref *iter = window->content.anim_refs;
while (iter != NULL) {
struct anim_ref *tmp = iter;
iter = iter->next;
free(tmp);
}
window->content.anim_refs = NULL;
}
void pop_anim_ref_front(struct window *window) {
struct anim_ref *old = window->content.anim_refs;
if (old != NULL) {
......
......@@ -25,5 +25,6 @@ void push_anim_ref_front(struct window *window, size_t index);
void push_anim_ref_back(struct window *window, size_t index);
void pop_anim_ref_front(struct window *window);
void pop_anim_ref_back(struct window *window);
void free_anim_refs(struct window *window);
#endif
......@@ -191,11 +191,20 @@ struct line *flow_content(struct line *lines, int width) {
// ======== new stuff:
// big todo: errcheck mallocs, strncpys, getlines
struct nline *mkblank() {
struct nline *blank = malloc(sizeof(struct nline));
blank->type = TEXT;
blank->text = malloc(sizeof(struct string));
blank->text->len = 0;
blank->text->data = strcpy(malloc(1*sizeof(char)), "");
return blank;
}
size_t read_nlines(FILE *fp, struct nline ***nlines) {
size_t n_raw = 0;
size_t nmemb = 8,
linear_step = 8;
*nlines = malloc(nmemb*sizeof(struct nline**));
*nlines = malloc(nmemb*sizeof(struct nline*));
char *line = NULL,
*full_line = NULL;
size_t line_buf_len = 0,
......@@ -321,13 +330,8 @@ size_t flow_nlines(struct nline **from, size_t n_from, struct nline ***to, int w
size_t nmemb = 8,
linear_step = 8;
size_t i = 0;
*to = malloc(nmemb*sizeof(struct nline**));
struct nline *blank = malloc(sizeof(struct nline));
blank->type = TEXT;
blank->text = malloc(sizeof(struct string));
blank->text->len = 0;
blank->text->data = strcpy(malloc(1*sizeof(char)), "");
(*to)[n_to] = blank;
*to = malloc(nmemb*sizeof(struct nline*));
(*to)[n_to] = mkblank();
n_to++;
while (i < n_from) {
if (from[i]->type == TEXT) {
......@@ -435,32 +439,64 @@ size_t flow_nlines(struct nline **from, size_t n_from, struct nline ***to, int w
nmemb++;
*to = realloc(*to, nmemb*sizeof(struct nline*));
}
struct nline *blank2 = malloc(sizeof(struct nline));
blank2->type = TEXT;
blank2->text = malloc(sizeof(struct string));
blank2->text->len = 0;
blank2->text->data = strcpy(malloc(1*sizeof(char)), "");
(*to)[n_to] = blank2;
(*to)[n_to] = mkblank();
n_to++;
return n_to;
}
void free_nlines(struct nline **nlines, size_t nmemb) {
size_t gen_index(struct section **sections, size_t nmemb, struct nline ***to, size_t width) {
*to = malloc((nmemb+2)*sizeof(struct nline*));
(*to)[0] = mkblank();
for (size_t i = 0; i < nmemb; i++) {
if (nlines[i]->type == TEXT) {
free(nlines[i]->text->data);
free(nlines[i]->text);
} else if (nlines[i]->type == ANIM) {
for (size_t j = 0; j < nlines[i]->anim->n_frames; j++) {
free(nlines[i]->anim->frames[j].s->data);
free(nlines[i]->anim->frames[j].s);
struct string *s = malloc(sizeof(struct string));
s->data = malloc((width+1)*sizeof(char));
s->len = width;
size_t j = 0;
size_t title_len = strlen(sections[i]->title);
while (j < width-title_len) {
s->data[j] = ' ';
j++;
}
strncpy(s->data+j, sections[i]->title, width-j);
s->data[s->len] = '\0';
struct nline *nline = malloc(sizeof(struct nline));
nline->type = TEXT;
nline->text = s;
(*to)[i+1] = nline;
}
(*to)[nmemb+1] = mkblank();
return nmemb+2;
}
void free_nlines(struct nline **nlines, size_t nmemb) {
if (nlines != NULL) {
for (size_t i = 0; i < nmemb; i++) {
if (nlines[i]->type == TEXT) {
free(nlines[i]->text->data);
free(nlines[i]->text);
} else if (nlines[i]->type == ANIM) {
for (size_t j = 0; j < nlines[i]->anim->n_frames; j++) {
free(nlines[i]->anim->frames[j].s->data);
free(nlines[i]->anim->frames[j].s);
}
free(nlines[i]->anim->frames);
free(nlines[i]->anim);
}
free(nlines[i]->anim->frames);
free(nlines[i]->anim);
free(nlines[i]);
}
free(nlines[i]);
free(nlines);
}
}
void free_content(struct window *window) {
if (window->content.raw != NULL) {
free_nlines(window->content.raw, window->content.n_raw);
window->content.raw = NULL;
}
if (window->content.formatted != NULL) {
free_nlines(window->content.formatted, window->content.n_formatted);
window->content.formatted = NULL;
}
free(nlines);
}
/*
......
......@@ -87,6 +87,8 @@ size_t flow_nlines(struct nline **from, size_t n_from, struct nline ***to, int w
void print_nlines(struct nline **nlines, size_t nmemb);
void free_nlines(struct nline **nlines, size_t nmemb);
size_t gen_index(struct section **sections, size_t nmemb, struct nline ***to, size_t width);
#define ANIM_HINT ";anim"
#define FRAME_HINT ";frame"
#define LOOP_HINT ";loop"
......@@ -99,5 +101,6 @@ void dump_sections(struct section **sections, size_t n_sections);
struct section **read_sections(DIR *dir, char *dirname, int follow_links, size_t *nmemb);
struct line *flow_content(struct line *lines, int width);
void free_lines(struct line *lines);
void free_content(struct window *window);
#endif
......@@ -24,17 +24,6 @@
#include <string.h>
#include <ncurses.h>
void render_index(WINDOW *window, struct section **sections, size_t n_sections) {
int maxx, maxy;
getmaxyx(window, maxy, maxx);
scrollok(window, 0);
for (size_t i=0; i<n_sections && i<maxy-1; i++) {
size_t title_len = strlen(sections[i]->title);
mvwprintw(window, i+1, maxx-title_len, "%s", sections[i]->title);
}
scrollok(window, 1);
}
void render_separator(WINDOW *window, int selected_index) {
int maxx, maxy;
getmaxyx(window, maxy, maxx);
......@@ -45,25 +34,11 @@ void render_separator(WINDOW *window, int selected_index) {
scrollok(window, 1);
}
void render_content(WINDOW *window, struct line *lines) {
int maxx, maxy;
getmaxyx(window, maxy, maxx);
scrollok(window, 0);
int cursor_y = 0;
struct line *iter = lines;
while (iter && cursor_y < maxy) {
mvwprintw(window, cursor_y, 0, "%s", iter->line);
cursor_y++;
iter = iter->next;
}
scrollok(window, 1);
}
// =========================
void render_ncontent(struct window *window) {
size_t cursor_y = 0,
i = 0;
i = window->scroll;
while (i < window->content.n_formatted && cursor_y < window->rows) {
render_nline(window->window, cursor_y, window->content.formatted[i]);
if (window->content.formatted[i]->type == ANIM &&
......
......@@ -24,9 +24,7 @@
#define SEPARATOR_SELECTED " < "
#define SEPARATOR_REGULAR " | "
void render_index(WINDOW *window, struct section **sections, size_t n_sections);
void render_separator(WINDOW *window, int selected_index);
void render_content(WINDOW *window, struct line *lines);
void render_ncontent(struct window *window);
void render_nline(WINDOW *window, size_t cursor_y, struct nline *nline);
......
......@@ -33,79 +33,6 @@ void make_scrollable(WINDOW *window) {
wsetscrreg(window, 0, maxy-1);
}
void scroll_content(WINDOW *window, struct line **content_top, struct line **content_bot, int dy) {
int maxy, maxx;
getmaxyx(window, maxy, maxx);
if (dy > 0 && (*content_bot)->next != NULL) {
wscrl(window, 1);
*content_top = (*content_top)->next;
*content_bot = (*content_bot)->next;
mvwprintw(window, maxy-1, 0, "%s", (*content_bot)->line);
} else if (dy < 0 && (*content_top)->prev != NULL) {
wscrl(window, -1);
*content_top = (*content_top)->prev;
*content_bot = (*content_bot)->prev;
mvwprintw(window, 0, 0, "%s", (*content_top)->line);
} else {
return;
}
wrefresh(window);
}
void top_content(WINDOW *window, struct line **content_top, struct line **content_bot) {
int maxx, maxy;
getmaxyx(window, maxy, maxx);
if ((*content_top)->prev != NULL) {
while ((*content_top)->prev != NULL) {
*content_top = (*content_top)->prev;
}
*content_bot = *content_top;
for (int i=0; i<maxy-1 && (*content_bot)->next; i++) {
*content_bot = (*content_bot)->next;
}
wclear(window);
render_content(window, *content_top);
wrefresh(window);
}
}
void bot_content(WINDOW *window, struct line **content_top, struct line **content_bot) {
if ((*content_bot)->next != NULL) {
while ((*content_bot)->next != NULL) {
*content_top = (*content_top)->next;
*content_bot = (*content_bot)->next;
}
wclear(window);
render_content(window, *content_top);
wrefresh(window);
}
}
void scroll_index(WINDOW *window, struct section **sections, size_t n_sections, int index_scroll, int dy) {
int maxx, maxy;
getmaxyx(window, maxy, maxx);
if (dy > 0) {
wscrl(window, 1);
if (maxy+index_scroll-2 < n_sections) {
size_t title_len = strlen(sections[maxy+index_scroll-2]->title);
scrollok(window, 0);
mvwprintw(window, maxy-1, maxx-title_len, "%s", sections[maxy+index_scroll-2]->title);
scrollok(window, 1);
}
} else if (dy < 0) {
wscrl(window, -1);
if (index_scroll-1 >= 0) {
size_t title_len = strlen(sections[index_scroll-1]->title);
scrollok(window, 0);
mvwprintw(window, 0, maxx-title_len, "%s", sections[index_scroll-1]->title);
scrollok(window, 1);
}
} else {
return;
}
wrefresh(window);
}
/*
* this is somewhat horrifying, but the separator is scrolled in the opposite
* direction from the index!
......
......@@ -22,10 +22,6 @@
#include <ncurses.h>
void make_scrollable(WINDOW *window);
void scroll_content(WINDOW *window, struct line **content_top, struct line **content_bot, int dy);
void top_content(WINDOW *window, struct line **content_top, struct line **content_bot);
void bot_content(WINDOW *window, struct line **content_top, struct line **content_bot);
void scroll_index(WINDOW *window, struct section **sections, size_t n_sections, int index_scroll, int dy);
void scroll_separator(WINDOW *window, int dy);
void scroll_ncontent(struct window *window, int dy);
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment