aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 91cfe3f99ca13d17bb49f985390e152b9a56d69e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#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];
	refresh();
	while (1) {
		if (need_redraw) {

			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");
				}

				fm_preview_dir(info, next_path);
			}

			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;
}