~nloomans/ft_select

ref: 1d90313037b5c7d0545be79e58d8f50a102501ff ft_select/src/state.c -rw-r--r-- 1.6 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <libft.h>
#include <ft_printf.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "error.h"
#include "state.h"

struct s_state_option	*unpack_option(struct s_list2_conn *conn)
{
	if (conn == NULL)
		return (NULL);
	return (struct s_state_option *)(
		(char *)conn - offsetof(struct s_state_option, conn));
}

t_error					state_make_options(
							t_list2_meta *dest, int argc, char **argv)
{
	int						i;
	t_list2_conn			*prev;
	struct s_state_option	*new_option;

	if (argc <= 1)
		return (errorf("no options to select"));
	i = 1;
	prev = NULL;
	while (i < argc)
	{
		new_option = ft_memalloc(sizeof(*new_option));
		if (new_option == NULL)
			return (errorf("failed to allocate option"));
		new_option->name = argv[i];
		new_option->selected = false;
		list2_insert(dest, prev, &new_option->conn);
		prev = &new_option->conn;
		i++;
	}
	return (ERROR_NULL);
}

void	state_debug(const struct s_state state)
{
	struct s_state_option	*option;

	ft_dprintf(STDERR_FILENO,
		"struct s_state {\n"
		"  .options = {\n");
	option = unpack_option(state.options.first);
	while (option != NULL)
	{
		ft_dprintf(STDERR_FILENO,
			"    %p (%+d) {\"%s\", .selected = %s},%s\n",
			option, (char *)&option->conn - (char *)option,
			option->name,
			option->selected
				? "true" : "false",
			state.cursor == &option->conn ? " // focused" : "");
		option = unpack_option(option->conn.next);
	}
	ft_dprintf(STDERR_FILENO,
		"  },\n"
		"  .cursor = %p, // %s\n"
		"  .terminal = {\n"
		"    .rows = %u,\n"
		"    .columns = %u,\n"
		"  },\n"
		"}\n",
		state.cursor, unpack_option(state.cursor)->name,
		state.terminal.rows,
		state.terminal.columns);
}