2006-10-02 05:22:43 +08:00
/** \file screen.c High level library for handling the terminal screen
2006-11-15 00:21:25 +08:00
The screen library allows the interactive reader to write its
output to screen efficiently by keeping an inetrnal representation
of the current screen contents and trying to find the most
efficient way for transforming that to the desired screen content .
2006-10-02 05:22:43 +08:00
*/
2006-10-02 00:02:58 +08:00
# include "config.h"
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include <errno.h>
# include <termios.h>
# include <sys/types.h>
# ifdef HAVE_SYS_TERMIOS_H
# include <sys/termios.h>
# endif
# include <unistd.h>
# include <wctype.h>
# if HAVE_NCURSES_H
# include <ncurses.h>
# else
# include <curses.h>
# endif
# if HAVE_TERMIO_H
# include <termio.h>
# endif
# if HAVE_TERM_H
# include <term.h>
# elif HAVE_NCURSES_TERM_H
# include <ncurses/term.h>
# endif
# include <wchar.h>
2006-10-05 07:33:12 +08:00
# include <time.h>
2006-10-02 00:02:58 +08:00
# include <assert.h>
2012-03-04 18:45:51 +08:00
# include <vector>
2006-10-02 00:02:58 +08:00
# include "fallback.h"
# include "common.h"
# include "util.h"
# include "wutil.h"
# include "output.h"
# include "highlight.h"
# include "screen.h"
2006-11-17 22:59:25 +08:00
# include "env.h"
2006-10-02 00:02:58 +08:00
2007-09-24 16:49:33 +08:00
/**
The number of characters to indent new blocks
*/
# define INDENT_STEP 4
2006-10-02 05:22:43 +08:00
/**
Ugly kludge . The internal buffer used to store output of
tputs . Since tputs external function can only take an integer and
not a pointer as parameter we need a static storage buffer .
*/
2012-03-04 18:45:51 +08:00
typedef std : : vector < char > data_buffer_t ;
static data_buffer_t * s_writeb_buffer = 0 ;
2006-10-02 00:02:58 +08:00
2012-03-26 16:21:10 +08:00
static int s_writeb ( char c ) ;
/* Class to temporarily set s_writeb_buffer and the writer function in a scoped way */
class scoped_buffer_t {
data_buffer_t * const old_buff ;
int ( * const old_writer ) ( char ) ;
public :
scoped_buffer_t ( data_buffer_t * buff ) : old_buff ( s_writeb_buffer ) , old_writer ( output_get_writer ( ) )
{
s_writeb_buffer = buff ;
output_set_writer ( s_writeb ) ;
}
~ scoped_buffer_t ( )
{
s_writeb_buffer = old_buff ;
output_set_writer ( old_writer ) ;
}
} ;
2006-10-02 00:02:58 +08:00
/**
Tests if the specified narrow character sequence is present at the
specified position of the specified wide character string . All of
\ c seq must match , but str may be longer than seq .
*/
2011-12-29 04:36:47 +08:00
static int try_sequence ( const char * seq , const wchar_t * str )
2006-10-02 00:02:58 +08:00
{
int i ;
for ( i = 0 ; ; i + + )
{
if ( ! seq [ i ] )
return i ;
if ( seq [ i ] ! = str [ i ] )
return 0 ;
}
return 0 ;
}
2008-01-14 00:47:47 +08:00
/**
Returns the number of columns left until the next tab stop , given
the current cursor postion .
*/
2012-08-02 07:32:52 +08:00
static size_t next_tab_stop ( size_t in )
2006-11-11 18:54:52 +08:00
{
2006-11-15 21:30:46 +08:00
/*
Assume tab stops every 8 characters if undefined
*/
2006-11-11 18:54:52 +08:00
if ( init_tabs < = 0 )
init_tabs = 8 ;
2011-12-27 11:18:46 +08:00
2006-11-11 18:54:52 +08:00
return ( ( in / init_tabs ) + 1 ) * init_tabs ;
}
2012-04-14 05:40:16 +08:00
// PCA for term256 support, let's just detect the escape codes directly
static int is_term256_escape ( const wchar_t * str ) {
// An escape code looks like this: \x1b[38;5;<num>m
// or like this: \x1b[48;5;<num>m
// parse out the required prefix
int len = try_sequence ( " \x1b [38;5; " , str ) ;
if ( ! len ) len = try_sequence ( " \x1b [48;5; " , str ) ;
if ( ! len ) return 0 ;
// now try parsing out a string of digits
// we need at least one
if ( ! iswdigit ( str [ len ] ) ) return 0 ;
while ( iswdigit ( str [ len ] ) ) len + + ;
// look for the terminating m
if ( str [ len + + ] ! = L ' m ' ) return 0 ;
// success
return len ;
}
2006-10-02 00:02:58 +08:00
/**
Calculate the width of the specified prompt . Does some clever magic
to detect common escape sequences that may be embeded in a prompt ,
such as color codes .
*/
2012-08-02 07:32:52 +08:00
static size_t calc_prompt_width ( const wchar_t * prompt )
2006-10-02 00:02:58 +08:00
{
2012-08-02 07:32:52 +08:00
size_t res = 0 ;
2012-01-15 14:48:53 +08:00
size_t j , k ;
2006-10-02 00:02:58 +08:00
for ( j = 0 ; prompt [ j ] ; j + + )
{
2007-08-22 15:52:39 +08:00
if ( prompt [ j ] = = L ' \x1b ' )
2006-10-02 00:02:58 +08:00
{
/*
This is the start of an escape code . Try to guess it ' s width .
*/
2012-01-15 14:48:53 +08:00
size_t p ;
2006-11-15 00:21:25 +08:00
int len = 0 ;
2012-04-14 05:40:16 +08:00
bool found = false ;
2011-12-27 11:18:46 +08:00
2006-11-15 00:21:25 +08:00
/*
Detect these terminfo color escapes with parameter
value 0. .7 , all of which don ' t move the cursor
*/
2012-04-14 05:40:16 +08:00
char * const esc [ ] =
2006-10-02 00:02:58 +08:00
{
2006-11-15 00:21:25 +08:00
set_a_foreground ,
set_a_background ,
set_foreground ,
set_background ,
}
;
2006-10-02 00:02:58 +08:00
2006-11-15 00:21:25 +08:00
/*
Detect these semi - common terminfo escapes without any
parameter values , all of which don ' t move the cursor
*/
2012-04-14 05:40:16 +08:00
char * const esc2 [ ] =
2006-11-15 00:21:25 +08:00
{
enter_bold_mode ,
exit_attribute_mode ,
enter_underline_mode ,
exit_underline_mode ,
enter_standout_mode ,
exit_standout_mode ,
flash_screen ,
enter_subscript_mode ,
exit_subscript_mode ,
enter_superscript_mode ,
exit_superscript_mode ,
enter_blink_mode ,
enter_italics_mode ,
exit_italics_mode ,
enter_reverse_mode ,
enter_shadow_mode ,
exit_shadow_mode ,
enter_standout_mode ,
exit_standout_mode ,
enter_secure_mode
2006-10-02 00:02:58 +08:00
}
2006-11-15 00:21:25 +08:00
;
2012-04-14 05:40:16 +08:00
for ( p = 0 ; p < sizeof esc / sizeof * esc & & ! found ; p + + )
2006-11-15 00:21:25 +08:00
{
2012-01-15 14:48:53 +08:00
if ( ! esc [ p ] )
2006-11-15 00:21:25 +08:00
continue ;
2006-10-02 00:02:58 +08:00
2006-11-15 00:21:25 +08:00
for ( k = 0 ; k < 8 ; k + + )
2006-10-02 00:02:58 +08:00
{
2012-01-15 14:48:53 +08:00
len = try_sequence ( tparm ( esc [ p ] , k ) , & prompt [ j ] ) ;
2006-10-02 00:02:58 +08:00
if ( len )
{
j + = ( len - 1 ) ;
2012-04-14 05:40:16 +08:00
found = true ;
2006-11-15 00:21:25 +08:00
break ;
2006-10-02 00:02:58 +08:00
}
}
}
2012-04-14 05:40:16 +08:00
// PCA for term256 support, let's just detect the escape codes directly
if ( ! found ) {
len = is_term256_escape ( & prompt [ j ] ) ;
if ( len ) {
j + = ( len - 1 ) ;
found = true ;
}
}
2006-11-15 00:21:25 +08:00
2012-01-15 14:48:53 +08:00
for ( p = 0 ; p < ( sizeof ( esc2 ) / sizeof ( char * ) ) & & ! found ; p + + )
2006-10-02 00:02:58 +08:00
{
2012-01-15 14:48:53 +08:00
if ( ! esc2 [ p ] )
2006-11-15 00:21:25 +08:00
continue ;
2006-10-02 00:02:58 +08:00
/*
2006-11-15 00:21:25 +08:00
Test both padded and unpadded version , just to
be safe . Most versions of tparm don ' t actually
seem to do anything these days .
2006-10-02 00:02:58 +08:00
*/
2012-01-15 14:48:53 +08:00
len = maxi ( try_sequence ( tparm ( esc2 [ p ] ) , & prompt [ j ] ) ,
try_sequence ( esc2 [ p ] , & prompt [ j ] ) ) ;
2011-12-27 11:18:46 +08:00
2006-11-15 00:21:25 +08:00
if ( len )
{
j + = ( len - 1 ) ;
2012-04-14 05:40:16 +08:00
found = true ;
2006-11-15 00:21:25 +08:00
}
2006-10-02 00:02:58 +08:00
}
2011-12-27 11:18:46 +08:00
2006-11-15 00:21:25 +08:00
if ( ! found )
2006-10-02 00:02:58 +08:00
{
2006-11-15 21:30:46 +08:00
if ( prompt [ j + 1 ] = = L ' k ' )
2006-11-15 00:21:25 +08:00
{
2012-01-14 18:42:17 +08:00
const env_var_t term_name = env_get_string ( L " TERM " ) ;
if ( ! term_name . missing ( ) & & wcsstr ( term_name . c_str ( ) , L " screen " ) = = term_name )
2006-11-15 00:21:25 +08:00
{
2012-01-07 19:05:49 +08:00
const wchar_t * end ;
2006-11-15 00:21:25 +08:00
j + = 2 ;
2012-04-14 05:40:16 +08:00
found = true ;
2007-08-22 15:52:39 +08:00
end = wcsstr ( & prompt [ j ] , L " \x1b \\ " ) ;
2006-11-15 00:21:25 +08:00
if ( end )
{
2006-11-15 21:30:46 +08:00
/*
You ' d thing this should be
' ( end - prompt ) + 2 ' , in order to move j
past the end of the string , but there is
a ' j + + ' at the end of each lap , so j
should always point to the last menged
character , e . g . + 1.
*/
2006-11-15 00:21:25 +08:00
j = ( end - prompt ) + 1 ;
}
else
{
break ;
}
2011-12-27 11:18:46 +08:00
}
}
2006-10-02 00:02:58 +08:00
}
2011-12-27 11:18:46 +08:00
2006-10-02 00:02:58 +08:00
}
2006-11-15 00:21:25 +08:00
else if ( prompt [ j ] = = L ' \t ' )
{
res = next_tab_stop ( res ) ;
}
2010-09-24 19:01:13 +08:00
else if ( prompt [ j ] = = L ' \n ' )
{
res = 0 ;
}
2006-11-15 00:21:25 +08:00
else
{
/*
Ordinary decent character . Just add width .
*/
2012-07-16 01:45:18 +08:00
res + = fish_wcwidth ( prompt [ j ] ) ;
2006-11-15 00:21:25 +08:00
}
}
2006-10-02 00:02:58 +08:00
return res ;
}
2006-10-05 07:33:12 +08:00
/**
Test if there is space between the time fields of struct stat to
use for sub second information . If so , we assume this space
contains the desired information .
*/
static int room_for_usec ( struct stat * st )
{
int res = ( ( & ( st - > st_atime ) + 2 ) = = & ( st - > st_mtime ) & &
2007-08-02 03:07:54 +08:00
( & ( st - > st_atime ) + 4 ) = = & ( st - > st_ctime ) ) ;
2006-10-05 07:33:12 +08:00
return res ;
}
/**
Stat stdout and stderr and save result .
This should be done before calling a function that may cause output .
*/
static void s_save_status ( screen_t * s )
{
2012-06-17 03:30:20 +08:00
// PCA Let's not do this futimes stuff, because sudo dumbly uses the
// tty's ctime as part of its tty_tickets feature
// Disabling this should fix https://github.com/fish-shell/fish-shell/issues/122
#if 0
2006-10-05 07:33:12 +08:00
/*
This futimes call tries to trick the system into using st_mtime
as a tampering flag . This of course only works on systems where
futimes is defined , but it should make the status saving stuff
failsafe .
*/
struct timeval t [ ] =
{
{
time ( 0 ) - 1 ,
0
}
,
{
time ( 0 ) - 1 ,
0
}
}
;
/*
Don ' t check return value on these . We don ' t care if they fail ,
really . This is all just to make the prompt look ok , which is
impossible to do 100 % reliably . We try , at least .
*/
futimes ( 1 , t ) ;
futimes ( 2 , t ) ;
2012-06-17 03:30:20 +08:00
# endif
2006-10-05 07:33:12 +08:00
fstat ( 1 , & s - > prev_buff_1 ) ;
fstat ( 2 , & s - > prev_buff_2 ) ;
}
/**
Stat stdout and stderr and compare result to previous result in
reader_save_status . Repaint if modification time has changed .
Unfortunately , for some reason this call seems to give a lot of
false positives , at least under Linux .
*/
static void s_check_status ( screen_t * s )
{
fflush ( stdout ) ;
fflush ( stderr ) ;
fstat ( 1 , & s - > post_buff_1 ) ;
fstat ( 2 , & s - > post_buff_2 ) ;
int changed = ( s - > prev_buff_1 . st_mtime ! = s - > post_buff_1 . st_mtime ) | |
( s - > prev_buff_2 . st_mtime ! = s - > post_buff_2 . st_mtime ) ;
if ( room_for_usec ( & s - > post_buff_1 ) )
{
changed = changed | | ( ( & s - > prev_buff_1 . st_mtime ) [ 1 ] ! = ( & s - > post_buff_1 . st_mtime ) [ 1 ] ) | |
( ( & s - > prev_buff_2 . st_mtime ) [ 1 ] ! = ( & s - > post_buff_2 . st_mtime ) [ 1 ] ) ;
}
if ( changed )
{
2006-10-09 21:26:42 +08:00
/*
Ok , someone has been messing with our screen . We will want
to repaint . However , we do not know where the cursor is . It
is our best bet that we are still on the same line , so we
move to the beginning of the line , reset the modelled screen
2006-10-26 18:22:53 +08:00
contents , and then set the modeled cursor y - pos to its
2011-12-27 11:18:46 +08:00
earlier value .
2006-10-09 21:26:42 +08:00
*/
2011-12-27 11:18:46 +08:00
2012-08-05 04:54:20 +08:00
int prev_line = s - > actual . cursor . y ;
2009-02-23 04:28:52 +08:00
write_loop ( 1 , " \r " , 1 ) ;
2012-03-25 18:00:38 +08:00
s_reset ( s , false ) ;
2012-08-05 04:54:20 +08:00
s - > actual . cursor . y = prev_line ;
2006-10-05 07:33:12 +08:00
}
}
2006-10-02 05:22:43 +08:00
/**
2006-11-15 00:21:25 +08:00
Appends a character to the end of the line that the output cursor is
on . This function automatically handles linebreaks and lines longer
2011-12-27 11:18:46 +08:00
than the screen width .
2006-10-02 00:02:58 +08:00
*/
2012-08-05 09:41:14 +08:00
static void s_desired_append_char ( screen_t * s ,
wchar_t b ,
int c ,
int indent ,
size_t prompt_width )
2006-10-02 00:02:58 +08:00
{
2012-08-05 04:54:20 +08:00
int line_no = s - > desired . cursor . y ;
2011-12-27 11:18:46 +08:00
2006-10-02 00:02:58 +08:00
switch ( b )
{
case L ' \n ' :
{
int i ;
2012-07-16 01:45:18 +08:00
s - > desired . create_line ( s - > desired . line_count ( ) ) ;
2012-08-05 04:54:20 +08:00
s - > desired . cursor . y + + ;
s - > desired . cursor . x = 0 ;
2007-09-24 16:49:33 +08:00
for ( i = 0 ; i < prompt_width + indent * INDENT_STEP ; i + + )
2006-10-02 00:02:58 +08:00
{
2006-10-07 08:56:25 +08:00
s_desired_append_char ( s , L ' ' , 0 , indent , prompt_width ) ;
2006-10-02 00:02:58 +08:00
}
break ;
}
2012-08-05 09:41:14 +08:00
2006-10-02 00:02:58 +08:00
case L ' \r ' :
{
2011-12-28 10:41:38 +08:00
line_t & current = s - > desired . line ( line_no ) ;
2012-07-16 01:45:18 +08:00
current . clear ( ) ;
2012-08-05 04:54:20 +08:00
s - > desired . cursor . x = 0 ;
2012-08-05 09:41:14 +08:00
break ;
}
2006-10-02 00:02:58 +08:00
default :
{
2006-10-02 04:54:23 +08:00
int screen_width = common_get_width ( ) ;
2012-07-16 01:45:18 +08:00
int cw = fish_wcwidth ( b ) ;
int ew = fish_wcwidth ( ellipsis_char ) ;
2011-12-27 11:18:46 +08:00
2011-12-28 10:41:38 +08:00
s - > desired . create_line ( line_no ) ;
2012-08-05 09:41:14 +08:00
2006-10-02 05:22:43 +08:00
/*
2012-08-05 09:41:14 +08:00
Check if we are at the end of the line . If so , print an
ellipsis character and continue on the next line .
*/
2012-08-05 04:54:20 +08:00
if ( s - > desired . cursor . x + cw + ew > screen_width )
2006-10-02 04:54:23 +08:00
{
2012-07-16 01:45:18 +08:00
s - > desired . line ( line_no ) . append ( ellipsis_char , HIGHLIGHT_COMMENT ) ;
2011-12-28 10:41:38 +08:00
2012-07-29 08:49:46 +08:00
line_no = ( int ) s - > desired . line_count ( ) ;
2011-12-28 10:41:38 +08:00
s - > desired . add_line ( ) ;
2012-08-05 04:54:20 +08:00
s - > desired . cursor . y + + ;
s - > desired . cursor . x = 0 ;
2012-08-05 09:41:14 +08:00
if ( prompt_width > ew ) {
for ( size_t i = 0 ; i < ( prompt_width - ew ) ; i + + )
{
s_desired_append_char ( s , L ' ' , 0 , indent , prompt_width ) ;
}
}
2006-10-07 08:56:25 +08:00
s_desired_append_char ( s , ellipsis_char , HIGHLIGHT_COMMENT , indent , prompt_width ) ;
2006-10-02 04:54:23 +08:00
}
2011-12-27 11:18:46 +08:00
2011-12-28 10:41:38 +08:00
line_t & line = s - > desired . line ( line_no ) ;
2012-07-16 01:45:18 +08:00
line . append ( b , c ) ;
2012-08-05 04:54:20 +08:00
s - > desired . cursor . x + = cw ;
2006-10-02 00:02:58 +08:00
break ;
}
}
2011-12-27 11:18:46 +08:00
2006-10-02 00:02:58 +08:00
}
2006-10-02 05:22:43 +08:00
/**
The writeb function offered to tputs .
*/
2006-10-02 00:02:58 +08:00
static int s_writeb ( char c )
{
2012-03-04 18:45:51 +08:00
s_writeb_buffer - > push_back ( c ) ;
2006-10-02 00:02:58 +08:00
return 0 ;
}
2006-10-02 05:22:43 +08:00
/**
2006-10-16 23:32:26 +08:00
Write the bytes needed to move screen cursor to the specified
position to the specified buffer . The actual_cursor field of the
specified screen_t will be updated .
2011-12-27 11:18:46 +08:00
2006-10-16 23:32:26 +08:00
\ param s the screen to operate on
\ param b the buffer to send the output escape codes to
\ param new_x the new x position
\ param new_y the new y position
2006-10-02 05:22:43 +08:00
*/
2012-03-04 18:45:51 +08:00
static void s_move ( screen_t * s , data_buffer_t * b , int new_x , int new_y )
2006-10-02 00:02:58 +08:00
{
int i ;
int x_steps , y_steps ;
2011-12-27 11:18:46 +08:00
2006-10-02 00:02:58 +08:00
char * str ;
/*
2011-12-27 11:18:46 +08:00
debug ( 0 , L " move from %d %d to %d %d " ,
s - > screen_cursor [ 0 ] , s - > screen_cursor [ 1 ] ,
2006-11-15 00:21:25 +08:00
new_x , new_y ) ;
2006-10-02 00:02:58 +08:00
*/
2012-03-26 16:21:10 +08:00
scoped_buffer_t scoped_buffer ( b ) ;
2012-08-05 04:54:20 +08:00
y_steps = new_y - s - > actual . cursor . y ;
2006-10-02 00:02:58 +08:00
if ( y_steps > 0 & & ( strcmp ( cursor_down , " \n " ) = = 0 ) )
2011-12-27 11:18:46 +08:00
{
2006-10-02 00:02:58 +08:00
/*
2006-11-15 21:30:46 +08:00
This is very strange - it seems some ( all ? ) consoles use a
2006-10-02 00:02:58 +08:00
simple newline as the cursor down escape . This will of
2006-11-15 21:30:46 +08:00
course move the cursor to the beginning of the line as well
as moving it down one step . The cursor_up does not have this
behaviour . . .
2006-10-02 00:02:58 +08:00
*/
2012-08-05 04:54:20 +08:00
s - > actual . cursor . x = 0 ;
2006-10-02 00:02:58 +08:00
}
2011-12-27 11:18:46 +08:00
2006-10-02 00:02:58 +08:00
if ( y_steps < 0 )
{
str = cursor_up ;
}
else
{
str = cursor_down ;
2011-12-27 11:18:46 +08:00
2006-10-02 00:02:58 +08:00
}
2011-12-27 11:18:46 +08:00
2006-10-02 00:02:58 +08:00
for ( i = 0 ; i < abs ( y_steps ) ; i + + )
{
writembs ( str ) ;
}
2012-08-05 04:54:20 +08:00
x_steps = new_x - s - > actual . cursor . x ;
2011-12-27 11:18:46 +08:00
2006-10-02 00:02:58 +08:00
if ( x_steps & & new_x = = 0 )
{
2012-03-04 18:45:51 +08:00
b - > push_back ( ' \r ' ) ;
2006-10-02 00:02:58 +08:00
x_steps = 0 ;
}
2011-12-27 11:18:46 +08:00
2012-07-16 01:45:18 +08:00
if ( x_steps < 0 )
{
2006-10-02 00:02:58 +08:00
str = cursor_left ;
}
else
{
str = cursor_right ;
}
2011-12-27 11:18:46 +08:00
2006-10-02 00:02:58 +08:00
for ( i = 0 ; i < abs ( x_steps ) ; i + + )
{
writembs ( str ) ;
}
2012-08-05 04:54:20 +08:00
s - > actual . cursor . x = new_x ;
s - > actual . cursor . y = new_y ;
2006-10-02 00:02:58 +08:00
}
2006-10-02 05:22:43 +08:00
/**
Set the pen color for the terminal
*/
2012-03-04 18:45:51 +08:00
static void s_set_color ( screen_t * s , data_buffer_t * b , int c )
2006-10-02 00:02:58 +08:00
{
2012-03-26 16:21:10 +08:00
scoped_buffer_t scoped_buffer ( b ) ;
2012-02-07 12:14:19 +08:00
unsigned int uc = ( unsigned int ) c ;
2012-02-14 01:52:17 +08:00
set_color ( highlight_get_color ( uc & 0xffff , false ) ,
2012-03-26 16:21:10 +08:00
highlight_get_color ( ( uc > > 16 ) & 0xffff , true ) ) ;
2006-10-02 00:02:58 +08:00
}
2006-10-02 05:22:43 +08:00
/**
Convert a wide character to a multibyte string and append it to the
buffer .
*/
2012-03-04 18:45:51 +08:00
static void s_write_char ( screen_t * s , data_buffer_t * b , wchar_t c )
2006-10-02 00:02:58 +08:00
{
2012-03-26 16:21:10 +08:00
scoped_buffer_t scoped_buffer ( b ) ;
2012-08-05 04:54:20 +08:00
s - > actual . cursor . x + = fish_wcwidth ( c ) ;
2006-10-02 00:02:58 +08:00
writech ( c ) ;
}
2006-11-15 21:30:46 +08:00
2012-07-16 01:45:18 +08:00
/**
Convert a wide string to a multibyte string and append it to the
buffer . Returns the width .
*/
static int s_write_string ( screen_t * s , data_buffer_t * b , const wcstring & str )
{
scoped_buffer_t scoped_buffer ( b ) ;
int width = fish_wcswidth ( str . c_str ( ) , str . size ( ) ) ;
writestr ( str . c_str ( ) ) ;
2012-08-05 04:54:20 +08:00
s - > actual . cursor . x + = width ;
2012-07-16 01:45:18 +08:00
return width ;
}
2006-10-02 05:22:43 +08:00
/**
Send the specified string through tputs and append the output to
the specified buffer .
*/
2012-03-04 18:45:51 +08:00
static void s_write_mbs ( data_buffer_t * b , char * s )
2006-10-02 00:02:58 +08:00
{
2012-03-26 16:21:10 +08:00
scoped_buffer_t scoped_buffer ( b ) ;
2006-10-02 00:02:58 +08:00
writembs ( s ) ;
}
2006-10-02 05:22:43 +08:00
/**
Convert a wide string to a multibyte string and append it to the
buffer .
*/
2012-03-04 18:45:51 +08:00
static void s_write_str ( data_buffer_t * b , const wchar_t * s )
2006-10-02 00:02:58 +08:00
{
2012-03-26 16:21:10 +08:00
scoped_buffer_t scoped_buffer ( b ) ;
2006-10-02 00:02:58 +08:00
writestr ( s ) ;
}
2012-07-16 01:45:18 +08:00
/** Returns the length of the "shared prefix" of the two lines, which is the run of matching text and colors.
If the prefix ends on a combining character , do not include the previous character in the prefix .
*/
static size_t line_shared_prefix ( const line_t & a , const line_t & b )
{
size_t idx , max = std : : min ( a . size ( ) , b . size ( ) ) ;
for ( idx = 0 ; idx < max ; idx + + )
{
wchar_t ac = a . char_at ( idx ) , bc = b . char_at ( idx ) ;
if ( fish_wcwidth ( ac ) < 1 | | fish_wcwidth ( bc ) < 1 )
{
/* Possible combining mark, return one index prior */
if ( idx > 0 ) idx - - ;
break ;
}
/* We're done if the text or colors are different */
if ( ac ! = bc | | a . color_at ( idx ) ! = b . color_at ( idx ) )
break ;
}
return idx ;
}
2006-10-02 05:22:43 +08:00
/**
Update the screen to match the desired output .
*/
2011-12-29 04:36:47 +08:00
static void s_update ( screen_t * scr , const wchar_t * prompt )
2006-10-02 00:02:58 +08:00
{
2012-08-02 07:32:52 +08:00
size_t prompt_width = calc_prompt_width ( prompt ) ;
2006-10-05 05:45:02 +08:00
int screen_width = common_get_width ( ) ;
2006-10-09 21:26:42 +08:00
int need_clear = scr - > need_clear ;
2012-03-04 18:45:51 +08:00
data_buffer_t output ;
2006-10-09 21:26:42 +08:00
scr - > need_clear = 0 ;
2012-03-04 18:45:51 +08:00
2006-10-05 05:45:02 +08:00
if ( scr - > actual_width ! = screen_width )
{
2006-10-09 21:26:42 +08:00
need_clear = 1 ;
2006-10-05 05:45:02 +08:00
s_move ( scr , & output , 0 , 0 ) ;
scr - > actual_width = screen_width ;
2012-03-25 18:00:38 +08:00
s_reset ( scr , false ) ;
2006-10-05 05:45:02 +08:00
}
2011-12-27 11:18:46 +08:00
2011-12-28 10:41:38 +08:00
if ( wcscmp ( prompt , scr - > actual_prompt . c_str ( ) ) )
2006-10-02 00:02:58 +08:00
{
s_move ( scr , & output , 0 , 0 ) ;
s_write_str ( & output , prompt ) ;
2011-12-28 10:41:38 +08:00
scr - > actual_prompt = prompt ;
2012-08-05 04:54:20 +08:00
scr - > actual . cursor . x = ( int ) prompt_width ;
2006-10-02 00:02:58 +08:00
}
2011-12-27 11:18:46 +08:00
2012-07-29 08:49:46 +08:00
for ( size_t i = 0 ; i < scr - > desired . line_count ( ) ; i + + )
2006-10-02 00:02:58 +08:00
{
2012-07-16 01:45:18 +08:00
const line_t & o_line = scr - > desired . line ( i ) ;
2011-12-28 10:41:38 +08:00
line_t & s_line = scr - > actual . create_line ( i ) ;
2012-08-02 07:32:52 +08:00
size_t start_pos = ( i = = 0 ? prompt_width : 0 ) ;
2012-07-16 01:45:18 +08:00
int current_width = 0 ;
2006-10-09 21:26:42 +08:00
if ( need_clear )
2006-10-05 07:33:12 +08:00
{
2012-08-02 07:32:52 +08:00
s_move ( scr , & output , ( int ) start_pos , ( int ) i ) ;
2006-10-05 07:33:12 +08:00
s_write_mbs ( & output , clr_eol ) ;
2012-07-16 01:45:18 +08:00
s_line . clear ( ) ;
2006-10-05 07:33:12 +08:00
}
2012-07-16 01:45:18 +08:00
/* Note that skip_remaining is a width, not a character count */
2012-08-02 07:32:52 +08:00
size_t skip_remaining = start_pos ;
2012-07-16 01:45:18 +08:00
/* Compute how much we should skip. At a minimum we skip over the prompt. But also skip over the shared prefix of what we want to output now, and what we output before, to avoid repeatedly outputting it. */
size_t shared_prefix = line_shared_prefix ( o_line , s_line ) ;
if ( shared_prefix > 0 )
{
int prefix_width = fish_wcswidth ( & o_line . text . at ( 0 ) , shared_prefix ) ;
if ( prefix_width > skip_remaining )
skip_remaining = prefix_width ;
}
/* Skip over skip_remaining width worth of characters */
size_t j = 0 ;
for ( ; j < o_line . size ( ) ; j + + )
{
int width = fish_wcwidth ( o_line . char_at ( j ) ) ;
2012-08-05 09:32:15 +08:00
if ( skip_remaining < = width )
2012-07-16 01:45:18 +08:00
break ;
2012-08-05 09:32:15 +08:00
skip_remaining - = width ;
2012-07-16 01:45:18 +08:00
current_width + = width ;
}
/* Skip over zero-width characters (e.g. combining marks at the end of the prompt) */
for ( ; j < o_line . size ( ) ; j + + )
{
int width = fish_wcwidth ( o_line . char_at ( j ) ) ;
if ( width > 0 )
break ;
}
/* Now actually output stuff */
for ( ; j < o_line . size ( ) ; j + + )
{
2012-07-29 08:49:46 +08:00
s_move ( scr , & output , current_width , ( int ) i ) ;
2012-07-16 01:45:18 +08:00
s_set_color ( scr , & output , o_line . color_at ( j ) ) ;
s_write_char ( scr , & output , o_line . char_at ( j ) ) ;
current_width + = fish_wcwidth ( o_line . char_at ( j ) ) ;
}
/* If we wrote more on this line last time, clear it */
int prev_length = ( s_line . text . empty ( ) ? 0 : fish_wcswidth ( & s_line . text . at ( 0 ) , s_line . text . size ( ) ) ) ;
if ( prev_length > current_width )
2006-10-02 00:02:58 +08:00
{
2012-07-29 08:49:46 +08:00
s_move ( scr , & output , current_width , ( int ) i ) ;
2006-10-02 00:02:58 +08:00
s_write_mbs ( & output , clr_eol ) ;
}
}
2012-07-16 01:45:18 +08:00
/* Clear remaining lines */
2012-07-29 08:49:46 +08:00
for ( size_t i = scr - > desired . line_count ( ) ; i < scr - > actual . line_count ( ) ; i + + )
2006-10-02 00:02:58 +08:00
{
2012-07-29 08:49:46 +08:00
s_move ( scr , & output , 0 , ( int ) i ) ;
2006-10-02 00:02:58 +08:00
s_write_mbs ( & output , clr_eol ) ;
}
2011-12-27 11:18:46 +08:00
2012-08-05 04:54:20 +08:00
s_move ( scr , & output , scr - > desired . cursor . x , scr - > desired . cursor . y ) ;
2006-10-02 00:02:58 +08:00
s_set_color ( scr , & output , 0xffffffff ) ;
2012-03-04 18:45:51 +08:00
if ( ! output . empty ( ) )
2006-10-02 00:02:58 +08:00
{
2012-03-04 18:45:51 +08:00
write_loop ( 1 , & output . at ( 0 ) , output . size ( ) ) ;
2006-10-02 00:02:58 +08:00
}
2011-12-27 11:18:46 +08:00
2012-07-16 01:45:18 +08:00
/* We have now synced our actual screen against our desired screen. Note that this is a big assignment! */
scr - > actual = scr - > desired ;
2006-10-02 00:02:58 +08:00
}
2008-01-14 00:47:47 +08:00
/**
Returns non - zero if we are using a dumb terminal .
*/
2007-08-02 03:07:54 +08:00
static int is_dumb ( )
{
return ( ! cursor_up | | ! cursor_down | | ! cursor_left | | ! cursor_right ) ;
}
2006-10-02 00:02:58 +08:00
2006-10-02 04:54:23 +08:00
void s_write ( screen_t * s ,
2011-12-29 04:36:47 +08:00
const wchar_t * prompt ,
2012-07-13 03:51:47 +08:00
const wchar_t * commandline ,
2012-08-02 07:32:52 +08:00
size_t explicit_len ,
2011-12-29 04:36:47 +08:00
const int * c ,
const int * indent ,
2012-08-05 04:47:56 +08:00
size_t cursor_pos )
2006-10-02 00:02:58 +08:00
{
2012-08-05 04:54:20 +08:00
screen_data_t : : cursor_t cursor_arr ;
2006-10-02 00:02:58 +08:00
2012-08-02 07:32:52 +08:00
size_t prompt_width ;
size_t screen_width ;
2006-10-09 21:26:42 +08:00
2012-08-02 07:32:52 +08:00
int current_line_width = 0 , newline_count = 0 , explicit_portion_width = 0 ;
size_t max_line_width = 0 ;
2011-12-27 11:18:46 +08:00
2006-10-09 21:26:42 +08:00
CHECK ( s , ) ;
CHECK ( prompt , ) ;
2012-07-13 03:51:47 +08:00
CHECK ( commandline , ) ;
2006-10-09 21:26:42 +08:00
CHECK ( c , ) ;
CHECK ( indent , ) ;
2007-08-02 03:07:54 +08:00
2007-09-24 16:56:02 +08:00
/*
If we are using a dumb terminal , don ' t try any fancy stuff ,
just print out the text .
*/
2007-08-02 03:07:54 +08:00
if ( is_dumb ( ) )
{
char * prompt_narrow = wcs2str ( prompt ) ;
2012-07-13 03:51:47 +08:00
char * buffer_narrow = wcs2str ( commandline ) ;
2011-12-27 11:18:46 +08:00
2009-02-23 04:28:52 +08:00
write_loop ( 1 , " \r " , 1 ) ;
write_loop ( 1 , prompt_narrow , strlen ( prompt_narrow ) ) ;
write_loop ( 1 , buffer_narrow , strlen ( buffer_narrow ) ) ;
2007-08-02 03:07:54 +08:00
free ( prompt_narrow ) ;
free ( buffer_narrow ) ;
2011-12-27 11:18:46 +08:00
2007-08-02 03:07:54 +08:00
return ;
}
2011-12-27 11:18:46 +08:00
2006-10-09 21:26:42 +08:00
prompt_width = calc_prompt_width ( prompt ) ;
screen_width = common_get_width ( ) ;
2006-10-02 00:02:58 +08:00
2006-10-05 07:33:12 +08:00
s_check_status ( s ) ;
2006-10-02 04:54:23 +08:00
/*
2007-09-24 16:56:02 +08:00
Ignore prompts wider than the screen - only print a two
character placeholder . . .
It would be cool to truncate the prompt , but because it can
contain escape sequences , this is harder than you ' d think .
2006-10-02 04:54:23 +08:00
*/
2007-09-24 16:49:33 +08:00
if ( prompt_width > = screen_width )
2006-10-02 04:54:23 +08:00
{
2007-08-03 06:59:39 +08:00
prompt = L " > " ;
prompt_width = 2 ;
2006-10-02 04:54:23 +08:00
}
2011-12-27 11:18:46 +08:00
2006-10-02 04:54:23 +08:00
/*
2007-09-24 16:56:02 +08:00
Completely ignore impossibly small screens
2006-10-02 04:54:23 +08:00
*/
if ( screen_width < 4 )
{
return ;
}
2007-09-24 16:56:02 +08:00
/*
Check if we are overflowing
*/
2012-08-02 07:32:52 +08:00
size_t last_char_that_fits = 0 ;
for ( size_t i = 0 ; commandline [ i ] ; i + + )
2007-09-24 16:49:33 +08:00
{
2012-07-13 03:51:47 +08:00
if ( commandline [ i ] = = L ' \n ' )
2007-09-24 16:49:33 +08:00
{
if ( current_line_width > max_line_width )
max_line_width = current_line_width ;
current_line_width = indent [ i ] * INDENT_STEP ;
2012-07-13 03:51:47 +08:00
newline_count + + ;
2007-09-24 16:49:33 +08:00
}
else
{
2012-07-16 01:45:18 +08:00
int width = fish_wcwidth ( commandline [ i ] ) ;
2012-07-13 03:51:47 +08:00
current_line_width + = width ;
if ( i < explicit_len )
explicit_portion_width + = width ;
if ( prompt_width + current_line_width < screen_width )
last_char_that_fits = i ;
2007-09-24 16:49:33 +08:00
}
}
if ( current_line_width > max_line_width )
max_line_width = current_line_width ;
2011-12-28 10:41:38 +08:00
s - > desired . resize ( 0 ) ;
2012-08-05 04:54:20 +08:00
s - > desired . cursor . x = s - > desired . cursor . y = 0 ;
2012-07-13 03:51:47 +08:00
/* If we cannot fit with the autosuggestion, but we can fit without it, truncate the autosuggestion. We limit this check to just one line to avoid confusion; not sure how well this would work with multiple lines */
wcstring truncated_autosuggestion_line ;
if ( newline_count = = 0 & & prompt_width + max_line_width > = screen_width & & prompt_width + explicit_portion_width < screen_width )
{
max_line_width = screen_width - prompt_width - 1 ;
truncated_autosuggestion_line = wcstring ( commandline , 0 , last_char_that_fits ) ;
truncated_autosuggestion_line . push_back ( ellipsis_char ) ;
commandline = truncated_autosuggestion_line . c_str ( ) ;
}
2012-08-02 07:32:52 +08:00
for ( size_t i = 0 ; i < prompt_width ; i + + )
2012-07-24 07:05:37 +08:00
{
s_desired_append_char ( s , L ' ' , 0 , 0 , prompt_width ) ;
}
2007-09-24 16:49:33 +08:00
/*
2007-09-24 16:56:02 +08:00
If overflowing , give the prompt its own line to improve the
situation .
2007-09-24 16:49:33 +08:00
*/
if ( max_line_width + prompt_width > = screen_width )
2006-10-02 00:02:58 +08:00
{
2007-09-24 16:49:33 +08:00
s_desired_append_char ( s , L ' \n ' , 0 , 0 , 0 ) ;
prompt_width = 0 ;
2006-10-02 00:02:58 +08:00
}
2011-12-27 11:18:46 +08:00
2012-08-05 04:47:56 +08:00
size_t i ;
2012-07-13 03:51:47 +08:00
for ( i = 0 ; commandline [ i ] ; i + + )
2006-10-02 00:02:58 +08:00
{
int col = c [ i ] ;
2011-12-27 11:18:46 +08:00
2012-08-05 04:47:56 +08:00
if ( i = = cursor_pos )
2006-10-02 00:02:58 +08:00
{
col = 0 ;
}
2011-12-27 11:18:46 +08:00
2012-08-05 04:47:56 +08:00
if ( i = = cursor_pos )
2006-10-02 07:21:36 +08:00
{
2012-08-05 04:54:20 +08:00
cursor_arr = s - > desired . cursor ;
2006-10-02 07:21:36 +08:00
}
2011-12-27 11:18:46 +08:00
2012-07-13 03:51:47 +08:00
s_desired_append_char ( s , commandline [ i ] , col , indent [ i ] , prompt_width ) ;
2011-12-27 11:18:46 +08:00
2012-08-05 04:54:20 +08:00
if ( i = = cursor_pos & & s - > desired . cursor . y ! = cursor_arr . y & & commandline [ i ] ! = L ' \n ' )
2006-10-07 08:56:25 +08:00
{
2007-09-24 16:56:02 +08:00
/*
2006-10-07 08:56:25 +08:00
Ugh . We are placed exactly at the wrapping point of a
wrapped line , move cursor to the line below so the
cursor won ' t be on the ellipsis which looks
unintuitive .
*/
2012-08-05 04:54:20 +08:00
cursor_arr . x = s - > desired . cursor . x - fish_wcwidth ( commandline [ i ] ) ;
cursor_arr . y = s - > desired . cursor . y ;
2006-10-07 08:56:25 +08:00
}
2006-10-02 00:02:58 +08:00
}
2012-08-05 04:47:56 +08:00
if ( i = = cursor_pos )
2006-10-02 00:02:58 +08:00
{
2012-08-05 04:54:20 +08:00
cursor_arr = s - > desired . cursor ;
2006-10-02 00:02:58 +08:00
}
2011-12-27 11:18:46 +08:00
2012-08-05 04:54:20 +08:00
s - > desired . cursor = cursor_arr ;
2006-10-02 00:02:58 +08:00
s_update ( s , prompt ) ;
2006-10-05 07:33:12 +08:00
s_save_status ( s ) ;
2006-10-02 00:02:58 +08:00
}
2012-03-25 18:00:38 +08:00
void s_reset ( screen_t * s , bool reset_cursor )
2006-10-02 00:02:58 +08:00
{
2006-10-09 21:26:42 +08:00
CHECK ( s , ) ;
2007-10-01 06:53:54 +08:00
2012-08-05 04:54:20 +08:00
int prev_line = s - > actual . cursor . y ;
2011-12-28 10:41:38 +08:00
s - > actual . resize ( 0 ) ;
2012-08-05 04:54:20 +08:00
s - > actual . cursor . x = s - > actual . cursor . y = 0 ;
2011-12-28 10:41:38 +08:00
s - > actual_prompt = L " " ;
2006-10-09 21:26:42 +08:00
s - > need_clear = 1 ;
2007-09-24 16:13:01 +08:00
if ( ! reset_cursor )
{
/*
This should prevent reseting the cursor position during the
next repaint .
*/
2009-02-23 04:28:52 +08:00
write_loop ( 1 , " \r " , 1 ) ;
2012-08-05 04:54:20 +08:00
s - > actual . cursor . y = prev_line ;
2007-09-24 16:13:01 +08:00
}
2007-10-15 21:21:06 +08:00
fstat ( 1 , & s - > prev_buff_1 ) ;
fstat ( 2 , & s - > prev_buff_2 ) ;
2006-10-02 00:02:58 +08:00
}