cursive_extras/
lib.rs

1#![doc = include_str!("../README.md")]
2
3use cursive_core::utils::span::{SpannedStr, SpannedString};
4use std::str::Chars;
5use unicode_width::UnicodeWidthStr;
6
7mod views;
8mod funcs;
9mod macros;
10
11pub use views::*;
12pub use funcs::*;
13
14#[cfg(test)]
15mod tests;
16
17// cursive reexport for the macros
18#[doc(hidden)]
19pub use cursive_core as siv;
20
21/// [`SpannedString`]: cursive_core::utils::span::SpannedString
22/// [`SpannedStr`]: cursive_core::utils::span::SpannedStr
23/// [`StyledString`]: cursive_core::utils::markup::StyledString
24///
25/// Extension trait for [`SpannedString`] and [`SpannedStr`]
26///
27/// These methods might be useful when working with [`StyledString`]s
28pub trait SpannedStrExt<T>: Clone {
29    /// The amount of UTF-8 characters in the source string
30    fn char_len(&self) -> usize;
31
32    /// Iterate over the characters in the source string
33    ///
34    /// Disclaimer: some special characters are actually multiple characters
35    /// (like accented letters)
36    fn chars(&self) -> Chars;
37
38    /// Convert the [`SpannedStr`] back to a [`SpannedString`]
39    fn to_spanned_string(&self) -> SpannedString<T>;
40
41    /// Creates a [`SpannedStr`] reference from a [`SpannedString`]
42    fn as_spanned_str(&self) -> SpannedStr<T>;
43}
44
45impl<T: Clone> SpannedStrExt<T> for SpannedString<T> {
46    fn char_len(&self) -> usize { self.source().width() }
47    fn chars(&self) -> Chars { self.source().chars() }
48    fn to_spanned_string(&self) -> SpannedString<T> { self.clone() }
49    fn as_spanned_str(&self) -> SpannedStr<T> { SpannedStr::new(self.source(), self.spans_raw()) }
50}
51
52impl<'a, T: Clone> SpannedStrExt<T> for SpannedStr<'a, T> {
53    fn char_len(&self) -> usize { self.source().width() }
54    fn chars(&self) -> Chars { self.source().chars() }
55    fn to_spanned_string(&self) -> SpannedString<T> { SpannedString::with_spans(self.source(), self.spans_raw().to_vec()) }
56    fn as_spanned_str(&self) -> SpannedStr<T> { SpannedStr::new(self.source(), self.spans_raw()) }
57}
OSZAR »