~nloomans/ft_select

58e0f76775f4305cf0859829a92396ea09aad749 — Noah Loomans 1 year, 8 months ago 204227a
fix render norm
6 files changed, 136 insertions(+), 79 deletions(-)

A inc/box.h
M inc/event.h
M inc/handle.h
M meson.build
A src/box.c
M src/render.c
A inc/box.h => inc/box.h +23 -0
@@ 0,0 1,23 @@
/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   ft_select                                          :+:    :+:            */
/*                                                     +:+                    */
/*   Author: Noah Loomans                             +#+                     */
/*           <nloomans@student.codam.nl>             +#+                      */
/*                                                  #+#    #+#                */
/*   License: GPLv3                                ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#ifndef BOX_H
# define BOX_H

# include <stdbool.h>
# include "state.h"

void	box_top(const struct s_state_terminal terminal);
void	box_bottom(const struct s_state_terminal terminal);
void	box_option(char *option, bool selected, bool focus, size_t width);

#endif

M inc/event.h => inc/event.h +0 -1
@@ 17,7 17,6 @@
# include "action.h"
# include "error.h"


t_error				event_init(void);
t_error				event_next(t_action *dest);


M inc/handle.h => inc/handle.h +1 -1
@@ 24,6 24,6 @@ struct									s_handle_signal_map
};

extern const struct s_handle_signal_map	g_handle_signal_map[32];
extern const t_action 					g_handle_key_map[READ_TYPE_AMOUNT][256];
extern const t_action					g_handle_key_map[READ_TYPE_AMOUNT][256];

#endif

M meson.build => meson.build +1 -0
@@ 30,6 30,7 @@ src_files = files([
  'src/action/quit.c',
  'src/action/select.c',
  'src/action/update_size.c',
  'src/box.c',
  'src/derive.c',
  'src/derive_rows.c',
  'src/error.c',

A src/box.c => src/box.c +59 -0
@@ 0,0 1,59 @@
/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   ft_select                                          :+:    :+:            */
/*                                                     +:+                    */
/*   Author: Noah Loomans                             +#+                     */
/*           <nloomans@student.codam.nl>             +#+                      */
/*                                                  #+#    #+#                */
/*   License: GPLv3                                ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include <stdbool.h>
#include <unistd.h>
#include <ft_printf.h>
#include "state.h"
#include "terminal.h"
#include "box.h"

void	box_top(const struct s_state_terminal terminal)
{
	size_t	i;

	ft_dprintf(STDERR_FILENO, "%*s\n", terminal.columns, "");
	ft_dprintf(STDERR_FILENO, "  ┏");
	i = 1;
	while (i < terminal.columns - 5)
	{
		ft_dprintf(STDERR_FILENO, "━");
		i++;
	}
	ft_dprintf(STDERR_FILENO, "┓  \n");
}

void	box_bottom(const struct s_state_terminal terminal)
{
	size_t	i;

	ft_dprintf(STDERR_FILENO, "  ┗");
	i = 1;
	while (i < terminal.columns - 5)
	{
		ft_dprintf(STDERR_FILENO, "━");
		i++;
	}
	ft_dprintf(STDERR_FILENO, "┛  \n");
}

void	box_option(char *option, bool selected, bool focus, size_t width)
{
	ft_dprintf(STDERR_FILENO, "%c%c", focus ? '[' : ' ', selected ? '*' : ' ');
	if (focus)
		terminal_send(TERMINAL_FMT_BEGIN_UNDERLINE);
	if (selected)
		terminal_send(TERMINAL_FMT_BEGIN_REVERSE_VIDEO);
	ft_dprintf(STDERR_FILENO, "%-*s", (int)width, option);
	terminal_send(TERMINAL_FMT_EXIT);
	ft_dprintf(STDERR_FILENO, "%c", focus ? ']' : ' ');
}

M src/render.c => src/render.c +52 -77
@@ 17,111 17,86 @@
#include "state.h"
#include "derive.h"
#include "terminal.h"
#include "box.h"
#include "render.h"

static void	box_top(const struct s_state_terminal terminal)
static void	draw_row(
				struct s_state state,
				struct s_derived_dimensions dimensions,
				size_t *column_width,
				struct s_state_option **row)
{
	size_t	i;
	size_t	column_index;
	size_t	total_column_width;

	ft_dprintf(STDERR_FILENO, "%*s\n", terminal.columns, "");
	ft_dprintf(STDERR_FILENO, "  ┏");
	i = 1;
	while (i < terminal.columns - 5)
	ft_dprintf(STDERR_FILENO, "  ┃ ");
	column_index = 0;
	total_column_width = 0;
	while (column_index < dimensions.columns &&
		row[column_index] != NULL)
	{
		ft_dprintf(STDERR_FILENO, "━");
		i++;
		box_option(
			row[column_index]->name,
			row[column_index]->selected,
			unpack_option(state.cursor) == row[column_index],
			column_width[column_index]);
		total_column_width += column_width[column_index]
			+ DERIVE_OPTION_PADDING_LEFT + DERIVE_OPTION_PADDING_RIGHT;
		column_index++;
	}
	ft_dprintf(STDERR_FILENO, "┓  \n");
	ft_dprintf(STDERR_FILENO, "%*s ┃  \n",
		state.terminal.columns - total_column_width
		- DERIVE_PADDING_LEFT - DERIVE_PADDING_RIGHT, "");
}

static void	box_bottom(const struct s_state_terminal terminal)
static void	draw(
				struct s_state state,
				struct s_derived_dimensions dimensions,
				size_t *column_width,
				struct s_state_option ***rows)
{
	size_t	i;
	size_t						row_index;

	ft_dprintf(STDERR_FILENO, "  ┗");
	i = 1;
	while (i < terminal.columns - 5)
	box_top(state.terminal);
	row_index = 0;
	while (row_index < dimensions.rows)
	{
		ft_dprintf(STDERR_FILENO, "━");
		i++;
		draw_row(state, dimensions, column_width, rows[row_index]);
		row_index++;
	}
	ft_dprintf(STDERR_FILENO, "┛  \n");
}

static void	box_option(char *option, bool selected, bool focus, size_t width)
{
	ft_dprintf(STDERR_FILENO, "%c%c", focus ? '[' : ' ', selected ? '*' : ' ');
	if (focus)
		terminal_send(TERMINAL_FMT_BEGIN_UNDERLINE);
	if (selected)
		terminal_send(TERMINAL_FMT_BEGIN_REVERSE_VIDEO);
	ft_dprintf(STDERR_FILENO, "%-*s", (int)width, option);
	terminal_send(TERMINAL_FMT_EXIT);
	ft_dprintf(STDERR_FILENO, "%c", focus ? ']' : ' ');
	box_bottom(state.terminal);
}

void		render(const struct s_state state)
t_error		render_with_error(const struct s_state state)
{
	struct s_derived_dimensions	dimensions;
	size_t						*column_width;
	struct s_state_option		***rows;
	size_t						column_index;
	size_t						row_index;
	size_t						total_column_width;
	t_error						error;

	terminal_send(TERMINAL_CURSOR_HOME);
	error = derive_dimensions(&dimensions, state.terminal, state.options);
	if (is_error(error))
	{
		terminal_send(TERMINAL_CLEAR_SCREEN);
		ft_dprintf(STDERR_FILENO, "%s", error.msg);
		return ;
	}
		return (error);
	error = derive_column_width(&column_width, dimensions, state.options);
	if (is_error(error))
	{
		terminal_send(TERMINAL_CLEAR_SCREEN);
		ft_dprintf(STDERR_FILENO, "%s", error.msg);
		return ;
	}
		return (error);
	if (!derive_enough_columns(state.terminal, dimensions, column_width))
	{
		terminal_send(TERMINAL_CLEAR_SCREEN);
		ft_dprintf(STDERR_FILENO, "terminal too small to display options");
		return ;
	}
		return (errorf("terminal too small to display options"));
	error = derive_rows(&rows, dimensions, state.options);
	if (is_error(error))
	{
		ft_dprintf(STDERR_FILENO, "%s", error.msg);
		return ;
	}
	box_top(state.terminal);
	row_index = 0;
	while (row_index < dimensions.rows)
	{
		ft_dprintf(STDERR_FILENO, "  ┃ ");
		column_index = 0;
		total_column_width = 0;
		while (column_index < dimensions.columns &&
			rows[row_index][column_index] != NULL)
		{
			box_option(
				rows[row_index][column_index]->name,
				rows[row_index][column_index]->selected,
				unpack_option(state.cursor) == rows[row_index][column_index],
				column_width[column_index]);
			total_column_width += column_width[column_index]
				+ DERIVE_OPTION_PADDING_LEFT + DERIVE_OPTION_PADDING_RIGHT;
			column_index++;
		}
		ft_dprintf(STDERR_FILENO, "%*s ┃  \n",
			state.terminal.columns - total_column_width
			- DERIVE_PADDING_LEFT - DERIVE_PADDING_RIGHT, "");
		row_index++;
	}
	box_bottom(state.terminal);
		return (error);
	draw(state, dimensions, column_width, rows);
	ft_memdel((void **)&column_width);
	derive_free_rows(dimensions, &rows);
	return (ERROR_NULL);
}

void		render(const struct s_state state)
{
	t_error						error;

	error = render_with_error(state);
	if (is_error(error))
		ft_dprintf(STDERR_FILENO, "%s", error.msg);
}