aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
blob: 550cb40e46606b25ef484bab8deda058b61f5731 (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
115
116
117
#include <ctype.h>
#include <dirent.h>
#include <limits.h>
#include <linux/limits.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 "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 == NULL)
		return;

	size_t len = strlen(path);
	while (len > 1 && path[len - 1] == '/') {
		path[len - 1] = '\0';
		len--;
	}
}

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 *t = ncurses_init();
	stat *s = stat_init();
	stat_get_sysver(s);
	stat_get_diskinfo(s);
	stat_get_meminfo(s);
	log_write(LOG_INFO, "Terminal maxx = %d, maxy = %d", t->maxy, t->maxx);

	draw_stat_bar(t, s->sysver);

	/* int stat_w_h = t->maxy / 8 < 4 ? 4 : t->maxy / 8; */
	int stat_w_h = 4;

	window *stat_w = wm_create_new_w(t, "stat", stat_w_h, t->maxx, 0, 0);

	fm_info *f = fm_init(t);

	if (argc == 2) {
		snprintf(f->cwd, sizeof(f->cwd), "%s", argv[1]);
		log_write(LOG_INFO, "f->cwd = %s", f->cwd);
		log_write(LOG_INFO, "argv[1] = %s", argv[1]);
		path_normalize(f->cwd);
	} else {
		fm_getcwd(f);
	}

	log_write(LOG_INFO, "cwd = %s", f->cwd);
	fm_getdir(f, f->cur, f->cwd);

	timeout(100);
	int i;
	int sw_y, sw_x;
	while (1) {
		ch = getch();
		handling_keys(ch, t, f);
		stat_get_meminfo(s);
		stat_get_diskinfo(s);
		stat_get_cpuinfo(s);
		getmaxyx(stat_w->win, sw_y, sw_x);
		wclear(stat_w->win);

		wborder(stat_w->win, ' ', ' ', ' ', '-', ' ', ' ', '-', '-');

		stat_draw_cpu_bar(stat_w->win, sw_x, 0, 0, s);
		stat_print_cpu_info(stat_w->win, 0, sw_x, s);
		stat_draw_bar(stat_w->win, "Mem", sw_x, 1, 0,
			      s->meminfo.mem_total, s->meminfo.mem_available);
		stat_print_info(stat_w->win, 1, sw_x, "Mem",
				s->meminfo.mem_total, s->meminfo.mem_available);

		stat_draw_bar(stat_w->win, "Disk", sw_x, 2, 0,
			      s->diskinfo.disk_total,
			      s->diskinfo.disk_available);
		stat_print_info(stat_w->win, 2, sw_x, "Disk",
				s->diskinfo.disk_total,
				s->diskinfo.disk_available);

		if (f->needs_redraw) {
			wclear(f->fm_l->win);
			i = 0;
			for (int j = f->top; j < f->bottom; j++) {
				fm_draw_entries(f, j, i);
				i++;
			}

			f->needs_redraw = 0;
		}

		fm_draw_right_window(f);
		wm_refresh_all(t);
	}

	return 0;
}