mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-27 19:26:09 +00:00
Unify prefix_length and suffix_length signatures
This commit is contained in:
parent
d12622b56a
commit
be7f2184e9
@ -34,9 +34,9 @@ pub use self::reader::ProgressReader;
|
|||||||
/// prefix_length(my_text.graphemes(true), 5, "");
|
/// prefix_length(my_text.graphemes(true), 5, "");
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn prefix_length<'a, I: Iterator<Item = &'a str>>(iter: I, width: usize,
|
pub fn prefix_length<'a, I>(iter: I, width: usize, delimiter: &str) -> usize
|
||||||
delimiter: &str)
|
where I: Iterator<Item = &'s str>
|
||||||
-> usize {
|
{
|
||||||
let delimiter_width = delimiter.width();
|
let delimiter_width = delimiter.width();
|
||||||
let delimiter_len = delimiter.len();
|
let delimiter_len = delimiter.len();
|
||||||
|
|
||||||
@ -64,19 +64,21 @@ pub fn prefix_length<'a, I: Iterator<Item = &'a str>>(iter: I, width: usize,
|
|||||||
|
|
||||||
/// Computes the length of a suffix that fits in the given `width`.
|
/// Computes the length of a suffix that fits in the given `width`.
|
||||||
///
|
///
|
||||||
|
/// Doesn't break inside elements returned by `iter`.
|
||||||
|
///
|
||||||
/// Returns the number of bytes of the longest
|
/// Returns the number of bytes of the longest
|
||||||
/// suffix from `text` that fits in `width`.
|
/// suffix from `text` that fits in `width`.
|
||||||
pub fn suffix_length(text: &str, width: usize) -> usize {
|
///
|
||||||
text.graphemes(true)
|
/// This is a shortcut for `prefix_length(iter.rev(), width, delimiter)`
|
||||||
.rev()
|
pub fn suffix_length<'a, I>(iter: I, width: usize, delimiter: &str) -> usize
|
||||||
.scan(0, |w, g| {
|
where I: DoubleEndedIterator<Item = &'a str>
|
||||||
*w += g.width();
|
{
|
||||||
if *w > width {
|
prefix_length(iter.rev(), width, delimiter)
|
||||||
None
|
}
|
||||||
} else {
|
|
||||||
Some(g)
|
/// Computes the length of a suffix that fits in the given `width`.
|
||||||
}
|
///
|
||||||
})
|
/// Breaks between any two graphemes.
|
||||||
.map(|g| g.len())
|
pub fn simple_suffix_length(text: &str, width: usize) -> usize {
|
||||||
.fold(0, |a, b| a + b)
|
suffix_length(text.graphemes(true), width, "")
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ use theme::{ColorStyle, Effect};
|
|||||||
use vec::Vec2;
|
use vec::Vec2;
|
||||||
use view::View;
|
use view::View;
|
||||||
use event::{Callback, Event, EventResult, Key};
|
use event::{Callback, Event, EventResult, Key};
|
||||||
use utils::suffix_length;
|
use utils::simple_suffix_length;
|
||||||
|
|
||||||
|
|
||||||
/// Input box where the user can enter and edit text.
|
/// Input box where the user can enter and edit text.
|
||||||
@ -380,7 +380,7 @@ impl View for EditView {
|
|||||||
// From the end, count the length until we reach `available`.
|
// From the end, count the length until we reach `available`.
|
||||||
// Then sum the byte lengths.
|
// Then sum the byte lengths.
|
||||||
let suffix_length =
|
let suffix_length =
|
||||||
suffix_length(&self.content[self.offset..self.cursor],
|
simple_suffix_length(&self.content[self.offset..self.cursor],
|
||||||
available);
|
available);
|
||||||
self.offset = self.cursor - suffix_length;
|
self.offset = self.cursor - suffix_length;
|
||||||
assert!(self.cursor >= self.offset);
|
assert!(self.cursor >= self.offset);
|
||||||
@ -389,7 +389,7 @@ impl View for EditView {
|
|||||||
|
|
||||||
// If we have too much space
|
// If we have too much space
|
||||||
if self.content[self.offset..].width() < self.last_length {
|
if self.content[self.offset..].width() < self.last_length {
|
||||||
let suffix_length = suffix_length(&self.content,
|
let suffix_length = simple_suffix_length(&self.content,
|
||||||
self.last_length - 1);
|
self.last_length - 1);
|
||||||
self.offset = self.content.len() - suffix_length;
|
self.offset = self.content.len() - suffix_length;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user