~nloomans/ft_select

ref: a4ef762331941c5e33670f7bf81eb964fb761edb ft_select/src/main.c -rw-r--r-- 2.0 KiB
a4ef7623Noah Loomans fix build on linux 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
54
55
56
57
58
59
60
61
62
63
64
65
/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   ft_select                                          :+:    :+:            */
/*                                                     +:+                    */
/*   Author: Noah Loomans                             +#+                     */
/*           <nloomans@student.codam.nl>             +#+                      */
/*                                                  #+#    #+#                */
/*   License: GPLv3                                ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

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