зеркало из
https://github.com/glebtv/tui-textarea.git
synced 2026-09-07 16:05:50 +03:00
implement FromIterator for TextArea
Этот коммит содержится в:
@@ -161,6 +161,14 @@ let mut text: String = ...;
|
|||||||
let mut textarea = TextARea::from(text.lines());
|
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`
|
### Get text contents from `TextArea`
|
||||||
|
|
||||||
`TextArea::lines()` returns text lines as `&[String]`. It borrows text contents temporarily.
|
`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> {
|
impl<'a> Default for TextArea<'a> {
|
||||||
/// Create [`TextArea`] instance with empty text content.
|
/// Create [`TextArea`] instance with empty text content.
|
||||||
/// ```
|
/// ```
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user