1
0
зеркало из https://github.com/glebtv/tui-textarea.git synced 2026-09-03 14:26:17 +03:00

fix inser_str doesn't handle \r\n and ignores newline at end of input

Этот коммит содержится в:
rhysd
2023-11-19 22:08:25 +09:00
родитель 1194331a02
Коммит 9e3df38b54
2 изменённых файлов: 70 добавлений и 7 удалений

Просмотреть файл

@@ -748,6 +748,7 @@ impl<'a> TextArea<'a> {
}
/// Insert a string at current cursor position. This method returns if some text was inserted or not in the textarea.
/// Both `\n` and `\r\n` are recognized as newlines but `\r` isn't.
/// ```
/// use tui_textarea::TextArea;
///
@@ -761,7 +762,11 @@ impl<'a> TextArea<'a> {
/// ```
pub fn insert_str<S: AsRef<str>>(&mut self, s: S) -> bool {
let modified = self.delete_selection(false);
let mut lines: Vec<_> = s.as_ref().lines().map(ToString::to_string).collect();
let mut lines: Vec<_> = s
.as_ref()
.split('\n')
.map(|s| s.strip_suffix('\r').unwrap_or(s).to_string())
.collect();
match lines.len() {
0 => modified,
1 => self.insert_piece(lines.remove(0)),