forked from nushell/reedline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ide_menu.rs
848 lines (744 loc) · 29.7 KB
/
ide_menu.rs
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
use super::{menu_functions::find_common_string, Menu, MenuEvent, MenuTextStyle};
use crate::{
core_editor::Editor, menu_functions::string_difference, painting::{Painter, strip_ansi}, Completer,
Suggestion, UndoBehavior,
};
use nu_ansi_term::{ansi::RESET, Style};
use itertools::{EitherOrBoth::{Both, Left, Right}, Itertools};
pub enum DescriptionMode {
/// Description is shown on the right of the completion if there is enough space
/// otherwise it is shown on the left
PreferRight,
/// Description is always shown on the right
Right,
/// Description is always shown on the left
Left,
}
/// Symbols used for the border of the menu
struct BorderSymbols {
pub top_left: char,
pub top_right: char,
pub bottom_left: char,
pub bottom_right: char,
pub horizontal: char,
pub vertical: char,
}
impl Default for BorderSymbols {
fn default() -> Self {
Self {
top_left: '╭',
top_right: '╮',
bottom_left: '╰',
bottom_right: '╯',
horizontal: '─',
vertical: '│',
}
}
}
/// Default values used as reference for the menu. These values are set during
/// the initial declaration of the menu and are always kept as reference for the
/// changeable [`IdeMenuDetails`] values.
struct DefaultIdeMenuDetails {
/// Minimum width of the menu
pub min_width: usize,
/// Padding to the left and right of the suggestions
pub padding: usize,
/// Whether the menu has a border or not
pub border: Option<BorderSymbols>,
/// Horizontal offset from the cursor.
/// 0 means the top left corner of the menu is below the cursor
pub cursor_offset: i16,
/// How the description is shown
pub description_mode: DescriptionMode,
/// Max width of the description, including the border
pub max_description_width: usize,
/// Max height of the description, including the border
pub max_description_height: usize,
/// Offset from the suggestion box to the description box
pub description_offset: usize,
}
impl Default for DefaultIdeMenuDetails {
fn default() -> Self {
Self {
min_width: 0,
padding: 0,
border: None,
cursor_offset: 0,
description_mode: DescriptionMode::PreferRight,
max_description_width: 50,
max_description_height: 5,
description_offset: 1,
}
}
}
#[derive(Default)]
struct IdeMenuDetails {
// Width of the menu, including the padding and border and the description
pub menu_width: usize,
/// width of the completion box, including the padding and border
pub completion_width: usize,
/// width of the description box, including the padding and border
pub description_width: usize,
/// Where the discription box should be shown based on the description mode
/// and the available space
pub description_is_right: bool,
/// Distance from the left side of the terminal to the menu
pub left_distance: usize,
/// Distance from the right side of the terminal to the menu
pub right_distance: usize,
}
/// Menu to present suggestions like similar to Ide completion menus
pub struct IdeMenu {
/// Menu name
name: String,
/// Ide menu active status
active: bool,
/// Menu coloring
color: MenuTextStyle,
/// Default ide menu details that are set when creating the menu
/// These values are the reference for the working details
default_details: DefaultIdeMenuDetails,
/// Working ide menu details keep changing based on the collected values
working_details: IdeMenuDetails,
/// Menu cached values
values: Vec<Suggestion>,
/// Selected value. Starts at 0
selected: u16,
/// Menu marker when active
marker: String,
/// Event sent to the menu
event: Option<MenuEvent>,
/// Longest suggestion found in the values
longest_suggestion: usize,
/// String collected after the menu is activated
input: Option<String>,
/// Calls the completer using only the line buffer difference difference
/// after the menu was activated
only_buffer_difference: bool,
}
impl Default for IdeMenu {
fn default() -> Self {
Self {
name: "ide_completion_menu".to_string(),
active: false,
color: MenuTextStyle::default(),
default_details: DefaultIdeMenuDetails::default(),
working_details: IdeMenuDetails::default(),
values: Vec::new(),
selected: 0,
marker: "| ".to_string(),
event: None,
longest_suggestion: 0,
input: None,
only_buffer_difference: false,
}
}
}
impl IdeMenu {
/// Menu builder with new name
#[must_use]
pub fn with_name(mut self, name: &str) -> Self {
self.name = name.into();
self
}
/// Menu builder with new value for text style
#[must_use]
pub fn with_text_style(mut self, text_style: Style) -> Self {
self.color.text_style = text_style;
self
}
/// Menu builder with new value for text style
#[must_use]
pub fn with_selected_text_style(mut self, selected_text_style: Style) -> Self {
self.color.selected_text_style = selected_text_style;
self
}
/// Menu builder with new value for text style
#[must_use]
pub fn with_description_text_style(mut self, description_text_style: Style) -> Self {
self.color.description_style = description_text_style;
self
}
/// Menu builder with new value for width value
#[must_use]
pub fn with_width(mut self, width: usize) -> Self {
self.default_details.min_width = width;
self
}
/// Menu builder with new value for padding value
#[must_use]
pub fn with_padding(mut self, padding: usize) -> Self {
self.default_details.padding = padding;
self
}
/// Menu builder with the default border value
#[must_use]
pub fn with_default_border(mut self) -> Self {
self.default_details.border = Some(BorderSymbols::default());
self
}
/// Menu builder with new value for border value
#[must_use]
pub fn with_border(
mut self,
top_right: char,
top_left: char,
bottom_right: char,
bottom_left: char,
horizontal: char,
vertical: char
) -> Self {
self.default_details.border = Some(BorderSymbols {
top_right,
top_left,
bottom_right,
bottom_left,
horizontal,
vertical,
});
self
}
/// Menu builder with new value for cursor offset value
#[must_use]
pub fn with_cursor_offset(mut self, cursor_offset: i16) -> Self {
self.default_details.cursor_offset = cursor_offset;
self
}
/// Menu builder with marker
#[must_use]
pub fn with_marker(mut self, marker: String) -> Self {
self.marker = marker;
self
}
/// Menu builder with new only buffer difference
#[must_use]
pub fn with_only_buffer_difference(mut self, only_buffer_difference: bool) -> Self {
self.only_buffer_difference = only_buffer_difference;
self
}
/// Menu builder with new description mode
#[must_use]
pub fn with_description_mode(mut self, description_mode: DescriptionMode) -> Self {
self.default_details.description_mode = description_mode;
self
}
/// Menu builder with new max description width
#[must_use]
pub fn with_max_description_width(mut self, max_description_width: usize) -> Self {
self.default_details.max_description_width = max_description_width;
self
}
/// Menu builder with new max description height
#[must_use]
pub fn with_max_description_height(mut self, max_description_height: usize) -> Self {
self.default_details.max_description_height = max_description_height;
self
}
}
// Menu functionality
impl IdeMenu {
fn move_next(&mut self) {
if self.selected < (self.values.len() as u16).saturating_sub(1) {
self.selected += 1;
} else {
self.selected = 0;
}
}
fn move_previous(&mut self) {
if self.selected > 0 {
self.selected -= 1;
} else {
self.selected = self.values.len().saturating_sub(1) as u16;
}
}
fn index(&self) -> usize {
self.selected as usize
}
fn get_value(&self) -> Option<Suggestion> {
self.values.get(self.index()).cloned()
}
/// Calculates how many rows the Menu will use
fn get_rows(&self) -> u16 {
let mut values = self.get_values().len() as u16;
if values == 0 {
// When the values are empty the no_records_msg is shown, taking 1 line
return 1;
}
if self.default_details.border.is_some() {
// top and bottom border take 1 line each
values += 2;
}
let descripion_height = self.get_value()
.and_then(|value| value.description)
.map(|description| self.description_dims(description).1 as u16)
.unwrap_or(0);
values.max(descripion_height)
}
/// Returns working details width
fn get_width(&self) -> usize {
self.working_details.menu_width
}
fn reset_position(&mut self) {
self.selected = 0;
}
fn no_records_msg(&self, use_ansi_coloring: bool) -> String {
let msg = "NO RECORDS FOUND";
if use_ansi_coloring {
format!(
"{}{}{}",
self.color.selected_text_style.prefix(),
msg,
RESET
)
} else {
msg.to_string()
}
}
fn create_description(
&self,
description: String,
use_ansi_coloring: bool
) -> Vec<String> {
let max_width = self.default_details.max_description_width;
let max_height = self.default_details.max_description_height;
let content_width = max_width.saturating_sub(if self.default_details.border.is_some() { 2 } else { 0 });
let mut description_lines = split_string(&description, content_width);
let needs_padding = description_lines.len() > 1;
if let Some(border) = &self.default_details.border {
let horizontal_border = border.horizontal.to_string()
.repeat(if needs_padding {
content_width
} else {
description_lines[0].chars().count()
});
for line in &mut description_lines {
let padding = if needs_padding {
" ".repeat(content_width.saturating_sub(line.chars().count()))
} else {
String::new()
};
if use_ansi_coloring {
*line = format!("{}{}{}{}{}{}", border.vertical, self.color.description_style.prefix(), line, padding, RESET, border.vertical);
} else {
*line = format!("{}{}{}{}", border.vertical, line, padding, border.vertical);
}
}
description_lines.insert(0, format!("{}{}{}", border.top_left, horizontal_border, border.top_right));
description_lines.push(format!("{}{}{}", border.bottom_left, horizontal_border, border.bottom_right));
} else {
for line in &mut description_lines {
let padding = if needs_padding {
" ".repeat(content_width.saturating_sub(line.len()))
} else {
String::new()
};
if use_ansi_coloring {
*line = format!("{}{}{}{}", self.color.description_style.prefix(), line, padding, RESET);
} else {
*line = format!("{}{}", line, padding);
}
}
}
// TODO: cut off the description if it is too long
description_lines
}
/// Returns width and height of the description, including the border
fn description_dims(&self, description: String) -> (u16, u16) {
let lines = self.create_description(description, false);
let height = lines.len() as u16;
let string = lines.get(0).cloned().unwrap_or_default();
let width = strip_ansi(&string).chars().count() as u16;
(width, height)
}
fn create_value_string(
&self,
suggestion: &Suggestion,
index: usize,
use_ansi_coloring: bool
) -> String {
let vertical_border = self.default_details.border.as_ref().map(|border| border.vertical).unwrap_or_default();
let padding_right = self.working_details.completion_width.saturating_sub(suggestion.value.len()).saturating_sub(if self.default_details.border.is_some() { 2 } else { 0 });
if use_ansi_coloring {
if index == self.index() {
format!(
"{}{}{}{}{}{}{}",
vertical_border,
self.color.selected_text_style.prefix(),
" ".repeat(self.default_details.padding),
suggestion.value,
" ".repeat(padding_right),
RESET,
vertical_border,
)
} else {
format!(
"{}{}{}{}{}{}{}",
vertical_border,
self.color.text_style.prefix(),
" ".repeat(self.default_details.padding),
suggestion.value,
" ".repeat(padding_right),
RESET,
vertical_border,
)
}
} else {
let marker = if index == self.index() { ">" } else { "" };
format!(
"{}{}{}{}{}",
vertical_border,
marker,
suggestion.value,
" ".repeat(padding_right),
vertical_border,
)
}
}
}
impl Menu for IdeMenu {
/// Menu name
fn name(&self) -> &str {
self.name.as_str()
}
/// Menu indicator
fn indicator(&self) -> &str {
self.marker.as_str()
}
/// Deactivates context menu
fn is_active(&self) -> bool {
self.active
}
/// The ide menu can to quick complete if there is only one element
fn can_quick_complete(&self) -> bool {
true
}
fn can_partially_complete(
&mut self,
values_updated: bool,
editor: &mut Editor,
completer: &mut dyn Completer,
) -> bool {
// If the values were already updated (e.g. quick completions are true)
// there is no need to update the values from the menu
if !values_updated {
self.update_values(editor, completer);
}
let values = self.get_values();
if let (Some(Suggestion { value, span, .. }), Some(index)) = find_common_string(values) {
let index = index.min(value.len());
let matching = &value[0..index];
// make sure that the partial completion does not overwrite user entered input
let extends_input = matching.starts_with(&editor.get_buffer()[span.start..span.end]);
if !matching.is_empty() && extends_input {
let mut line_buffer = editor.line_buffer().clone();
line_buffer.replace_range(span.start..span.end, matching);
let offset = if matching.len() < (span.end - span.start) {
line_buffer
.insertion_point()
.saturating_sub((span.end - span.start) - matching.len())
} else {
line_buffer.insertion_point() + matching.len() - (span.end - span.start)
};
line_buffer.set_insertion_point(offset);
editor.set_line_buffer(line_buffer, UndoBehavior::CreateUndoPoint);
// The values need to be updated because the spans need to be
// recalculated for accurate replacement in the string
self.update_values(editor, completer);
true
} else {
false
}
} else {
false
}
}
/// Selects what type of event happened with the menu
fn menu_event(&mut self, event: MenuEvent) {
match &event {
MenuEvent::Activate(_) => self.active = true,
MenuEvent::Deactivate => {
self.active = false;
self.input = None;
}
_ => {}
}
self.event = Some(event);
}
/// Update menu values
fn update_values(&mut self, editor: &mut Editor, completer: &mut dyn Completer) {
if self.only_buffer_difference {
if let Some(old_string) = &self.input {
let (start, input) = string_difference(editor.get_buffer(), old_string);
if !input.is_empty() {
self.values = completer.complete(input, start);
self.reset_position();
}
}
} else {
// If there is a new line character in the line buffer, the completer
// doesn't calculate the suggested values correctly. This happens when
// editing a multiline buffer.
// Also, by replacing the new line character with a space, the insert
// position is maintain in the line buffer.
let trimmed_buffer = editor.get_buffer().replace('\n', " ");
self.values = completer.complete(trimmed_buffer.as_str(), editor.insertion_point());
self.reset_position();
}
}
/// The working details for the menu changes based on the size of the lines
/// collected from the completer
fn update_working_details(
&mut self,
editor: &mut Editor,
completer: &mut dyn Completer,
painter: &Painter,
) {
if let Some(event) = self.event.take() {
// The working value for the menu are updated first before executing any of the
match event {
MenuEvent::Activate(updated) => {
self.active = true;
self.reset_position();
self.input = if self.only_buffer_difference {
Some(editor.get_buffer().to_string())
} else {
None
};
if !updated {
self.update_values(editor, completer);
}
}
MenuEvent::Deactivate => self.active = false,
MenuEvent::Edit(updated) => {
self.reset_position();
if !updated {
self.update_values(editor, completer);
}
}
MenuEvent::NextElement | MenuEvent::MoveDown => self.move_next(),
MenuEvent::PreviousElement | MenuEvent::MoveUp => self.move_previous(),
MenuEvent::MoveLeft | MenuEvent::MoveRight | MenuEvent::PreviousPage | MenuEvent::NextPage => {}
}
self.longest_suggestion = self.get_values().iter().fold(0, |prev, suggestion| {
if prev >= suggestion.value.len() {
prev
} else {
suggestion.value.len()
}
});
let terminal_width = painter.screen_width();
let cursor_pos = crossterm::cursor::position().unwrap().0;
self.working_details.description_width = self.get_value()
.and_then(|value| value.description)
.map(|description| self.description_dims(description).0)
.unwrap_or(0) as usize;
self.working_details.completion_width = self.longest_suggestion + self.default_details.padding * 2 + if self.default_details.border.is_some() {
2
} else {
0
}.max(self.default_details.min_width);
self.working_details.menu_width = self.working_details.completion_width + self.working_details.description_width + if self.working_details.description_width > 0 {
self.default_details.description_offset
} else {
0
};
self.working_details.description_is_right = match self.default_details.description_mode {
DescriptionMode::Left => false,
DescriptionMode::PreferRight => {
// if there is enough space to the right of the cursor, the description is shown on the right
// otherwise it is shown on the left
let potential_right_distance = (terminal_width as i16).saturating_sub(cursor_pos as i16 + self.default_details.cursor_offset + self.default_details.description_offset as i16 + self.working_details.completion_width as i16).max(0) as usize ;
if potential_right_distance >= self.working_details.description_width + self.default_details.description_offset {
true
} else {
false
}
},
DescriptionMode::Right => true,
};
if self.working_details.description_is_right {
let potential_left_distance = cursor_pos as i16 + self.default_details.cursor_offset;
let left_distance = potential_left_distance.clamp(0, terminal_width.saturating_sub(self.get_width() as u16) as i16);
let right_distance = (terminal_width as usize).saturating_sub(left_distance as usize + self.get_width());
self.working_details.left_distance = left_distance as usize;
self.working_details.right_distance = right_distance;
} else {
let potential_left_distance = cursor_pos as i16 + self.default_details.cursor_offset - self.working_details.description_width as i16 - self.default_details.description_offset as i16;
let left_distance = potential_left_distance.clamp(0, terminal_width.saturating_sub(self.get_width() as u16) as i16);
let right_distance = (terminal_width as usize).saturating_sub(left_distance as usize + self.get_width());
self.working_details.left_distance = left_distance as usize;
self.working_details.right_distance = right_distance;
}
}
}
/// The buffer gets replaced in the Span location
fn replace_in_buffer(&self, editor: &mut Editor) {
if let Some(Suggestion {
mut value,
span,
append_whitespace,
..
}) = self.get_value()
{
let start = span.start.min(editor.line_buffer().len());
let end = span.end.min(editor.line_buffer().len());
if append_whitespace {
value.push(' ');
}
let mut line_buffer = editor.line_buffer().clone();
line_buffer.replace_range(start..end, &value);
let mut offset = line_buffer.insertion_point();
offset = offset.saturating_add(value.len());
offset = offset.saturating_sub(end.saturating_sub(start));
line_buffer.set_insertion_point(offset);
editor.set_line_buffer(line_buffer, UndoBehavior::CreateUndoPoint);
}
}
/// Minimum rows that should be displayed by the menu
fn min_rows(&self) -> u16 {
self.get_rows()
}
fn get_values(&self) -> &[Suggestion] {
&self.values
}
fn menu_required_lines(&self, _terminal_columns: u16) -> u16 {
self.get_rows()
}
fn menu_string(&self, available_lines: u16, use_ansi_coloring: bool) -> String {
if self.get_values().is_empty() {
self.no_records_msg(use_ansi_coloring)
} else {
// The skip values represent the number of lines that should be skipped
// while printing the menu
let skip_values = if self.selected >= available_lines {
let skip_lines = self.selected.saturating_sub(available_lines) + 1;
skip_lines as usize
} else {
0
};
let available_values = self.get_values().len();
let mut strings = self.get_values()
.iter()
.skip(skip_values)
.take(available_values)
.enumerate()
.map(|(index, suggestion)| {
// Correcting the enumerate index based on the number of skipped values
let index = index + skip_values;
self.create_value_string(suggestion, index, use_ansi_coloring)
})
.collect::<Vec<String>>();
if let Some(border) = &self.default_details.border {
let inner_width = self.working_details.completion_width.saturating_sub(2);
strings.insert(0, format!(
"{}{}{}",
border.top_left,
border.horizontal.to_string().repeat(inner_width),
border.top_right,
));
strings.push(format!(
"{}{}{}",
border.bottom_left,
border.horizontal.to_string().repeat(inner_width),
border.bottom_right,
));
}
let description_lines = self.get_value()
.and_then(|value| value.clone().description)
.map(|description| self.create_description(description, use_ansi_coloring))
.unwrap_or_default();
let padding_left = &" ".repeat(self.working_details.left_distance);
// vertically join the description lines with the suggestion lines
if self.working_details.description_is_right {
for (idx, pair) in strings.clone().iter().zip_longest(description_lines.iter()).enumerate() {
match pair {
Both(_suggestion_line, description_line) => {
strings[idx] = format!(
"{}{}{}{}",
padding_left,
strings[idx],
" ".repeat(self.default_details.description_offset),
description_line,
)
},
Left(suggestion_line) => {
strings[idx] = format!(
"{}{}",
padding_left,
suggestion_line,
)
},
Right(description_line) => {
strings.push(format!(
"{}{}",
" ".repeat(self.working_details.completion_width + self.default_details.description_offset) + padding_left,
description_line,
))
}
}
}
} else {
for (idx, pair) in strings.clone().iter().zip_longest(description_lines.iter()).enumerate() {
match pair {
Both(suggestion_line, description_line) => {
strings[idx] = format!(
"{}{}{}{}",
padding_left,
description_line,
" ".repeat(self.default_details.description_offset),
suggestion_line,
)
},
Left(suggestion_line) => {
strings[idx] = format!(
"{}{}",
" ".repeat(self.working_details.description_width + self.default_details.description_offset) + padding_left,
suggestion_line,
)
},
Right(description_line) => {
strings.push(format!(
"{}{}",
padding_left,
description_line,
))
}
}
}
}
strings.join("\r\n")
}
}
}
/// Split the input into strings that are at most `max_width` long
/// The split is done at spaces if possible
fn split_string(input: &str, max_width: usize) -> Vec<String> {
let words: Vec<&str> = input.split_whitespace().collect();
let mut result = Vec::new();
let mut current_line = String::new();
for word in words {
if word.len() > max_width {
let chars: Vec<char> = word.chars().collect();
let mut i = 0;
while i < chars.len() {
let end = usize::min(i + max_width, chars.len());
result.push(chars[i..end].iter().collect());
i = end;
}
} else if current_line.len() + word.len() + 1 > max_width {
if !current_line.is_empty() {
result.push(current_line.trim_end().to_string());
}
current_line = String::from(word);
} else {
if !current_line.is_empty() {
current_line.push(' ');
}
current_line.push_str(word);
}
}
if !current_line.is_empty() {
result.push(current_line.trim_end().to_string());
}
result
}