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
/*!
# FYI Msg - Fitted Widths
This optional module contains methods for counting the display width of byte strings, and/or figuring out the closest char index to chop on to make something fit.
*/
use crate;
use Cow;
use UnicodeWidthChar;
/// # Fit to Width.
///
/// Slice (and maybe rejigger) a string to make it fit within a given display
/// width.
///
/// Unlike [`length_width`], this method works line-by-line,
/// evaluating/truncating each individually to keep them within `width` columns.
///
/// ANSI formatting sequences and line breaks are always preserved; a new
/// string will be allocated to stitch everything back together when necessary.
///
/// **This requires the `fitted` crate feature.**
///
/// ## Examples
///
/// ```
/// use fyi_msg::fit_to_width;
///
/// // A single line string that fits as-is.
/// assert_eq!(
/// fit_to_width("Hello World", 11),
/// "Hello World",
/// );
///
/// // A single line with trailing break that doesn't _quite_ fit. Note the
/// // "d" is dropped, but not the trailing "\n".
/// assert_eq!(
/// fit_to_width("Hello World\n", 10),
/// "Hello Worl\n",
/// );
///
/// // Same as above, but with a stupid "\r\n" break, which is also supported.
/// assert_eq!(
/// fit_to_width("Hello World\r\n", 10),
/// "Hello Worl\r\n",
/// );
///
/// // Multi-line trim!
/// assert_eq!(
/// fit_to_width("\
/// Apples\n\
/// Bananas\n\
/// Carrots\n\
/// ", 6),
/// "\
/// Apples\n\
/// Banana\n\
/// Carrot\n\
/// ",
/// );
/// ```
///
/// This method does not "parse" ANSI sequences, but will recognize and
/// preserve them (even in chopped regions) to prevent any accidental
/// display weirdness.
///
/// ```
/// use fyi_msg::fit_to_width;
///
/// let s = "\x1b[1mHello World\x1b[0m";
/// assert_eq!(
/// fit_to_width(s, 5),
/// "\x1b[1mHello\x1b[0m", // The reset was saved!
/// );
///
/// // If there are multiple sequences in the cut, they'll be added back in
/// // one big lump at the end, minus their original content.
/// let s = "\x1b[1mHello\x1b[0m \x1b[91mWorld\x1b[0m";
/// assert_eq!(
/// fit_to_width(s, 5),
/// "\x1b[1mHello\x1b[0m\x1b[91m\x1b[0m",
/// );
/// ```
/// # Length Width.
///
/// Return the maximum byte *length* for the slice that fits a given display
/// *width*, such that `slice[..len]` will be a valid substring likely to fit.
///
/// See the documentation for [`width`] for more information.
///
/// **This requires the `fitted` crate feature.**
///
/// ## Examples
///
/// ```
/// // Split to a display width of five.
/// let full: &str = "\x1b[2mBjörk\x1b[0m Guðmundsdóttir";
/// let idx = fyi_msg::length_width(full, 5);
/// assert_eq!(
/// &full[..idx],
/// "\x1b[2mBjörk\x1b[0m",
/// );
///
/// // Be careful with strings containing ANSI sequences or line breaks,
/// // though; they can be lost depending on where the max width falls.
/// let idx = fyi_msg::length_width(full, 4);
/// assert_eq!(
/// &full[..idx],
/// "\x1b[2mBjör", // No reset!
/// );
///
/// // Were you to send the above to the printer, this would happen:
/// println!("{}", &full[..idx]); // Looks fine.
/// println!("Something else!"); // This is dim too!
///
/// // To avoid this, use `fit_to_width` instead.
/// let fitted = fyi_msg::fit_to_width(&full, 4);
/// assert_eq!(
/// fitted,
/// "\x1b[2mBjör\x1b[0m", // There we are!
/// );
/// println!("{fitted}"); // Looks the same as before.
/// println!("Something else!"); // Now this looks fine too.
/// ```
/// # Width.
///
/// Find the (total) "display width" of a string slice.
///
/// Like anything having to do with width vs length, this should be considered
/// at best an approximation. The [`unicode_width`](https://crates.io/crates/unicode-width) crate
/// is used under-the-hood; refer to their documentation for more details on
/// how terrible Unicode is. Haha.
///
/// This method is ANSI-aware and will automatically skip past any
/// [CSI](https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_(Control_Sequence_Introducer)_sequences) or [OSC](https://en.wikipedia.org/wiki/ANSI_escape_code#OSC_(Operating_System_Command)_sequences)
/// sequences it encounters.
///
/// **This requires the `fitted` crate feature.**
///
/// ## Examples
///
/// ```
/// use fyi_msg::width;
///
/// // The problem with length is inclusivity:
/// assert_eq!("\x1b[1mBjörk\x1b[0m".len(), 14);
///
/// // Even without the ANSI, the length is still too high:
/// assert_eq!("Björk".len(), 6);
///
/// // Width is just what it looks like:
/// assert_eq!(width("\x1b[1mBjörk\x1b[0m"), 5);
/// assert_eq!(
/// width("Björk"), // Unicode.
/// width("Bjork"), // ASCII.
/// );
///
/// // Be careful with multi-line content; widths are _total_.
/// assert_eq!(width("Hello World"), 11);
/// assert_eq!(width("Hello\nWorld"), 10); // Line breaks have no width.
/// ```