/*****************************************************************************
	テキスト画面操作ルーチン集
******************************************************************************/

#include	<string.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<ctype.h>


/* 見易いようにプロトタイプ宣言 */
#ifndef STLFUNC
#define STLFUNC


/* LOCATE文の再現(^^;) */
#define STL_locate(x,y)	printf("\x1b=%c%c",(y)+32,(x)+32);

/* CLS! */
#define	STL_cls()	STL_puts("\x1b*"); STL_locate(0,0);

/* テキストの色とアトリビュートを戻す */
#define STL_normal_color()	STL_puts("\x1b[37;0m");

/* グラフ文字の表示マクロ */
#define STL_graph(ch)	putchar('\x1b'); putchar( (ch) );

void	STL_cursor_disp(int num);			/* カーソル表示切り換え */
void	STL_color(int color);		/* テキストの色とアトリビュートを指定 */
void	STL_rlocate(int x,int y);			/* 相対(relative)ロケート */
int		STL_puts(char *str);				/* 文字列表示 */


/* 色番号の文字列化 */
#ifndef	TXTCOLOR
#define	TXTCOLOR

#define	A_NORMAL	0						/* ふつ〜(^^;) */
#define	A_HIGH		1						/* 高輝度 */
#define	A_BLINK		5						/* ブリンク */
#define	A_REVERSE	7						/* 反転表示 */

#define	C_BLACK		30
#define	C_RED		31
#define	C_GREEN		32
#define	C_YELLOW	33
#define	C_BLUE		34
#define	C_PURPLE	35
#define	C_MAGENTA	35
#define	C_WATERBLUE	36
#define	C_SKYBLUE	36
#define	C_LIGHTBLUE	36
#define	C_WHITE		37

#endif




/* カーソル表示切り換え */
void	STL_cursor_disp(int num)
{
	if (num==0)
		STL_puts("\x1b[>5h");
	else
		STL_puts("\x1b[>5l");
	return;
}


/* テキストの色とかを指定 */
void	STL_color(int color)
{
	char	str[64];
	
	sprintf(str,"\x1b[%d;m",color);
	STL_puts(str);
	return;
}


/* 相対(relative)ロケート */
void	STL_rlocate(int x,int y)
{
	char	mx,my;

	if (x<0)
	{ x=-x;	mx='D';	}
	else
		mx='C';

	if (y<0)
	{ y=-y;	my='A';	}
	else
		my='B';
	
	if (x!=0)
		printf("\x1b[%d%c",x,mx);
	if (y!=0)
		printf("\x1b[%d%c",y,my);
	
	return;
}


/* STL_putsの下請け関数 */
static void	hexan(char *str)
{
	int		num=0;
	
	while( isxdigit(*(++str)) )
	{	
		num=num<<4;
		num+=*str;
	}
	putchar(num);
	return;
}



/* 文字列表示(putsだとCR/LFが勝手に入るので・・・。) */
int		STL_puts(char *str)
{
	while (*str!=0)
	{
		if (*str=='\\')
		{
			switch (*(++str))
			{
				case 'x':	hexan(str);		break;
				case 'n':	putchar(10);	break;
				case '\\':	putchar('\\');	break;
				case 't':	putchar(9);		break;
				case 'r':	putchar(13);	break;
				case 'a':	putchar(7);		break;
				case 'b':	putchar(8);		break;
				case 'f':	putchar(12);	break;
				case 'v':	putchar(11);	break;
				default:	putchar(*str);
			}
			str++;
		}
		else
		{
			putchar(*str);
			str++;
		}
	}
	return(0);
}




#endif
