~nloomans/ft_select

ref: 1d90313037b5c7d0545be79e58d8f50a102501ff ft_select/src/main.c -rw-r--r-- 1.2 KiB
1d903130Noah Loomans handle CTRL+Z and fg 2 years ago
                                                                                
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
#include <stdlib.h>
#include <libft.h>
#include <ft_printf.h>
#include "terminal.h"
#include "event.h"
#include "action.h"
#include "state.h"
#include "render.h"
#include "error.h"

static t_error	loop(struct s_state *state)
{
	t_error		error;
	t_action	action;

	error = event_next(&action);
	if (is_error(error))
		return (errorf("failed to get next event: %s", error.msg));
	if (action != NULL)
	{
		(*action)(state);
		render(*state);
	}
	return (ERROR_NULL);
}

int				main(int argc, char **argv)
{
	t_error			error;
	struct s_state	state;

	ft_memset(&state, '\0', sizeof(state));
	error = event_init();
	if (is_error(error))
	{
		return (ft_eprintf(1, "ft_select: failed to initialize events: %s\n",
					error.msg));
	}
	action_update_size(&state);
	error = state_make_options(&state.options, argc, argv);
	if (is_error(error))
	{
		return (ft_eprintf(1, "ft_select: failed to initialize options: %s\n",
					error.msg));
	}
	state.cursor = state.options.first;
	terminal_configure(TERMINAL_CONFIGURE_SETUP);
	render(state);
	while (!is_error(error))
		error = loop(&state);
	terminal_configure(TERMINAL_CONFIGURE_RESTORE);
	return (ft_eprintf(1, "ft_select: %s\n", error.msg));
}