Commit 7a3ae89c authored by mbionchi's avatar mbionchi

rework scroll logic, fix animation resize bug

parent 46efc827
......@@ -24,6 +24,8 @@
#include <string.h>
#include <sys/stat.h>
#define ERR_OPENING_FILE_STR "<error opening file>"
void print_lines(struct line *lines) {
struct line *iter = lines;
while (iter) {
......@@ -200,6 +202,17 @@ struct nline *mkblank() {
return blank;
}
size_t gen_err_opening(struct nline ***nlines) {
*nlines = malloc(sizeof(struct nline*));
struct nline *nline = malloc(sizeof(struct nline));
nline->type = TEXT;
nline->text = malloc(sizeof(struct string));
nline->text->data = strcpy(malloc((strlen(ERR_OPENING_FILE_STR)+1)*sizeof(char)), ERR_OPENING_FILE_STR);
nline->text->len = strlen(ERR_OPENING_FILE_STR);
(*nlines)[0] = nline;
return 1;
}
size_t read_nlines(FILE *fp, struct nline ***nlines) {
size_t n_raw = 0;
size_t nmemb = 8,
......
......@@ -82,6 +82,8 @@ struct anim_ref {
struct anim_ref *next;
};
size_t gen_err_opening(struct nline ***nlines);
size_t read_nlines(FILE *fp, struct nline ***nlines);
size_t flow_nlines(struct nline **from, size_t n_from, struct nline ***to, int width);
void print_nlines(struct nline **nlines, size_t nmemb);
......
......@@ -17,12 +17,7 @@
#include "site.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
int main(int argc, char **argv) {
exit(site(argc, argv));
......
......@@ -24,18 +24,14 @@
#include <string.h>
#include <ncurses.h>
void render_separator(WINDOW *window, int selected_index) {
int maxx, maxy;
getmaxyx(window, maxy, maxx);
scrollok(window, 0);
for (int i=0; i<maxy; i++) {
mvwprintw(window, i, 0, "%s", i==selected_index?SEPARATOR_SELECTED:SEPARATOR_REGULAR);
void render_separator(struct window *window) {
scrollok(window->window, 0);
for (size_t i = 0; i < window->rows; i++) {
mvwprintw(window->window, i, 0, "%s", i==window->scroll?SEPARATOR_SELECTED:SEPARATOR_REGULAR);
}
scrollok(window, 1);
scrollok(window->window, 1);
}
// =========================
void render_ncontent(struct window *window) {
size_t cursor_y = 0,
i = window->scroll;
......
......@@ -24,8 +24,7 @@
#define SEPARATOR_SELECTED " < "
#define SEPARATOR_REGULAR " | "
void render_separator(WINDOW *window, int selected_index);
void render_separator(struct window *window);
void render_ncontent(struct window *window);
void render_nline(WINDOW *window, size_t cursor_y, struct nline *nline);
......
......@@ -33,25 +33,23 @@ void make_scrollable(WINDOW *window) {
wsetscrreg(window, 0, maxy-1);
}
/*
* this is somewhat horrifying, but the separator is scrolled in the opposite
* direction from the index!
*/
void scroll_separator(WINDOW *window, int dy) {
int maxx, maxy;
getmaxyx(window, maxy, maxx);
if (dy > 0) {
wscrl(window, -1);
mvwprintw(window, 0, 0, "%s", SEPARATOR_REGULAR);
} else if (dy < 0) {
wscrl(window, 1);
scrollok(window, 0);
mvwprintw(window, maxy-1, 0, "%s", SEPARATOR_REGULAR);
scrollok(window, 1);
void scroll_separator(struct window *window, int dy) {
if (0 < dy && window->scroll < window->rows) {
wscrl(window->window, -1);
window->scroll++;
scrollok(window->window, 0);
mvwprintw(window->window, 0, 0, "%s", SEPARATOR_REGULAR);
scrollok(window->window, 1);
} else if (dy < 0 && 1 < window->scroll) {
wscrl(window->window, 1);
window->scroll--;
scrollok(window->window, 0);
mvwprintw(window->window, window->rows-1, 0, "%s", SEPARATOR_REGULAR);
scrollok(window->window, 1);
} else {
return;
}
wrefresh(window);
wrefresh(window->window);
}
void scroll_ncontent(struct window *window, int dy) {
......
......@@ -22,8 +22,8 @@
#include <ncurses.h>
void make_scrollable(WINDOW *window);
void scroll_separator(WINDOW *window, int dy);
void scroll_separator(struct window *window, int dy);
void scroll_ncontent(struct window *window, int dy);
#endif
......@@ -21,6 +21,7 @@
#include "render.h"
#include "scroll.h"
#include "data.h"
#include <errno.h>
#include <ncurses.h>
#include <stdlib.h>
#include <stdio.h>
......@@ -59,14 +60,16 @@ void winch_handler(int signo) {
* cursor possibly reacing the end of screen, and scrollok(window, 1)
* needs to be called after the mvwprintw call.
* TODO:
* - WINCH
* - scroll file list
* - figure out why scrolling fails over telnet (SOMETIMES????) ?
* - error handling everywhere
* - error handling everywhere please
*/
int site(int argc, char **argv) {
signal(SIGWINCH, winch_handler);
if (argc < 2) {
fprintf(stderr, "Usage: %s <path-to-site.d>\n", argv[0]);
exit(EXIT_FAILURE);
}
struct window index_window;
struct window separator_window;
struct window content_window;
......@@ -76,19 +79,25 @@ int site(int argc, char **argv) {
size_t n_sections;
DIR *dir = opendir(argv[1]);
if (dir == NULL) {
perror("opendir");
fprintf(stderr, "[E] %s:%s:%u: %s: %s\n", argv[0], __FILE__, __LINE__, strerror(errno), argv[1]);
exit(1);
}
struct section **sections = read_sections(dir, argv[1], 1, &n_sections);
closedir(dir);
FILE *fp = fopen(sections[selected_index]->filename, "r");
content_window.content.n_raw = read_nlines(fp, &content_window.content.raw);
fclose(fp);
if (fp != NULL) {
content_window.content.n_raw = read_nlines(fp, &content_window.content.raw);
fclose(fp);
} else {
fprintf(stderr, "[W] %s:%s:%u: %s: %s\n", argv[0], __FILE__, __LINE__, strerror(errno), sections[selected_index]->filename);
content_window.content.n_raw = gen_err_opening(&content_window.content.raw);
}
initscr();
cbreak();
halfdelay(1);
noecho();
curs_set(0);
int index_left_margin = 2;
int index_cols = get_index_width(sections, n_sections);
int index_scroll_threshold = 5;
......@@ -100,8 +109,6 @@ int site(int argc, char **argv) {
int content_left_margin = index_left_margin + index_cols + separator_cols;
int content_right_margin = 8;
WINDOW *separator_win = newwin(separator_rows, separator_cols, 0, separator_left_margin);
index_window.cols = get_index_width(sections, n_sections);
index_window.rows = LINES;
index_window.scroll = 0;
......@@ -113,6 +120,11 @@ int site(int argc, char **argv) {
&index_window.content.formatted,
index_window.cols);
separator_window.cols = strlen(SEPARATOR_SELECTED);
separator_window.rows = LINES;
separator_window.scroll = 1;
separator_window.window = newwin(separator_window.rows, separator_window.cols, 0, separator_left_margin);
content_window.cols = COLS - content_left_margin - content_right_margin;
content_window.rows = LINES;
content_window.scroll = 0;
......@@ -125,16 +137,16 @@ int site(int argc, char **argv) {
content_window.cols);
make_scrollable(index_window.window);
make_scrollable(separator_win);
make_scrollable(separator_window.window);
make_scrollable(content_window.window);
render_ncontent(&index_window);
render_separator(separator_win, selected_index+1);
render_separator(&separator_window);
render_ncontent(&content_window);
refresh();
wrefresh(index_window.window);
wrefresh(separator_win);
wrefresh(separator_window.window);
wrefresh(content_window.window);
int ch = getch();
......@@ -144,11 +156,11 @@ int site(int argc, char **argv) {
if (selected_index < n_sections-1) {
selected_index++;
reload_content = 1;
if (selected_index + index_scroll_threshold - index_window.scroll >= index_window.rows &&
index_window.scroll + index_window.rows - 1 <= n_sections ) {
scroll_ncontent(&index_window, 1);
if (separator_window.scroll + index_scroll_threshold < separator_window.rows ||
index_window.content.n_formatted <= index_window.scroll + index_window.rows) {
scroll_separator(&separator_window, 1);
} else {
scroll_separator(separator_win, 1);
scroll_ncontent(&index_window, 1);
}
}
break;
......@@ -156,11 +168,11 @@ int site(int argc, char **argv) {
if (selected_index > 0) {
selected_index--;
reload_content = 1;
if (selected_index-index_window.scroll-index_scroll_threshold <= 0 &&
index_window.scroll > 0) {
scroll_ncontent(&index_window, -1);
if (separator_window.scroll >= index_scroll_threshold ||
index_window.scroll == 0) {
scroll_separator(&separator_window, -1);
} else {
scroll_separator(separator_win, -1);
scroll_ncontent(&index_window, -1);
}
}
break;
......@@ -189,10 +201,9 @@ int site(int argc, char **argv) {
case ERR:
anim_tick(&content_window);
if (had_winch) {
free_nlines(content_window.content.formatted, content_window.content.n_formatted);
delwin(index_window.window);
delwin(separator_win);
delwin(separator_window.window);
delwin(content_window.window);
endwin();
......@@ -201,14 +212,18 @@ int site(int argc, char **argv) {
index_window.rows = LINES;
index_window.window = newwin(index_window.rows, index_window.cols, 0, index_left_margin);
separator_rows = LINES;
separator_win = newwin(separator_rows, separator_cols, 0, separator_left_margin);
separator_window.rows = LINES;
separator_window.scroll = selected_index+1;
separator_window.window = newwin(separator_window.rows, separator_window.cols, 0, separator_left_margin);
content_window.cols = COLS - content_left_margin - content_right_margin;
content_window.rows = LINES;
content_window.scroll = 0;
content_window.window = newwin(content_window.rows, content_window.cols, 0, content_left_margin);
free_nlines(content_window.content.formatted, content_window.content.n_formatted);
free_anim_refs(&content_window);
content_window.content.n_formatted =
flow_nlines(content_window.content.raw,
content_window.content.n_raw,
......@@ -216,17 +231,17 @@ int site(int argc, char **argv) {
content_window.cols);
make_scrollable(index_window.window);
make_scrollable(separator_win);
make_scrollable(separator_window.window);
make_scrollable(content_window.window);
render_ncontent(&index_window);
render_separator(separator_win, selected_index+1);
render_separator(&separator_window);
render_ncontent(&content_window);
refresh();
wrefresh(index_window.window);
wrefresh(separator_win);
wrefresh(separator_window.window);
wrefresh(content_window.window);
had_winch = 0;
......@@ -236,8 +251,13 @@ int site(int argc, char **argv) {
free_anim_refs(&content_window);
FILE *fp = fopen(sections[selected_index]->filename, "r");
content_window.content.n_raw = read_nlines(fp, &content_window.content.raw);
fclose(fp);
if (fp != NULL) {
content_window.content.n_raw = read_nlines(fp, &content_window.content.raw);
fclose(fp);
} else {
fprintf(stderr, "[W] %s:%s:%u: %s: %s\n", argv[0], __FILE__, __LINE__, strerror(errno), sections[selected_index]->filename);
content_window.content.n_raw = gen_err_opening(&content_window.content.raw);
}
content_window.content.n_formatted =
flow_nlines(content_window.content.raw,
......@@ -261,7 +281,7 @@ int site(int argc, char **argv) {
free_content(&content_window);
free_anim_refs(&content_window);
delwin(index_window.window);
delwin(separator_win);
delwin(separator_window.window);
delwin(content_window.window);
endwin();
return 0;
......
......@@ -18,9 +18,6 @@
#ifndef _TELNET_SITE_SITE_H
#define _TELNET_SITE_SITE_H
#include <unistd.h>
#include <stddef.h>
int site(int argc, char **argv);
#endif
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