Finish support for plugging in a new output function for output.c

darcs-hash:20060216142100-ac50b-06db798debb56c7640f7ffec0bc32c8ca7b013e5.gz
This commit is contained in:
axel 2006-02-17 00:21:00 +10:00
parent 94cae410f3
commit cfa367f14c
2 changed files with 41 additions and 40 deletions

View file

@ -51,6 +51,8 @@
*/ */
#define COLORS (sizeof(col)/sizeof(wchar_t *)) #define COLORS (sizeof(col)/sizeof(wchar_t *))
static int writeb_internal( char c );
/** /**
Names of different colors. Names of different colors.
*/ */
@ -106,14 +108,14 @@ static char *writestr_buff = 0;
The function used for output The function used for output
*/ */
static int (*out)(char *str) = &writembs; static int (*out)(char c) = &writeb_internal;
static void output_destroy() static void output_destroy()
{ {
free( writestr_buff ); free( writestr_buff );
} }
void output_set_writer( int (*writer)(char *) ) void output_set_writer( int (*writer)(char) )
{ {
out = writer; out = writer;
} }
@ -141,8 +143,8 @@ void set_color( int c, int c2 )
{ {
c = c2 = FISH_COLOR_NORMAL; c = c2 = FISH_COLOR_NORMAL;
if( fg ) if( fg )
out( tparm( fg, 0 ) ); writembs( tparm( fg, 0 ) );
out( exit_attribute_mode ); writembs( exit_attribute_mode );
return; return;
} }
@ -175,7 +177,7 @@ void set_color( int c, int c2 )
Background color changed and is set, so we enter bold Background color changed and is set, so we enter bold
mode to make reading easier mode to make reading easier
*/ */
out( enter_bold_mode ); writembs( enter_bold_mode );
} }
if(!bg_set && last_bg_set) if(!bg_set && last_bg_set)
{ {
@ -183,14 +185,14 @@ void set_color( int c, int c2 )
Background color changed and is no longer set, so we Background color changed and is no longer set, so we
exit bold mode exit bold mode
*/ */
out( exit_attribute_mode ); writembs( exit_attribute_mode );
/* /*
We don't know if exit_attribute_mode resets colors, so We don't know if exit_attribute_mode resets colors, so
we set it to something known. we set it to something known.
*/ */
if( fg ) if( fg )
{ {
out( tparm( fg, 0 ) ); writembs( tparm( fg, 0 ) );
last_color=0; last_color=0;
} }
} }
@ -201,8 +203,8 @@ void set_color( int c, int c2 )
if( c==FISH_COLOR_NORMAL ) if( c==FISH_COLOR_NORMAL )
{ {
if( fg ) if( fg )
out( tparm( fg, 0 ) ); writembs( tparm( fg, 0 ) );
out( exit_attribute_mode ); writembs( exit_attribute_mode );
last_color2 = FISH_COLOR_NORMAL; last_color2 = FISH_COLOR_NORMAL;
} }
@ -210,7 +212,7 @@ void set_color( int c, int c2 )
{ {
if( fg ) if( fg )
{ {
out( tparm( fg, c ) ); writembs( tparm( fg, c ) );
} }
} }
} }
@ -223,13 +225,13 @@ void set_color( int c, int c2 )
{ {
if( bg ) if( bg )
{ {
out( tparm( bg, 0 ) ); writembs( tparm( bg, 0 ) );
} }
out(exit_attribute_mode); writembs(exit_attribute_mode);
if(( last_color != FISH_COLOR_NORMAL ) && fg ) if(( last_color != FISH_COLOR_NORMAL ) && fg )
{ {
out(tparm( fg, last_color )); writembs(tparm( fg, last_color ));
} }
last_color2 = c2; last_color2 = c2;
@ -238,7 +240,7 @@ void set_color( int c, int c2 )
{ {
if( bg ) if( bg )
{ {
out( tparm( bg, c2 ) ); writembs( tparm( bg, c2 ) );
} }
last_color2 = c2; last_color2 = c2;
} }
@ -260,30 +262,37 @@ void move_cursor( int steps )
if( steps < 0 ){ if( steps < 0 ){
for( i=0; i>steps; i--) for( i=0; i>steps; i--)
{ {
out(cursor_left); writembs(cursor_left);
} }
} }
else else
{ {
for( i=0; i<steps; i++) for( i=0; i<steps; i++)
{ {
out(cursor_right); writembs(cursor_right);
} }
} }
} }
static int writeb_internal( char c )
{
write( 1, &c, 1 );
return 0;
}
int writeb( tputs_arg_t b ) int writeb( tputs_arg_t b )
{ {
write( 1, &b, 1 ); out( b );
return 0; return 0;
} }
int writembs( char *str ) int writembs( char *str )
{ {
#ifdef TPUTS_KLUDGE #ifdef TPUTS_KLUDGE
write( 1, str, strlen(str)); while( *str )
out( *str );
#else #else
tputs(str,1,&writeb); tputs(str,1,writeb);
#endif #endif
return 0; return 0;
} }
@ -294,24 +303,17 @@ int writech( wint_t ch )
char buff[MB_CUR_MAX]; char buff[MB_CUR_MAX];
size_t bytes = wcrtomb( buff, ch, &out_state ); size_t bytes = wcrtomb( buff, ch, &out_state );
int err; int err;
int i;
while( (err =write( 1, buff, bytes ) ) ) for( i=0; i<bytes; i++ )
{ out( buff[i] );
if( err >= 0 )
break;
if( errno == EINTR )
continue;
wperror( L"write" );
return 1;
}
return 0; return 0;
} }
void writestr( const wchar_t *str ) void writestr( const wchar_t *str )
{ {
char *pos;
// while( *str ) // while( *str )
// writech( *str++ ); // writech( *str++ );
@ -355,7 +357,8 @@ void writestr( const wchar_t *str )
/* /*
Write Write
*/ */
write( 1, writestr_buff, strlen( writestr_buff ) ); for( pos = writestr_buff; *pos; pos++ )
out( *pos );
} }
@ -429,17 +432,15 @@ int writespace( int c )
{ {
if( repeat_char && strlen(repeat_char) ) if( repeat_char && strlen(repeat_char) )
{ {
out( tparm( repeat_char, ' ', c ) ); writembs( tparm( repeat_char, ' ', c ) );
} }
else else
{ {
write( 1, " ", mini(c,8) ); int i;
if( c>8)
{
writespace( c-8);
}
}
for( i=0; i<c; i++ )
out( ' ' );
}
return 0; return 0;
} }

View file

@ -135,7 +135,7 @@ int writeb( tputs_arg_t b );
Set the function used for writing in move_cursor, writespace and Set the function used for writing in move_cursor, writespace and
set_color. By default, writembs is used. set_color. By default, writembs is used.
*/ */
void output_set_writer( int (*writer)(char *) ); void output_set_writer( int (*writer)(char) );
#endif #endif