From 9bffcafa9ae0c38103a8c64b3482bb099e792869 Mon Sep 17 00:00:00 2001 From: verdant Date: Tue, 28 Jul 2026 18:30:16 +0800 Subject: Move source files to src/ --- src/main.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/main.c (limited to 'src/main.c') 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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; +} -- cgit v1.2.3