/* W2.C		Status display; Prompting			01 Nov 1987 */

#include <ws.h>

void show_status()		/* displays status line for current index */
{
	extern char	*t_name;	/* name of text file		*/
	extern INDEX	t_begin, t_end;	/* text begin, end		*/
	extern INDEX	index;		/* current pos in text		*/
	extern unsigned n_lines;	/* no. of text lines		*/
/*	extern unsigned t_line,t_col;	/* line,col of current index */*/
extern unsigned	cx,cy;		/* cursor pos of current index	*/
	extern FLAG	ins_flag;	/* TRUE iff in insert mode	*/

	int	InpWait();		/* TRUE iff input is waiting	*/
	void	CursOff(), CursOn();	/* turn cursor off, on		*/
	void	CursorTo();		/* positions cursor on screen	*/
	void	LoVideo(), HiVideo();	/* sets screen display attributes */

	if (InpWait()) {		/* if input is waiting		*/
		CursorTo(cx,cy);	/* move cursor where it belongs	*/
		return;			/* and return immediately	*/
	}
	CursOff();
	CursorTo(PX,PY); 
	LoVideo();
	printf("      Line %-4d Col %-3d Ind %-5d   %3s  %14s %5d lines %5d ch",
		cy,cx, index-t_begin+1, 
		(ins_flag) ? "Ins" : "Ovr", 
		t_name, n_lines, t_end-t_begin);
	HiVideo(); 
	CursorTo(cx,cy); 
	CursOn();
	return;
}


int pmpt_key(ch)	/* prompts control char ch and returns next input */
int ch;
{
	void 	CursorTo();		/* positions cursor on screen	*/
	void	OutCtl();		/* writes control char to screen */
	int	GetKey();		/* gets next input char		*/

	CursorTo(PX,PY);
	OutCtl(ch);
	if ((ch=GetKey()) != 0x7F) ch &= 0x1F;
	OutCtl(ch);
	return ch;
}


void prompt(str)		/* displays string str at prompt location */
char *str;
{
	void 	CursorTo();		/* positions cursor on screen	*/
	void	ClrEol();		/* clears screen to end of line	*/
	void	LoVideo(), HiVideo();	/* set video attributes		*/
	void	OutStr();		/* writes string to screen	*/

	CursorTo(PX,PY); ClrEol(); 
	LoVideo();
	OutStr(str);
	HiVideo();
	return;
}


void unprompt()			/* leads out of the prompt line	*/
{
	void	to_xy();		/* positions cursor at cx,cy	*/

	to_xy();
	return;
}


static char string[80];	/* global buffer for concatenated prompt strings */


#define BIG_Y	0x59
#define BIG_N	0x4E


FLAG pmptYN(str,deflt)		/* prompts string str for Yes/No answer	*/
char *str;			/*  deflt ('Y' or 'N') is default	*/
FLAG deflt;			/* returns TRUE iff 'Y', FALSE iff 'N'	*/
{
	int	GetKey();		/* gets input char without echo	*/
	void	prompt(),unprompt();	/* leads in,out of prompt string */
	char	*strcat(), *strcpy();	/* concatenates, copies strings	*/
	int	toupper();		/* converts char to upper case	*/
	void	OutChar();		/* writes char to screen	*/

	static int	c;		/* input char			*/

	prompt(strcat( strcpy(string,str),
			(deflt) ? "? ([Y]/N) " : "? (Y/[N]) "));
	do
		if ( (c=toupper(GetKey())) == 0x0D) {
			c=toupper(deflt);
			break;
		}
	while (c != BIG_Y && c != BIG_N);
	OutChar(c);
	unprompt();
	return (c==BIG_Y);
}

# define ESC 0x1B

void pmpt_ack(str)	/* prompts string str for acknowledge with ESC */
char *str;
{
	int	GetKey();		/* gets input char without echo	*/
	void	prompt(),unprompt();	/* leads in,out of prompt string */
	char	*strcat(), *strcpy();	/* concatenates, copies strings	*/

	static int	c;		/* inpupt char			*/

	prompt(strcat( strcpy(string,str), " Press <ESC>")); 
	while ( GetKey() != ESC) ;
	unprompt();
	return;
}
