aboutsummaryrefslogtreecommitdiffhomepage
path: root/output.c
blob: 92f97f03d10329b2fe8c789129adf61ffc1b7922 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#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

#include <term.h>
#include <signal.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <wchar.h>

#include "util.h"
#include "wutil.h"
#include "expand.h"
#include "common.h"
#include "output.h"
#include "highlight.h"

/**
   Number of color names in the col array
*/
#define COLORS (sizeof(col)/sizeof(wchar_t *))

/**
   Names of different colors. 
*/
static wchar_t *col[]=
{
	L"black",
	L"red",
	L"green",
	L"brown",
	L"yellow",
	L"blue",
	L"magenta",
	L"purple",
	L"cyan",
	L"white"
	L"normal"
}
	;

/**
   Mapping from color name (the 'col' array) to color index as used in
   ANSI color terminals, and also the fish_color_* constants defined
   in highlight.h. Non-ANSI terminals will display the wrong colors,
   since they use a different mapping.
*/
static int col_idx[]=
{
	0, 
	1,
	2,
	3,
	3,
	4,
	5,
	5,
	6,
	7,
	FISH_COLOR_NORMAL,
}
	;

void set_color( int c, int c2 )
{
	static int last_color = FISH_COLOR_NORMAL, last_color2=FISH_COLOR_NORMAL;
	int bg_set=0, last_bg_set=0;
	char *fg = 0, *bg=0;

	if( (set_a_foreground != 0) && (strlen( set_a_foreground) != 0 ) )
	{
		fg = set_a_foreground;
		bg = set_a_background;
	}
	else if( (set_foreground != 0) && (strlen( set_foreground) != 0 ) )
	{
		fg = set_foreground;
		bg = set_background;
	}

	if( (c == FISH_COLOR_RESET) || (c2 == FISH_COLOR_RESET))
	{
		c = c2 = FISH_COLOR_NORMAL;
		if( fg )
			writembs( tparm( set_a_foreground, 0 ) );
		writembs( exit_attribute_mode );
		return;
	}

	if( last_color2 != FISH_COLOR_NORMAL &&
		last_color2 != FISH_COLOR_RESET &&
		last_color2 != FISH_COLOR_IGNORE )
	{
		/*
		  Background was set
		*/
		last_bg_set=1;
	}

	if( c2 != FISH_COLOR_NORMAL &&
		c2 != FISH_COLOR_RESET &&
		c2 != FISH_COLOR_IGNORE )
	{
		/*
		  Background is set
		*/
		bg_set=1;
		c = (c2==FISH_COLOR_WHITE)?FISH_COLOR_BLACK:FISH_COLOR_WHITE;
	}

	if( (enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0))
	{
		if(bg_set && !last_bg_set)
		{
			/*
			  Background color changed and is set, so we enter bold mode to make reading easier
			*/
			writembs( enter_bold_mode );
		}
		if(!bg_set && last_bg_set)
		{
			/*
			  Background color changed and is no longer set, so we exit bold mode
			*/
			writembs( exit_attribute_mode );
			/*
			  We don't know if exit_attribute_mode resets colors, so
			  we set it to something known.
			*/
			if( fg )
			{
				writembs( tparm( fg, 0 ) );
				last_color=0;
			}
		}
	}

	if( last_color != c )
	{
		if( c==FISH_COLOR_NORMAL )
		{
			if( fg )
				writembs( tparm( fg, 0 ) );
			writembs( exit_attribute_mode );

			last_color2 = FISH_COLOR_NORMAL;
		}
		else if( ( c >= 0) && ( c < FISH_COLOR_NORMAL ) )
		{
			if( fg )
			{
				writembs( tparm( fg, c ) );
			}
		}
	}

	last_color = c;

	if( last_color2 != c2 )
	{
		if( c2 == FISH_COLOR_NORMAL )
		{
			if( bg )
			{
				writembs( tparm( bg, 0 ) );
			}

			writembs(exit_attribute_mode);
			if(( last_color != FISH_COLOR_NORMAL ) && fg )
			{
				writembs(tparm( fg, last_color ));
			}

			last_color2 = c2;
		}
		else if ((c2 >= 0 ) &&(c2 < FISH_COLOR_NORMAL))
		{
			if( bg )
			{
				writembs( tparm( bg, c2 ) );
			}
			last_color2 = c2;
		}
	}
}

int writembs( char *str )
{
#ifdef TPUTS_KLUDGE
	write( 1, str, strlen(str));
#else
	tputs(str,1,&writeb);
#endif
	return 0;
}

/**
   Write a wide character to fd 1.
*/
int writech( wint_t ch )
{
	static mbstate_t out_state;
	char buff[MB_CUR_MAX];
	size_t bytes = wcrtomb( buff, ch, &out_state );
	int err;
	
	while( (err =write( 1, buff, bytes ) ) )
	{
		if( err >= 0 )
			break;
		
		if( errno == EINTR )
			continue;
		
		wperror( L"write" );
		return 1;
	}
	
	return 0;
}

/**
   Write a wide character string to FD 1.
*/
void writestr( const wchar_t *str )
{
	while( *str != 0 )
		writech( *(str++) );
}


/**
   Write a wide character string to FD 1. If the string is wider than
   the specified maximum, truncate and ellipsize it.
*/
void writestr_ellipsis( const wchar_t *str, int max_width )
{
	int written=0;
	int tot = my_wcswidth(str);

	if( tot <= max_width )
	{
		writestr( str );
		return;
	}

	while( *str != 0 )
	{
		int w = wcwidth( *str );
		if( written+w+wcwidth( ellipsis_char )>max_width )
			break;
		written+=w;
		writech( *(str++) );
	}

	written += wcwidth( ellipsis_char );
	writech( ellipsis_char );

	while( written < max_width )
	{
		written++;
		writestr( L" " );
	}
}

/**
   Escape and write a string to fd 1
*/
int write_escaped_str( const wchar_t *str, int max_len )
{

	wchar_t *out = escape( wcsdup(str), 1 );
	int i;
	int len = my_wcswidth( out );
	int written=0;

	if( max_len && (max_len < len))
	{
		for( i=0; (written+wcwidth(out[i]))<=(max_len-1); i++ )
		{
			writech( out[i] );
			written += wcwidth( out[i] );
		}
		writech( ellipsis_char );
		written += wcwidth( ellipsis_char );
		
		for( i=written; i<max_len; i++ )
		{
			writech( L' ' );
			written++;
		}
	}
	else
	{
		written = len;
		writestr( out );
	}

	free( out );
	return written;
}


/**
   parm_ich seems to often be undefined, so we use this
   workalike. Writes the specified number of spaces.
*/
int writespace( int c )
{
	if( repeat_char && strlen(repeat_char) )
	{
		debug( 1, L"YAY" );
		
		writembs( tparm( repeat_char, ' ', c ) );
	}
	else
	{
		write( 1, "        ", mini(c,8) );
		if( c>8)
		{
			writespace( c-8);
		}
	}
	
	return 0;
}


int output_color_code( const wchar_t *val )
{
	int i, color=-1;
	
	for( i=0; i<COLORS; i++ )
	{
		if( wcscasecmp( col[i], val ) == 0 )
		{
			color = col_idx[i];
			break;
		}
	}

	if( color >= 0 )
		return color;
	else
		return FISH_COLOR_NORMAL;
}