aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
authorverdant <im@verdant.ee>2026-07-28 18:30:16 +0800
committerverdant <im@verdant.ee>2026-07-28 18:30:16 +0800
commit9bffcafa9ae0c38103a8c64b3482bb099e792869 (patch)
tree3cc18cabd99f9e300ca2539a9a48fb9c2d30e337 /src/main.c
parentcbdc8462b6e982d3137b74d61bed6f8977405b8a (diff)
downloadsf-9bffcafa9ae0c38103a8c64b3482bb099e792869.tar.gz
sf-9bffcafa9ae0c38103a8c64b3482bb099e792869.zip
Move source files to src/
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c121
1 files changed, 121 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..ba19c05
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,121 @@
+#include <ctype.h>
+#include <dirent.h>
+#include <limits.h>
+#include <linux/limits.h>
+#include <magic.h>
+#include <ncurses.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/statvfs.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "dir.h"
+#include "display.h"
+#include "err.h"
+#include "fm.h"
+#include "keys.h"
+#include "log.h"
+#include "mem.h"
+#include "version.h"
+#include "wm.h"
+
+static void
+path_normalize(char *path)
+{
+ if (!path)
+ return;
+
+ char new_path[PATH_MAX];
+ if (realpath(path, new_path)) {
+ path = new_path;
+ } else {
+ return;
+ }
+
+ return;
+}
+
+int
+main(int argc, char **argv)
+{
+ if (argc > 1 &&
+ (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-v") == 0))
+ version();
+
+ int ch;
+ log_init();
+
+ terminal *term = ncurses_init();
+
+ fm_info *info = fm_init(term);
+ int stat_bar_win_index = wm_fd_w_by_name(term, "status_bar");
+ if (stat_bar_win_index == -1) {
+ log_write(LOG_WARN, "Cannot find window: status_bar");
+ }
+ need_redraw = true;
+ if (argc == 2) {
+ snprintf(info->cwd, sizeof(info->cwd), "%s", argv[1]);
+ path_normalize(info->cwd);
+ } else {
+ fm_getcwd(info);
+ }
+
+ info->cur = fm_getdir(info->cur, info->cwd);
+ magic_init();
+ char next_path[PATH_MAX];
+ timeout(100);
+ while (1) {
+ if (!need_redraw)
+ continue;
+
+ fm_draw_entries(info->windows[FM_PANE_LEFT], info->cur,
+ info->cursor, info->top, info->bottom);
+
+ if (!info->cur->items[info->cursor].is_dir) {
+ size_t len = snprintf(
+ next_path, sizeof(next_path), "%s/%s", info->cwd,
+ info->cur->items[info->cursor].name);
+ if (len >= sizeof(next_path)) {
+ die("Length of items error");
+ }
+ fm_preview_file(info->windows[FM_PANE_RIGHT],
+ next_path);
+ } else {
+ size_t len = snprintf(
+ next_path, sizeof(next_path), "%s/%s", info->cwd,
+ info->cur->items[info->cursor].name);
+
+ if (len >= sizeof(next_path)) {
+ die("Length of items error");
+ }
+ file_list *new_list = info->next;
+ new_list = fm_getdir(new_list, next_path);
+ if (new_list) {
+ info->next = new_list;
+ int right_height =
+ info->windows[FM_PANE_RIGHT]->height;
+ fm_draw_entries(info->windows[FM_PANE_RIGHT],
+ info->next, -1, 0,
+ right_height);
+ }
+ }
+
+ draw_status_bar(term->windows[stat_bar_win_index], info->cwd,
+ info->cur->items[info->cursor].permission);
+
+ wrefresh(info->windows[FM_PANE_RIGHT]->win);
+ wrefresh(info->windows[FM_PANE_LEFT]->win);
+ wrefresh(term->windows[stat_bar_win_index]->win);
+
+ ch = getch();
+ handling_keys(ch, info);
+ }
+
+ magic_cleanup();
+ endwin();
+
+ return 0;
+}