/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   c_keys_next.c                                      :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <stddef.h>
#include <term.h>

#include "c_init.h"
#include "c_keys.h"
#include "d_define.h"
#include "s_struct.h"

short	c_key_up(char *line[], t_caps *tcaps, t_msh *msh)
{
	char			tmp[ARG_MAX];
	static char		*ptr;
	static t_bool	set = TRUE;

	if (set == TRUE)
	{
		ptr = msh->curr_hist_ptr + ft_strlen(msh->curr_hist_ptr);
		set = FALSE;
	}
	ptr -= 2;
	while (ptr - msh->curr_hist_ptr != 0 && *ptr != '\n')
	{
		ptr--;
	}
	ptr += 1;
	c_home_key(tcaps->plen, tcaps);
	ft_substr_s(tmp, ptr, 0, ft_strclen(ptr, '\n'));
	ft_memdel((void*)&(*line));
	tputs(tgetstr("ce", NULL), 1, ft_putchar);
	*line = ft_strdup(tmp);
	tcaps->cpos = ft_strlen(*line);
	return (1);
}