1 /* vifm
2  * Copyright (C) 2013 xaizek.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #ifndef VIFM__UI__ESCAPE_H__
20 #define VIFM__UI__ESCAPE_H__
21 
22 #include <regex.h>
23 
24 #include <curses.h>
25 
26 #include <stddef.h> /* size_t */
27 
28 #include "../utils/test_helpers.h"
29 #include "colors.h"
30 
31 /* Possible modes of processing escape codes. */
32 typedef enum
33 {
34 	ESM_SHORT,         /* "Normal" state for standard escape codes. */
35 	ESM_GOT_FG_PREFIX, /* After first number of xterm256 foreground sequence. */
36 	ESM_GOT_BG_PREFIX, /* After first number of xterm256 background sequence. */
37 	ESM_WAIT_FG_COLOR, /* After second number of xterm256 foreground sequence. */
38 	ESM_WAIT_BG_COLOR, /* After second number of xterm256 background sequence. */
39 }
40 EscStateMode;
41 
42 /* Holds state of escape sequence parsing. */
43 typedef struct
44 {
45 	EscStateMode mode;   /* Current mode of processing escape codes. */
46 
47 	int attrs;           /* Current set of attributes. */
48 	int fg;              /* Current foreground color. */
49 	int bg;              /* Current background color. */
50 
51 	col_attr_t defaults; /* Default values of other fields. */
52 	int max_colors;      /* Limit on number of colors. */
53 }
54 esc_state;
55 
56 /* Returns a copy of the str with all escape sequences removed.  The string
57  * returned should be freed by a caller. */
58 char * esc_remove(const char str[]);
59 
60 /* Returns number of characters in the str taken by terminal escape
61  * sequences. */
62 size_t esc_str_overhead(const char str[]);
63 
64 /* Highlights all matches of the re regular expression in the line using color
65  * inversion considering escape sequences in the line.  Returns processed string
66  * that should be freed by the caller or NULL on error. */
67 char * esc_highlight_pattern(const char line[], const regex_t *re);
68 
69 /* Prints at most whole line to a window with column and row initial offsets
70  * while honoring maximum character positions specified by the max_width
71  * parameter.  Sets *printed to number of screen characters actually printed,
72  * it's set to zero otherwise.  The truncated parameter specifies whether
73  * printed part is the final part of the line that's being printed.  Returns
74  * offset in the line at which line processing was stopped. */
75 int esc_print_line(const char line[], WINDOW *win, int column, int row,
76 		int max_width, int dry_run, int truncated, esc_state *state, int *printed);
77 
78 /* Initializes escape sequence parsing state with values from the defaults and
79  * limit on number of colors. */
80 void esc_state_init(esc_state *state, const col_attr_t *defaults,
81 		int max_colors);
82 
83 TSTATIC_DEFS(
84 	size_t get_char_width_esc(const char str[]);
85 	void esc_state_update(esc_state *state, const char str[]);
86 	const char * strchar2str(const char str[], int pos, size_t *screen_width);
87 )
88 
89 #endif /* VIFM__UI__ESCAPE_H__ */
90 
91 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
92 /* vim: set cinoptions+=t0 filetype=c : */
93