1 /* Invisible Vector Library
2 * simple FlexBox-based TUI engine
4 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
5 * Understanding is not required. Only obedience.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 3 of the License ONLY.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 module iv
.egtui
.utils
/*is aliced*/;
25 import iv
.egtui
.types
;
28 // ////////////////////////////////////////////////////////////////////////// //
29 // calculate text bounds (can call delegate to output string too ;-)
30 // use "|...|" to "quote" word
31 void calcTextBounds (out int cols
, out int lines
, const(char)[] text
, int maxwdt
,
32 scope void delegate (int x
, int y
, const(char)[] s
) dg
=null)
36 void putWord (const(char)[] text
) {
37 while (text
.length
> 0) {
38 while (col
== 0 && text
.length
> maxwdt
) {
39 if (dg
!is null) dg(0, lines
, text
[0..maxwdt
]);
41 text
= text
[maxwdt
..$];
43 if (text
.length
== 0) break;
45 if (dg
!is null) dg(0, lines
, text
);
46 col
+= cast(int)text
.length
;
47 if (cols
< col
) cols
= col
;
50 int nw
= col
+cast(int)text
.length
+1;
53 if (dg
!is null) dg(col
, lines
, text
);
54 col
+= cast(int)text
.length
;
55 if (cols
< col
) cols
= col
;
65 while (pos
< text
.length
&& text
.ptr
[pos
] != '\n' && text
.ptr
[pos
] <= ' ') ++pos
;
66 if (pos
> 0) { text
= text
[pos
..$]; continue; }
68 if (text
.ptr
[pos
] == '\n') {
74 if (text
.ptr
[pos
] == '|') {
76 while (pos
< text
.length
&& text
.ptr
[pos
] != '|') ++pos
;
77 putWord(text
[1..pos
]);
78 if (pos
>= text
.length
) break;
79 text
= text
[pos
+1..$];
81 while (pos
< text
.length
&& text
.ptr
[pos
] > ' ') ++pos
;
82 putWord(text
[0..pos
]);
90 // ////////////////////////////////////////////////////////////////////////// //
91 // calculate text bounds, insert soft wraps, remove excessive spaces
92 // use "|...|" to "quote" word
93 // return new text length
94 uint calcTextBoundsEx (out int cols
, out int lines
, char[] text
, int maxwdt
) {
98 if (maxwdt
< 1) maxwdt
= 1;
104 void putText (const(char)[] s
...) {
105 foreach (char ch
; s
) {
106 if (dpos
< dtext
.length
) dtext
.ptr
[dpos
++] = ch
;
110 // replace soft wraps with blanks
111 foreach (char ch
; text
) {
112 if (ch
== EOT
) break;
113 if (ch
== SoftWrap
) ch
= ' ';
114 if (ch
< 1 || ch
> 3) {
115 if (ch
!= '\n' && (ch
< ' ' || ch
== 127)) ch
= ' ';
120 text
= text
[0..dpos
];
123 void putWord (const(char)[] text
) {
124 while (text
.length
> 0) {
125 while (col
== 0 && text
.length
> maxwdt
) {
126 putText(text
[0..maxwdt
]);
128 text
= text
[maxwdt
..$];
130 if (text
.length
== 0) break;
133 col
+= cast(int)text
.length
;
134 if (cols
< col
) cols
= col
;
137 int nw
= col
+cast(int)text
.length
+1;
141 col
+= cast(int)text
.length
+1;
142 if (cols
< col
) cols
= col
;
146 if (lines
!= 0 || col
) putText(SoftWrap
);
153 if (text
.length
&& text
.ptr
[0] > 0 && text
.ptr
[0] <= 3) {
154 putText(text
.ptr
[0]);
157 while (text
.length
) {
159 while (pos
< text
.length
&& text
.ptr
[pos
] != '\n' && text
.ptr
[pos
] <= ' ') ++pos
;
160 if (pos
> 0) { text
= text
[pos
..$]; continue; }
162 if (text
.ptr
[pos
] == '\n') {
163 if (lines || col
) putText('\n');
168 if (text
.length
&& text
.ptr
[0] > 0 && text
.ptr
[0] <= 3) {
169 putText(text
.ptr
[0]);
174 if (text
.ptr
[pos
] == '|') {
176 while (pos
< text
.length
&& text
.ptr
[pos
] != '|') ++pos
;
177 putWord(text
[1..pos
]);
178 if (pos
>= text
.length
) break;
179 text
= text
[pos
+1..$];
181 while (pos
< text
.length
&& text
.ptr
[pos
] > ' ') ++pos
;
182 putWord(text
[0..pos
]);
186 if (col
> 0) ++lines
;
188 return cast(uint)dpos
;