commit cd8f903859eb59bf54d8de14d0436050a09eb6ff
Author: Raniconduh <clagv.randomgames@gmail.com>
Date: Sun Jan 29 20:30:32 2023 +0000
diff --git a/Makefile b/Makefile
index 6394d67..ba3a9c7 100644
--- a/Makefile
+++ b/Makefile
@@ -47 +47 @@ include config.mk
BIN = cscroll
SRC = src/commands.c src/dir.c src/hash.c src/io.c src/main.c \
- src/opts.c src/type.c src/var.c
+ src/opts.c src/type.c src/var.c src/info.c
OBJ = ${SRC:.c=.o}
CC ?= cc
diff --git a/include/info.h b/include/info.h
new file mode 100644
index 0000000..64b8114
--- /dev/null
+++ b/include/info.h
@@ -00 +116 @@
+#ifndef _INFO_H
+#define _INFO_H
+
+enum info_t {
+ INFO_INFO,
+ INFO_WARN,
+ INFO_ERR,
+};
+
+
+void info_init(void);
+void display_info(enum info_t, char *);
+void refresh_info(void);
+void page_info(void);
+
+#endif /* _INFO_H */
diff --git a/include/io.h b/include/io.h
index 3b42ca4..345575d 100644
--- a/include/io.h
+++ b/include/io.h
@@ -8018 +806 @@ enum keys {
CTRL_Z = 26,
};
-enum info_t {
- INFO_INFO,
- INFO_WARN,
- INFO_ERR,
-};
-
-
-struct info_node {
- enum info_t type;
- char * msg;
-};
-
void curses_init(void);
void terminate_curses(void);
@@ -11111 +996 @@ char get_file_ident(struct dir_entry_t *);
size_t get_ilen(long, int);
char * get_oname(uid_t);
char * get_gname(gid_t);
-void info_init(void);
-void display_info(enum info_t, char *);
-void refresh_info(void);
-int get_info_color(struct info_node *);
-void page_info(void);
extern bool print_path;
diff --git a/src/dir.c b/src/dir.c
index 4c2b19b..f35813a 100644
--- a/src/dir.c
+++ b/src/dir.c
@@ -106 +107 @@
#include <pwd.h>
#include <grp.h>
+#include "info.h"
#include "type.h"
#include "opts.h"
#include "dir.h"
diff --git a/src/info.c b/src/info.c
new file mode 100644
index 0000000..cfd958d
--- /dev/null
+++ b/src/info.c
@@ -00 +1132 @@
+#include <ncurses.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "info.h"
+#include "io.h"
+
+
+struct info_node {
+ enum info_t type;
+ char * msg;
+};
+
+
+static struct {
+ WINDOW * w;
+ struct info_node ** i;
+ size_t n;
+} info_buffer = {NULL, NULL, 0};
+
+
+static int get_info_color(struct info_node * n) {
+ switch (n->type) {
+ default:
+ case INFO_INFO: return COLOR_PAIR(WHITE);
+ case INFO_WARN: return COLOR_PAIR(YELLOW) | A_REVERSE;
+ case INFO_ERR: return COLOR_PAIR(RED) | A_REVERSE;
+ }
+}
+
+
+void info_init(void) {
+ info_buffer.w = newwin(1, COLS, LINES - 1, 0);
+ wclear(info_buffer.w);
+ refresh();
+}
+
+
+void display_info(enum info_t type, char * msg) {
+ if (!info_buffer.i) info_buffer.i = malloc(sizeof(struct info_node*) * 32);
+
+ // realloc every 32
+ if ((info_buffer.n + 1) % 32 == 0) {
+ info_buffer.i = realloc(info_buffer.i,
+ (info_buffer.n + 32) * sizeof(struct info_node*));
+ }
+
+ size_t n = info_buffer.n;
+ info_buffer.i[n] = malloc(sizeof(struct info_node));
+ info_buffer.i[n]->type = type;
+
+ info_buffer.i[n]->msg = malloc(strlen(msg) + 1);
+ strcpy(info_buffer.i[n]->msg, msg);
+
+ info_buffer.n++;
+
+ refresh_info();
+}
+
+
+void refresh_info(void) {
+ if (info_buffer.n == 0 || !info_buffer.w) return;
+
+ size_t n = info_buffer.n - 1;
+
+
+ int cp = get_info_color(info_buffer.i[n]);
+ werase(info_buffer.w);
+ waddstr(info_buffer.w, "(:i) ");
+ wattron(info_buffer.w, cp);
+ waddstr(info_buffer.w, info_buffer.i[n]->msg);
+ wattroff(info_buffer.w, cp);
+ wrefresh(info_buffer.w);
+}
+
+
+void page_info(void) {
+ if (info_buffer.n == 0 || !info_buffer.w) return;
+
+ clear();
+
+ size_t first_info = 0;
+ size_t last_info = info_buffer.n - 1;
+ if (last_info >= (unsigned)LINES - 1) last_info = LINES - 1;
+
+ bool done = false;
+
+ while (!done) {
+ erase();
+
+ for (size_t i = first_info; i <= last_info; i++) {
+ int cp = get_info_color(info_buffer.i[i]);
+ attron(cp);
+ addstr(info_buffer.i[i]->msg);
+ attroff(cp);
+ addch('\n');
+ }
+
+ printw("LINES %lu-%lu/%lu, q to close\n",
+ first_info + 1, last_info + 1, info_buffer.n);
+
+ refresh();
+
+ int c = getch();
+ switch (c) {
+ case UP_KEYS:
+ if (first_info > 0) {
+ first_info--;
+ last_info--;
+ }
+ break;
+ case DOWN_KEYS:
+ if (last_info < info_buffer.n - 1) {
+ first_info++;
+ last_info++;
+ }
+ break;
+ case KEY_RESIZE:
+ // jump back to the top, it is fine.
+ first_info = 0;
+ last_info = info_buffer.n - 1;
+ if (last_info >= (unsigned)LINES - 1) last_info = LINES - 1;
+ // make sure the file buffer is resized too
+ resize_fbuf();
+ break;
+ case CTRL_C:
+ case 'q':
+ done = true;
+ break;
+ }
+ }
+}
diff --git a/src/io.c b/src/io.c
index 539a140..50ab524 100644
--- a/src/io.c
+++ b/src/io.c
@@ -156 +157 @@
#if ICONS
#include "type.h"
#endif
+#include "info.h"
#include "main.h"
#include "opts.h"
#include "dir.h"
@@ -2613 +276 @@ int stdout_back = 0;
size_t n_marked_files = false;
-static struct {
- WINDOW * w;
- struct info_node ** i;
- size_t n;
-} info_buffer = {NULL, NULL, 0};
-
-
static int default_colors[] = {
[COLOR_DIR] = COLOR_BLUE,
[COLOR_LINK] = COLOR_CYAN,
@@ -784116 +7783 @@ char * get_gname(gid_t gid) {
}
return buf;
}
-
-
-void info_init(void) {
- info_buffer.w = newwin(1, COLS, LINES - 1, 0);
- wclear(info_buffer.w);
- refresh();
-}
-
-
-void display_info(enum info_t type, char * msg) {
- if (!info_buffer.i) info_buffer.i = malloc(sizeof(struct info_node*) * 32);
-
- // realloc every 32
- if ((info_buffer.n + 1) % 32 == 0) {
- info_buffer.i = realloc(info_buffer.i,
- (info_buffer.n + 32) * sizeof(struct info_node*));
- }
-
- size_t n = info_buffer.n;
- info_buffer.i[n] = malloc(sizeof(struct info_node));
- info_buffer.i[n]->type = type;
-
- info_buffer.i[n]->msg = malloc(strlen(msg) + 1);
- strcpy(info_buffer.i[n]->msg, msg);
-
- info_buffer.n++;
-
- refresh_info();
-}
-
-
-int get_info_color(struct info_node * n) {
- switch (n->type) {
- default:
- case INFO_INFO: return COLOR_PAIR(WHITE);
- case INFO_WARN: return COLOR_PAIR(YELLOW) | A_REVERSE;
- case INFO_ERR: return COLOR_PAIR(RED) | A_REVERSE;
- }
-}
-
-
-void refresh_info(void) {
- if (info_buffer.n == 0 || !info_buffer.w) return;
-
- size_t n = info_buffer.n - 1;
-
-
- int cp = get_info_color(info_buffer.i[n]);
- werase(info_buffer.w);
- waddstr(info_buffer.w, "(:i) ");
- wattron(info_buffer.w, cp);
- waddstr(info_buffer.w, info_buffer.i[n]->msg);
- wattroff(info_buffer.w, cp);
- wrefresh(info_buffer.w);
-}
-
-
-void page_info(void) {
- if (info_buffer.n == 0 || !info_buffer.w) return;
-
- clear();
-
- size_t first_info = 0;
- size_t last_info = info_buffer.n - 1;
- if (last_info >= (unsigned)LINES - 1) last_info = LINES - 1;
-
- bool done = false;
-
- while (!done) {
- erase();
-
- for (size_t i = first_info; i <= last_info; i++) {
- int cp = get_info_color(info_buffer.i[i]);
- attron(cp);
- addstr(info_buffer.i[i]->msg);
- attroff(cp);
- addch('\n');
- }
-
- printw("LINES %lu-%lu/%lu, q to close\n",
- first_info + 1, last_info + 1, info_buffer.n);
-
- refresh();
-
- int c = getch();
- switch (c) {
- case UP_KEYS:
- if (first_info > 0) {
- first_info--;
- last_info--;
- }
- break;
- case DOWN_KEYS:
- if (last_info < info_buffer.n - 1) {
- first_info++;
- last_info++;
- }
- break;
- case KEY_RESIZE:
- // jump back to the top, it is fine.
- first_info = 0;
- last_info = info_buffer.n - 1;
- if (last_info >= (unsigned)LINES - 1) last_info = LINES - 1;
- // make sure the file buffer is resized too
- resize_fbuf();
- break;
- case CTRL_C:
- case 'q':
- done = true;
- break;
- }
- }
-}
diff --git a/src/main.c b/src/main.c
index 853803d..4441320 100644
--- a/src/main.c
+++ b/src/main.c
@@ -86 +87 @@
#include <signal.h>
#include "commands.h"
+#include "info.h"
#include "main.h"
#include "opts.h"
#include "dir.h"
diff --git a/src/opts.c b/src/opts.c
index 16e17ca..59bf77e 100644
--- a/src/opts.c
+++ b/src/opts.c
@@ -66 +67 @@
#include <stdio.h>
#include <ctype.h>
+#include "info.h"
#include "opts.h"
#include "var.h"
#include "io.h"