aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
blob: 4a24bcf36f05c78b49874da20d58900183687afe (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
#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 "stat.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;
	file_list *list;
	log_init();

	terminal *term = ncurses_init();

	fm_info *info = fm_init(term);

	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 (info->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) {
				snprintf(next_path, sizeof(next_path), "%s/%s",
					 info->cwd,
					 info->cur->items[info->cursor].name);
				fm_preview_file(info->windows[FM_PANE_RIGHT],
						next_path);
			} else {
				snprintf(next_path, sizeof(next_path), "%s/%s",
					 info->cwd,
					 info->cur->items[info->cursor].name);
				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);
				}
			}

			wrefresh(info->windows[FM_PANE_RIGHT]->win);
			wrefresh(info->windows[FM_PANE_LEFT]->win);
		}

		ch = getch();
		handling_keys(ch, info);
	}

	magic_cleanup();
	endwin();

	return 0;
}