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

implement FromIterator for TextArea

Этот коммит содержится в:
rhysd
2022-06-14 22:15:38 +09:00
родитель c1598c5600
Коммит c019f10431
2 изменённых файлов: 28 добавлений и 0 удалений

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

@@ -161,6 +161,14 @@ let mut text: String = ...;
let mut textarea = TextARea::from(text.lines());
```
`TextArea` also implements `FromIterator<impl Into<String>>`. `Iterator::collect()` can collect strings as an editor
instance. This allows to create `TextArea` reading lines from file efficiently using `io::BufReader`.
```rust
let file = fs::File::open(path)?;
let mut textarea: TextArea = io::BufReader::new(file).lines().collect::<io::Result<_>>()?;
```
### Get text contents from `TextArea`
`TextArea::lines()` returns text lines as `&[String]`. It borrows text contents temporarily.

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

@@ -77,6 +77,26 @@ where
}
}
/// Collect line texts from iterator as [`TextArea`]. It is useful when creating a textarea with text read from a file.
/// [`Iterator::collect`] handles errors which may happen on reading each lines. The following example reads text from
/// a file efficiently line-by-line.
/// ```no_run
/// use std::fs;
/// use std::io::{self, BufRead};
/// use std::path::Path;
/// use tui_textarea::TextArea;
///
/// fn read_from_file<'a>(path: impl AsRef<Path>) -> io::Result<TextArea<'a>> {
/// let file = fs::File::open(path.as_ref())?;
/// io::BufReader::new(file).lines().collect()
/// }
/// ```
impl<'a, S: Into<String>> FromIterator<S> for TextArea<'a> {
fn from_iter<I: IntoIterator<Item = S>>(iter: I) -> Self {
iter.into()
}
}
impl<'a> Default for TextArea<'a> {
/// Create [`TextArea`] instance with empty text content.
/// ```