summary refs log tree commit diff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c129
1 files changed, 65 insertions, 64 deletions
diff --git a/main.c b/main.c
index 46d186c..fccd383 100644
--- a/main.c
+++ b/main.c
@@ -121,8 +121,6 @@ int timestamp_invalid(struct timespec *ts) {
 	return 0;
 }
 
-/* formatting */
-
 /* posts */
 
 typedef struct Post {
@@ -149,11 +147,11 @@ int ts_cmp(const struct timespec *a, const struct timespec *b) {
 }
 
 int post_cmp(const void *a, const void *b) {
-	const Post *pa = (const Post *)a;
-	const Post *pb = (const Post *)b;
-	return -ts_cmp(&pa->timestamp, &pb->timestamp);
+	return -ts_cmp(&((const Post *)a)->timestamp, &((const Post *)b)->timestamp);
 }
 
+/* post indexing */
+
 /* @path must be both
  * (a) absolute; and
  * (b) allocated either statically or within arena @a
@@ -210,22 +208,7 @@ void posts_refresh(PostList *posts, Arena *a) {
 	posts_load(posts, a);
 }
 
-/* display */
-
-typedef struct {
-	Post *src;
-	Str text;
-	int lines;
-	struct {
-		unsigned drawn : 1;
-		unsigned has_mention : 1;
-	};
-} GfxPost;
-
-typedef struct {
-	int len;
-	GfxPost *posts;
-} Gfx;
+/* word wrapping */
 
 static inline int ch_space(char c) {
 	return c <= 0x20;
@@ -255,6 +238,23 @@ int str_cat_wrap(Str *out, Str s, int width, Arena *a) {
 	return lines;
 }
 
+/* display */
+
+typedef struct {
+	Post *src;
+	Str text;
+	int lines;
+	struct {
+		unsigned drawn : 1;
+		unsigned has_mention : 1;
+	};
+} GfxPost;
+
+typedef struct {
+	int len;
+	GfxPost *posts;
+} Gfx;
+
 int gfx_post_width(void) {
 	return getmaxx(stdscr) - GFX_MARGIN_X * 2 - 2;
 }
@@ -343,30 +343,23 @@ void gfx_draw(Gfx *gfx, int cur) {
 	}
 }
 
-/* main */
-
-void init_curses(void) {
-	setlocale(LC_ALL, "");
-	initscr();
-	start_color();
-	init_color(COLOR_BLACK, 0, 0, 0);
-	cbreak();
-	noecho();
-	intrflush(stdscr, FALSE);
-	keypad(stdscr, TRUE);
-	curs_set(0);
-
-	init_pair(CPAIR_BORDER, COLOR_BLUE, COLOR_BLACK);
-	init_pair(CPAIR_USER, COLOR_YELLOW, COLOR_BLACK);
-	init_pair(CPAIR_TIME, COLOR_BLUE, COLOR_BLACK);
-	init_pair(CPAIR_TEXT, COLOR_WHITE, COLOR_BLACK);
-	init_pair(CPAIR_MENTION, COLOR_BLACK, COLOR_WHITE);
+/* will skip at least one post, so not suitable for just scrolling one line at
+ * a time like bink.py does */
+int gfx_line_skip(Gfx *gfx, int cur, int amt) {
+	int sign = amt < 0 ? -1 : 1;
+	amt = abs(amt);
+	while (amt > 0 && (sign < 0 ? cur > 0 : cur + 1 < gfx->len)) {
+		amt -= gfx_post_height(&gfx->posts[cur]) + 1;
+		cur += sign;
+	}
+	if (amt < 0) {
+		if (sign < 0 && cur + 1 < gfx->len) cur++;
+		else if (sign > 0 && cur > 0) cur--;
+	}
+	return cur;
 }
 
-void fini_curses(void) {
-	curs_set(1);
-	endwin();
-}
+/* post creation & editing */
 
 char *get_editor(void) {
 	char *editor = getenv("EDITOR");
@@ -374,7 +367,7 @@ char *get_editor(void) {
 	return editor;
 }
 
-int my_post(Post *post) {
+int is_post_mine(Post *post) {
 	return str_eql(post->user, str_from_cstr(getlogin()));
 }
 
@@ -432,23 +425,31 @@ void new_post(Arena *temp) {
 	fclose(f);
 }
 
-/* will skip at least one post, so not suitable for just scrolling one line at
- * a time like bink.py does */
-int line_skip(int cur, Gfx *gfx, int amt) {
-	int sign = amt < 0 ? -1 : 1;
-	amt = abs(amt);
-	while (amt > 0 && (sign < 0 ? cur > 0 : cur + 1 < gfx->len)) {
-		amt -= gfx_post_height(&gfx->posts[cur]) + 1;
-		cur += sign;
-	}
-	if (amt < 0) {
-		if (sign < 0 && cur + 1 < gfx->len) cur++;
-		else if (sign > 0 && cur > 0) cur--;
-	}
-	return cur;
+/* main */
+
+void init_curses(void) {
+	setlocale(LC_ALL, "");
+	initscr();
+	start_color();
+	init_color(COLOR_BLACK, 0, 0, 0);
+	cbreak();
+	noecho();
+	intrflush(stdscr, FALSE);
+	keypad(stdscr, TRUE);
+	curs_set(0);
+
+	init_pair(CPAIR_BORDER, COLOR_BLUE, COLOR_BLACK);
+	init_pair(CPAIR_USER, COLOR_YELLOW, COLOR_BLACK);
+	init_pair(CPAIR_TIME, COLOR_BLUE, COLOR_BLACK);
+	init_pair(CPAIR_TEXT, COLOR_WHITE, COLOR_BLACK);
+	init_pair(CPAIR_MENTION, COLOR_BLACK, COLOR_WHITE);
+}
+
+void fini_curses(void) {
+	curs_set(1);
+	endwin();
 }
 
-/* might crash on zero posts? */
 int main(void) {
 	/* init */
 
@@ -502,22 +503,22 @@ int main(void) {
 			break;
 		case ' ':
 		case KEY_NPAGE:
-			cur = line_skip(cur, &gfx, PAGE_LEN);
+			cur = gfx_line_skip(&gfx, cur, PAGE_LEN);
 			break;
 		case 'd':
 		case 0x04 /* ^D */:
-			cur = line_skip(cur, &gfx, HALF_PAGE_LEN);
+			cur = gfx_line_skip(&gfx, cur, HALF_PAGE_LEN);
 			break;
 		case 'b':
 		case KEY_PPAGE:
-			cur = line_skip(cur, &gfx, -PAGE_LEN);
+			cur = gfx_line_skip(&gfx, cur, -PAGE_LEN);
 			break;
 		case 'u':
 		case 0x15 /* ^U */:
-			cur = line_skip(cur, &gfx, -HALF_PAGE_LEN);
+			cur = gfx_line_skip(&gfx, cur, -HALF_PAGE_LEN);
 			break;
 		case 'e':
-			if (my_post(&posts.data[cur])) {
+			if (is_post_mine(&posts.data[cur])) {
 				fini_curses();
 				edit_post(&posts.data[cur], &temp_arena);
 				init_curses();