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
|
/*
* Copyright (C) 2012-2023 Robin Haberkorn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <glib.h>
#include <curses.h>
#include "sciteco.h"
#include "string-utils.h"
#include "curses-utils.h"
gsize
teco_curses_format_str(WINDOW *win, const gchar *str, gsize len, gint max_width)
{
int old_x, old_y;
gint chars_added = 0;
getyx(win, old_y, old_x);
if (max_width < 0)
max_width = getmaxx(win) - old_x;
while (len > 0) {
/*
* NOTE: This mapping is similar to
* teco_view_set_representations().
*/
switch (*str) {
case '\e':
chars_added++;
if (chars_added > max_width)
goto truncate;
waddch(win, '$' | A_REVERSE);
break;
case '\r':
chars_added += 2;
if (chars_added > max_width)
goto truncate;
waddch(win, 'C' | A_REVERSE);
waddch(win, 'R' | A_REVERSE);
break;
case '\n':
chars_added += 2;
if (chars_added > max_width)
goto truncate;
waddch(win, 'L' | A_REVERSE);
waddch(win, 'F' | A_REVERSE);
break;
case '\t':
chars_added += 3;
if (chars_added > max_width)
goto truncate;
waddch(win, 'T' | A_REVERSE);
waddch(win, 'A' | A_REVERSE);
waddch(win, 'B' | A_REVERSE);
break;
default:
if (TECO_IS_CTL(*str)) {
chars_added += 2;
if (chars_added > max_width)
goto truncate;
waddch(win, '^' | A_REVERSE);
waddch(win, TECO_CTL_ECHO(*str) | A_REVERSE);
} else {
chars_added++;
if (chars_added > max_width)
goto truncate;
waddch(win, *str);
}
}
str++;
len--;
}
return getcurx(win) - old_x;
truncate:
if (max_width >= 3) {
/*
* Truncate string
*/
wattron(win, A_UNDERLINE | A_BOLD);
mvwaddstr(win, old_y, old_x + max_width - 3, "...");
wattroff(win, A_UNDERLINE | A_BOLD);
}
return getcurx(win) - old_x;
}
gsize
teco_curses_format_filename(WINDOW *win, const gchar *filename,
gint max_width)
{
int old_x = getcurx(win);
g_autofree gchar *filename_printable = teco_string_echo(filename, strlen(filename));
size_t filename_len = strlen(filename_printable);
if (max_width < 0)
max_width = getmaxx(win) - old_x;
if (filename_len <= (size_t)max_width) {
waddstr(win, filename_printable);
} else {
const gchar *keep_post = filename_printable + filename_len -
max_width + 3;
#ifdef G_OS_WIN32
const gchar *keep_pre = g_path_skip_root(filename_printable);
if (keep_pre) {
waddnstr(win, filename_printable,
keep_pre - filename_printable);
keep_post += keep_pre - filename_printable;
}
#endif
wattron(win, A_UNDERLINE | A_BOLD);
waddstr(win, "...");
wattroff(win, A_UNDERLINE | A_BOLD);
waddstr(win, keep_post);
}
return getcurx(win) - old_x;
}
|