~nloomans/ft_select

0c2485d73088ebbcd0133973f1b818f72df5a8f6 — Noah Loomans 1 year, 8 months ago 0364e83
fix state norm
3 files changed, 40 insertions(+), 34 deletions(-)

M src/action/confirm.c
M src/render.debug.c
M src/state.c
M src/action/confirm.c => src/action/confirm.c +5 -1
@@ 10,6 10,7 @@
/*                                                                            */
/* ************************************************************************** */

#include <unistd.h>
#include <stdlib.h>
#include <ft_printf.h>
#include "terminal.h"


@@ 35,6 36,9 @@ void			action_confirm(struct s_state *state)
		}
		conn = conn->next;
	}
	ft_printf("\n");
	if (!is_first)
		ft_printf("\n");
	else
		ft_dprintf(STDERR_FILENO, "ft_select: no option selected\n");
	exit(0);
}

M src/render.debug.c => src/render.debug.c +35 -1
@@ 10,12 10,46 @@
/*                                                                            */
/* ************************************************************************** */

#include <unistd.h>
#include <ft_printf.h>
#include "state.h"
#include "terminal.h"
#include "render.h"

static void	render_option(struct s_state_option *option, bool focused)
{
	ft_dprintf(STDERR_FILENO,
		"    %p (%+d) {\"%s\", .selected = %s},%s\n",
		option, (char *)&option->conn - (char *)option,
		option->name,
		option->selected
			? "true" : "false",
		focused ? " // focused" : "");
}

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

	terminal_send(TERMINAL_CLEAR_SCREEN);
	state_debug(state);
	ft_dprintf(STDERR_FILENO,
		"struct s_state {\n"
		"  .options = {\n");
	option = unpack_option(state.options.first);
	while (option != NULL)
	{
		render_option(option, state.cursor == &option->conn);
		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);
}

M src/state.c => src/state.c +0 -32
@@ 50,35 50,3 @@ t_error					state_make_options(
	}
	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);
}