~nloomans/ft_select

9783305a370e42cdcc43c542a2172cc8b95e8b9b — Noah Loomans 1 year, 10 months ago 97f6c57
confirm selection by pressing return

Reference:

When the user validates the selection with the return key, the list of
choices will be sent back to shell. The latter (i.e. list of choices
sent back) will be separated by a space. This will allow you to use your
program ft_select inside a shell script (to create a “set” for example).
4 files changed, 31 insertions(+), 0 deletions(-)

M inc/action.h
M meson.build
A src/action/confirm.c
M src/event.c
M inc/action.h => inc/action.h +1 -0
@@ 5,6 5,7 @@

typedef void	(*t_action)(struct s_state *state);

void			action_confirm(struct s_state *state);
void			action_delete(struct s_state *state);
void			action_noop(struct s_state *state);
void			action_quit(struct s_state *state);

M meson.build => meson.build +1 -0
@@ 24,6 24,7 @@ endif
inc_dir = include_directories('inc')
src_files = files([
  'src/main.c',
  'src/action/confirm.c',
  'src/action/delete.c',
  'src/action/move.c',
  'src/action/noop.c',

A src/action/confirm.c => src/action/confirm.c +28 -0
@@ 0,0 1,28 @@
#include <stdlib.h>
#include <ft_printf.h>
#include "terminal.h"
#include "state.h"
#include "action.h"

void			action_confirm(struct s_state *state)
{
	t_list2_item			*curr_item;
	struct s_state_option	*curr_option;
	bool					is_first;

	terminal_configure(TERMINAL_CONFIGURE_RESTORE);
	curr_item = state->options.first;
	is_first = true;
	while (curr_item != NULL)
	{
		curr_option = (struct s_state_option *)curr_item->content;
		if (curr_option->selected)
		{
			ft_printf("%s%s", is_first ? "" : " ", curr_option->name);
			is_first = false;
		}
		curr_item = curr_item->next;
	}
	ft_printf("\n");
	exit(0);
}

M src/event.c => src/event.c +1 -0
@@ 27,6 27,7 @@ const t_action	g_event_key_map[READ_TYPE_AMOUNT][128] = {
		[ 'l' ] = action_right,
		[ ' ' ] = action_select,
		[ '\x7f' ] = action_delete,
		[ '\n' ] = action_confirm,
	},
	[ READ_TYPE_ESC ] = {
		[ 'A' ] = action_up,