/*
    This file is part of libtermui.

    libtermui is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    libtermui is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libtermui.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2006, Alexandre Becoulet <alexandre.becoulet@free.fr>

*/

#ifndef TERMUI_GETLINE_H_
#define TERMUI_GETLINE_H_

#include <termui/types.h>
#include <termui/bhv.h>

/** Initialize a getline behavior for specified terminal, with given
    maximum line buffer size */
termui_err_t termui_getline_init(struct termui_term_s *tm,
				 struct termui_term_behavior_s *bhv,
				 termui_strlen_t size);

/** Cleanup a getline behavior */
void termui_getline_cleanup(struct termui_term_behavior_s *bhv);

/** Wait for user input and return edited line */
const char *termui_getline_process(struct termui_term_behavior_s *bhv);



/** Prompt display function prototype. Get pointer to terminal object
    and associated private data */
#define TERMUI_GETLINE_FCN_PROMPT(f) termui_err_t f(struct termui_term_s *tm, void *priv)
typedef TERMUI_GETLINE_FCN_PROMPT(termui_getline_prompt_t);

/** Set prompt display function */
void termui_getline_setprompt(struct termui_term_behavior_s *bhv, termui_getline_prompt_t *p);



/** Enable user entered lines history support */
termui_err_t termui_getline_history_init(struct termui_term_behavior_s *bhv, termui_hist_index_t size);

/** Append a line to history */
termui_err_t termui_getline_history_add(struct termui_term_behavior_s *bhv, const char *str);

/** Get a line from history */
const char * termui_getline_history_get(struct termui_term_behavior_s *bhv, termui_hist_index_t index);

/** Append the last entered line to history */
termui_err_t termui_getline_history_addlast(struct termui_term_behavior_s *bhv);

/** Clear all history */
void termui_getline_history_clear(struct termui_term_behavior_s *bhv);

/** Disable history and cleanup */
void termui_getline_history_cleanup(struct termui_term_behavior_s *bhv);



/** Completion list function prototype. Get pointer to getline object
    and associated private data */
#define TERMUI_GETLINE_FCN_COMPLETE(f)	void f(struct termui_term_behavior_s *bhv, void *priv)
typedef TERMUI_GETLINE_FCN_COMPLETE(termui_getline_complete_t);

/** Enable completion support through specified completion callback function */
termui_err_t termui_getline_complete_init(struct termui_term_behavior_s *bhv, termui_getline_complete_t *f);

/** Get line start pointer in current line buffer. Can be called from completion callback */
const char *termui_getline_line_start(struct termui_term_behavior_s *bhv);

/** Get cursor pointer in current line buffer. Can be called from completion callback */
const char *termui_getline_line_cursor(struct termui_term_behavior_s *bhv);

/** Insert text at given cursor position. Can be called from completion callback */
termui_err_t termui_getline_insert(struct termui_term_behavior_s *bhv, const char *str, termui_strlen_t len);

/** Rewrite prompt after completion list display. Can be called from completion callback  */
void termui_getline_reprompt(struct termui_term_behavior_s *bhv);

/** Delete characters at cursor position, may be used from completion handler */
termui_err_t termui_getline_delete(struct termui_term_behavior_s *bhv, termui_cursor_pos_t len);

/** Move cursor forward, may be used from completion handler */
termui_err_t termui_getline_move_forward(struct termui_term_behavior_s *bhv, termui_cursor_pos_t len);

/** Move cursor backward, may be used from completion handler */
termui_err_t termui_getline_move_backward(struct termui_term_behavior_s *bhv, termui_cursor_pos_t len);

/** @internal */
struct				termui_getline_s
{
  termui_cursor_pos_t		width;		/* term width */
  termui_cursor_pos_t		offset;		/* prompt len % term width */
  char				*copy;		/* copy and paste buffer */

  char				**hist;		/* history table */
  termui_hist_index_t		hist_cur;
  termui_hist_index_t		hist_count;	/* current entry count */
  termui_hist_index_t		hist_size;	/* max entry count */

  termui_strlen_t		size;		/* line buffer size */
  char				*line;		/* available line buffer */

  /* edit buffer content pointers */
  char				*buf;		/* current edit buffer start */
  char				*cursor;	/* cursor position */
  char				*end;		/* end of current line */
  char				*max;		/* end of max line size */

  termui_getline_complete_t	*complete;
  termui_getline_prompt_t	*prompt;
};

#endif

