Improved linebuffering to stdout

This commit is contained in:
Michiel Broek
2002-07-29 20:31:18 +00:00
parent bc61853ff3
commit 1ed82f8b40
2 changed files with 242 additions and 230 deletions

View File

@@ -46,7 +46,7 @@ int termmode; /* 0 = tty, 1 = ANSI */
void TermInit(int mode)
{
termmode = mode;
termmode = mode;
}
@@ -56,10 +56,11 @@ void TermInit(int mode)
*/
void Enter(int num)
{
int i;
int i;
for(i = 0; i < num; i++)
printf("\n");
for (i = 0; i < num; i++)
fprintf(stdout, "\n");
fflush(stdout);
}
@@ -67,24 +68,26 @@ void Enter(int num)
void pout(int fg, int bg, char *Str)
{
colour(fg, bg);
printf(Str);
colour(fg, bg);
fprintf(stdout, Str);
fflush(stdout);
}
void poutCenter(int fg, int bg, char *Str)
{
colour(fg, bg);
Center(Str);
colour(fg, bg);
Center(Str);
}
void poutCR(int fg, int bg, char *Str)
{
colour(fg, bg);
puts(Str);
colour(fg, bg);
fputs(Str, stdout);
fflush(stdout);
}
@@ -94,83 +97,87 @@ void poutCR(int fg, int bg, char *Str)
*/
void colour(int fg, int bg)
{
if (termmode == 1) {
if (termmode == 1) {
int att=0, fore=37, back=40;
int att=0, fore=37, back=40;
if (fg<0 || fg>31 || bg<0 || bg>7) {
printf("ANSI: Illegal colour specified: %i, %i\n", fg, bg);
return;
}
printf("[");
if ( fg > 15) {
printf("5;");
fg-=16;
}
if (fg > 7) {
att=1;
fg=fg-8;
}
if (fg==0) fore=30;
else if (fg==1) fore=34;
else if (fg==2) fore=32;
else if (fg==3) fore=36;
else if (fg==4) fore=31;
else if (fg==5) fore=35;
else if (fg==6) fore=33;
else fore=37;
if (bg==1) back=44;
else if (bg==2) back=42;
else if (bg==3) back=46;
else if (bg==4) back=41;
else if (bg==5) back=45;
else if (bg==6) back=43;
else if (bg==7) back=47;
else back=40;
printf("%d;%d;%dm", att, fore, back);
if (fg<0 || fg>31 || bg<0 || bg>7) {
fprintf(stdout, "ANSI: Illegal colour specified: %i, %i\n", fg, bg);
fflush(stdout);
return;
}
fprintf(stdout, "[");
if ( fg > 15) {
fprintf(stdout, "5;");
fg-=16;
}
if (fg > 7) {
att=1;
fg=fg-8;
}
if (fg==0) fore=30;
else if (fg==1) fore=34;
else if (fg==2) fore=32;
else if (fg==3) fore=36;
else if (fg==4) fore=31;
else if (fg==5) fore=35;
else if (fg==6) fore=33;
else fore=37;
if (bg==1) back=44;
else if (bg==2) back=42;
else if (bg==3) back=46;
else if (bg==4) back=41;
else if (bg==5) back=45;
else if (bg==6) back=43;
else if (bg==7) back=47;
else back=40;
fprintf(stdout, "%d;%d;%dm", att, fore, back);
fflush(stdout);
}
}
void Center(char *string)
{
int Strlen;
int Maxlen = 70;
int i, x, z;
char *Str;
int Strlen;
int Maxlen = 70;
int i, x, z;
char *Str;
Str = calloc(81, sizeof(char));
Strlen = strlen(string);
Str = calloc(81, sizeof(char));
Strlen = strlen(string);
if(Strlen == Maxlen)
printf("%s\n", string);
else {
x = Maxlen - Strlen;
z = x / 2;
for(i = 0; i < z; i++)
strcat(Str, " ");
strcat(Str, string);
printf("%s\n", Str);
}
if (Strlen == Maxlen)
fprintf(stdout, "%s\n", string);
else {
x = Maxlen - Strlen;
z = x / 2;
for (i = 0; i < z; i++)
strcat(Str, " ");
strcat(Str, string);
fprintf(stdout, "%s\n", Str);
}
free(Str);
fflush(stdout);
free(Str);
}
void clear()
{
if (termmode == 1) {
colour(LIGHTGRAY, BLACK);
printf(ANSI_HOME);
printf(ANSI_CLEAR);
} else
Enter(1);
if (termmode == 1) {
colour(LIGHTGRAY, BLACK);
fprintf(stdout, ANSI_HOME);
fprintf(stdout, ANSI_CLEAR);
fflush(stdout);
} else
Enter(1);
}
@@ -180,38 +187,42 @@ void clear()
*/
void locate(int y, int x)
{
if (termmode > 0) {
if (exitinfo.iScreenLen != 0) {
if (y > exitinfo.iScreenLen || x > 80) {
printf("ANSI: Invalid screen coordinates: %i, %i\n", y, x);
printf("ANSI: exitinfo.iScreenLen: %i\n", exitinfo.iScreenLen);
return;
}
} else {
if (y > 25 || x > 80) {
printf("ANSI: Invalid screen coordinates: %i, %i\n", y, x);
return;
}
}
printf("\x1B[%i;%iH", y, x);
if (termmode > 0) {
if (exitinfo.iScreenLen != 0) {
if (y > exitinfo.iScreenLen || x > 80) {
fprintf(stdout, "ANSI: Invalid screen coordinates: %i, %i\n", y, x);
fprintf(stdout, "ANSI: exitinfo.iScreenLen: %i\n", exitinfo.iScreenLen);
fflush(stdout);
return;
}
} else {
if (y > 25 || x > 80) {
fprintf(stdout, "ANSI: Invalid screen coordinates: %i, %i\n", y, x);
fflush(stdout);
return;
}
}
fprintf(stdout, "\x1B[%i;%iH", y, x);
fflush(stdout);
}
}
void fLine(int Len)
{
int x;
int x;
if (termmode == 0)
for (x = 0; x < Len; x++)
printf("-");
if (termmode == 0)
for (x = 0; x < Len; x++)
fprintf(stdout, "-");
if (termmode == 1)
for (x = 0; x < Len; x++)
printf("%c", 196);
if (termmode == 1)
for (x = 0; x < Len; x++)
fprintf(stdout, "%c", 196);
printf(" \n");
fprintf(stdout, " \n");
fflush(stdout);
}
@@ -219,7 +230,7 @@ void fLine(int Len)
void sLine()
{
fLine(79);
fLine(79);
}
@@ -229,18 +240,19 @@ void sLine()
*/
void mvprintw(int y, int x, const char *format, ...)
{
char *outputstr;
va_list va_ptr;
char *outputstr;
va_list va_ptr;
outputstr = calloc(2048, sizeof(char));
outputstr = calloc(2048, sizeof(char));
va_start(va_ptr, format);
vsprintf(outputstr, format, va_ptr);
va_end(va_ptr);
va_start(va_ptr, format);
vsprintf(outputstr, format, va_ptr);
va_end(va_ptr);
locate(y, x);
printf(outputstr);
free(outputstr);
locate(y, x);
fprintf(stdout, outputstr);
free(outputstr);
fflush(stdout);
}