Commit 353df3f9 authored by mbionchi's avatar mbionchi

add alignment directives

parent 905b578a
......@@ -83,6 +83,21 @@ will be aligned to the first letter character on this line, for example:
number
one
- You can change text or animation alignment by using the `;align <how>`
directive, on a line by itself, where <how> is one-of {left,right,center}.
The chosen alignment will apply until explicitly changed. Example:
;align center
* * *
;align right
This paragraph will be right-aligned.
And same for this one.
;align left
However, this paragraph will be left-aligned! How wonderful.
ANIMATIONS
----------
......
......@@ -17,6 +17,7 @@
#include "anim.h"
#include "data.h"
#include "render.h"
#include <stdlib.h>
#include <stddef.h>
......@@ -106,8 +107,7 @@ void anim_tick(struct window *window) {
window->content.lines->formatted[i]->anim->current_frame_index =
(window->content.lines->formatted[i]->anim->current_frame_index + 1) %
window->content.lines->formatted[i]->anim->n_frames;
mvwprintw(window->window, y, 0, "%s",
window->content.lines->formatted[i]->anim->frames[window->content.lines->formatted[i]->anim->current_frame_index].s->data);
render_nline(window, y, window->content.lines->formatted[i]);
i++;
y++;
while (i < window->content.lines->n_formatted &&
......@@ -116,8 +116,7 @@ void anim_tick(struct window *window) {
window->content.lines->formatted[i]->anim->current_frame_index =
(window->content.lines->formatted[i]->anim->current_frame_index + 1) %
window->content.lines->formatted[i]->anim->n_frames;
mvwprintw(window->window, y, 0, "%s",
window->content.lines->formatted[i]->anim->frames[window->content.lines->formatted[i]->anim->current_frame_index].s->data);
render_nline(window, y, window->content.lines->formatted[i]);
i++;
y++;
}
......
......@@ -22,6 +22,7 @@
#include "data.h"
#include "module.h"
#include <dlfcn.h>
#include <stdlib.h>
#include <stddef.h>
......@@ -171,6 +172,10 @@ struct nline *string2nline(char *str) {
return nline;
}
/*
* the following two functions should be rewritten in a more
* understanable and maintainable way
*/
size_t read_nlines(FILE *fp, struct nline ***nlines) {
size_t n_raw = 0;
size_t nmemb = 8,
......@@ -180,6 +185,7 @@ size_t read_nlines(FILE *fp, struct nline ***nlines) {
*full_line = NULL;
size_t line_buf_len = 0,
full_line_len = 0;
enum align align = LEFT;
ssize_t rv = getline(&line, &line_buf_len, fp);
while (rv != -1) {
line[rv-1] = '\0';
......@@ -206,6 +212,7 @@ size_t read_nlines(FILE *fp, struct nline ***nlines) {
if (n_frames < 1) {
struct nline *nline = malloc(sizeof(struct nline));
nline->type = ANIM;
nline->align = align;
nline->anim = malloc(sizeof(struct animation));
nline->anim->nmemb_frames = 8;
nline->anim->frames = malloc(nline->anim->nmemb_frames*sizeof(struct frame));
......@@ -261,6 +268,21 @@ size_t read_nlines(FILE *fp, struct nline ***nlines) {
(*nlines)[n_raw]->anim->loop = 0;
}
n_raw += n_anim_lines;
free(line);
goto next_line;
} else if (!strncmp(line, ALIGN_HINT, strlen(ALIGN_HINT))) {
char *how = index(line, ' ');
if (how != NULL) {
how++;
if (!strcmp(how, ALIGN_LEFT_HINT)) {
align = LEFT;
} else if (!strcmp(how, ALIGN_RIGHT_HINT)) {
align = RIGHT;
} else if (!strcmp(how, ALIGN_CENTER_HINT)) {
align = CENTER;
}
}
free(line);
goto next_line;
} else {
full_line = line;
......@@ -277,6 +299,7 @@ size_t read_nlines(FILE *fp, struct nline ***nlines) {
if (full_line != NULL && (full_line_len == 0 || full_line[full_line_len-1] != ' ')) {
struct nline *nline = malloc(sizeof(struct nline));
nline->type = TEXT;
nline->align = align;
nline->text = malloc(sizeof(struct string));
nline->text->data = full_line;
nline->text->len = full_line_len;
......@@ -348,6 +371,7 @@ size_t flow_nlines(struct nline **from, size_t n_from, struct nline ***to, int w
}
struct nline *nline = malloc(sizeof(struct nline));
nline->type = TEXT;
nline->align = from[i]->align;
nline->text = s1;
if (n_to >= nmemb) {
nmemb += linear_step;
......@@ -372,6 +396,7 @@ size_t flow_nlines(struct nline **from, size_t n_from, struct nline ***to, int w
}
struct nline *nline = malloc(sizeof(struct nline));
nline->type = TEXT;
nline->align = from[i]->align;
nline->text = s;
if (n_to >= nmemb) {
nmemb += linear_step;
......@@ -382,6 +407,7 @@ size_t flow_nlines(struct nline **from, size_t n_from, struct nline ***to, int w
} else if (from[i]->type == ANIM) {
struct nline *nline = malloc(sizeof(struct nline));
nline->type = ANIM;
nline->align = from[i]->align;
nline->anim = malloc(sizeof(struct animation));
nline->anim->current_frame_index = from[i]->anim->current_frame_index;
nline->anim->n_frames = from[i]->anim->n_frames;
......@@ -391,11 +417,13 @@ size_t flow_nlines(struct nline **from, size_t n_from, struct nline ***to, int w
for (size_t i_frame = 0; i_frame < nline->anim->n_frames; i_frame++) {
nline->anim->frames[i_frame].delay = from[i]->anim->frames[i_frame].delay;
nline->anim->frames[i_frame].s = malloc(sizeof(struct string));
nline->anim->frames[i_frame].s->len = from[i]->anim->frames[i_frame].s->len;
size_t tmp_len = from[i]->anim->frames[i_frame].s->len > width ? width : from[i]->anim->frames[i_frame].s->len;
nline->anim->frames[i_frame].s->len = tmp_len;
nline->anim->frames[i_frame].s->data =
strncpy(malloc(nline->anim->frames[i_frame].s->len*sizeof(char)+1),
strncpy(malloc(tmp_len*sizeof(char)+1),
from[i]->anim->frames[i_frame].s->data,
from[i]->anim->frames[i_frame].s->len+1);
tmp_len);
nline->anim->frames[i_frame].s->data[tmp_len] = '\0';
}
if (n_to >= nmemb) {
nmemb += linear_step;
......
......@@ -20,6 +20,11 @@
#define NO_TRAILING_NEWLINE 0x1
#define ALIGN_HINT ";align"
#define ALIGN_LEFT_HINT "left"
#define ALIGN_RIGHT_HINT "right"
#define ALIGN_CENTER_HINT "center"
#include <stdio.h>
#include <dirent.h>
#include <curses.h>
......@@ -30,10 +35,7 @@ enum content_type { STATIC, DYNAMIC };
enum mode { COMMAND, INSERT };
struct line {
char *line;
struct line *prev, *next;
};
enum align { LEFT, CENTER, RIGHT };
struct section {
char *title;
......@@ -62,6 +64,7 @@ struct animation {
struct nline {
enum { ANIM, TEXT } type;
enum align align;
union {
struct string *text;
struct animation *anim;
......@@ -125,11 +128,8 @@ size_t gen_index(struct section **sections, size_t nmemb, struct nline ***to, si
// ======= old stuff:
void print_lines(struct line *lines);
void dump_sections(struct section **sections, size_t n_sections);
struct section **read_sections(DIR *dir, char *dirname, 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
......@@ -37,7 +37,7 @@ void render_ncontent(struct window *window) {
size_t cursor_y = 0,
i = window->scroll;
while (i < window->content.lines->n_formatted && cursor_y < window->rows) {
render_nline(window->window, cursor_y, window->content.lines->formatted[i]);
render_nline(window, cursor_y, window->content.lines->formatted[i]);
if (window->content.lines->formatted[i]->type == ANIM &&
window->content.lines->formatted[i]->anim->is_first_line) {
push_anim_ref_back(window, i);
......@@ -48,12 +48,24 @@ void render_ncontent(struct window *window) {
}
}
void render_nline(WINDOW *window, size_t cursor_y, struct nline *nline) {
scrollok(window, 0);
void render_nline(struct window *window, size_t cursor_y, struct nline *nline) {
struct string *s = NULL;
if (nline->type == TEXT) {
mvwprintw(window, cursor_y, 0, "%s", nline->text->data);
s = nline->text;
} else if (nline->type == ANIM) {
mvwprintw(window, cursor_y, 0, nline->anim->frames[nline->anim->current_frame_index].s->data);
s = nline->anim->frames[nline->anim->current_frame_index].s;
}
if (s != NULL) {
size_t cursor_x = 0; /* assume left-align by default */
if (nline->align == RIGHT) {
cursor_x = window->cols - s->len;
fprintf(stderr, "renderer detected right-align, cursor_x = %d\n", cursor_x);
} else if (nline->align == CENTER) {
cursor_x = (window->cols - s->len)/2;
fprintf(stderr, "renderer detected center-align, cursor_x = %d\n", cursor_x);
}
scrollok(window->window, 0);
mvwprintw(window->window, cursor_y, cursor_x, "%s", s->data);
scrollok(window->window, 1);
}
scrollok(window, 1);
}
......@@ -26,6 +26,6 @@
void render_separator(struct window *window);
void render_ncontent(struct window *window);
void render_nline(WINDOW *window, size_t cursor_y, struct nline *nline);
void render_nline(struct window *window, size_t cursor_y, struct nline *nline);
#endif
......@@ -22,6 +22,7 @@
#include "site.h"
#include "render.h"
#include "data.h"
#include <string.h>
#include <ncurses.h>
......@@ -57,7 +58,7 @@ void scroll_ncontent(struct window *window, int dy) {
if (dy == 1 && window->scroll+window->rows < window->content.lines->n_formatted) {
wscrl(window->window, 1);
window->scroll++;
render_nline(window->window, window->rows-1, window->content.lines->formatted[window->scroll+window->rows-1]);
render_nline(window, window->rows-1, window->content.lines->formatted[window->scroll+window->rows-1]);
if (window->content.lines->formatted[window->scroll-1]->type == ANIM &&
(window->content.lines->formatted[window->scroll]->type != ANIM ||
window->content.lines->formatted[window->scroll]->anim->is_first_line)) {
......@@ -70,7 +71,7 @@ void scroll_ncontent(struct window *window, int dy) {
} else if (dy == -1 && window->scroll > 0) {
wscrl(window->window, -1);
window->scroll--;
render_nline(window->window, 0, window->content.lines->formatted[window->scroll]);
render_nline(window, 0, window->content.lines->formatted[window->scroll]);
// was there an animation that went out of focus?
if (window->content.lines->formatted[window->scroll+window->rows-1]->type == ANIM &&
window->content.lines->formatted[window->scroll+window->rows-1]->anim->is_first_line) {
......
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