use ratatui::style::Style;
use ratatui::text::Span;
use unicode_width::UnicodeWidthChar;
pub fn text_field_spans(
value: &str,
cursor: usize,
visible: bool,
value_style: Style,
cursor_style: Style,
) -> [Span<'static>; 3] {
let (pre, rest) = split_at_char(value, cursor);
let mut rest_chars = rest.chars();
let under = rest_chars.next();
let post: String = rest_chars.collect();
let cell_style = if visible { cursor_style } else { value_style };
[
Span::styled(pre, value_style),
Span::styled(under.unwrap_or(' ').to_string(), cell_style),
Span::styled(post, value_style),
]
}
pub fn scroll_field(value: &str, cursor: usize, scroll: usize, width: usize) -> (usize, String) {
let chars: Vec<char> = value.chars().collect();
let cursor = cursor.min(chars.len());
let cells = |cs: &[char]| -> usize { cs.iter().map(|c| c.width().unwrap_or(0)).sum() };
let cursor_cell = chars
.get(cursor)
.map_or(1, |c| c.width().unwrap_or(0).max(1));
let mut scroll = scroll.min(cursor);
while scroll < cursor && cells(&chars[scroll..cursor]) + cursor_cell > width {
scroll += 1;
}
while scroll > 0 && cells(&chars[scroll - 1..]) < width {
scroll -= 1;
}
let mut used = 0;
let visible = chars[scroll..]
.iter()
.take_while(|c| {
used += c.width().unwrap_or(0);
used <= width
})
.collect();
(scroll, visible)
}
pub fn scrolled_field_spans(
value: &str,
cursor: usize,
scroll: &mut usize,
width: usize,
visible: bool,
value_style: Style,
cursor_style: Style,
) -> Vec<Span<'static>> {
let cursor = cursor.min(value.chars().count());
let (new_scroll, shown) = scroll_field(value, cursor, *scroll, width);
*scroll = new_scroll;
let mut spans = Vec::from(text_field_spans(
&shown,
cursor - new_scroll,
visible,
value_style,
cursor_style,
));
let used: usize = spans.iter().map(Span::width).sum();
let pad = width.saturating_sub(used);
if pad > 0 {
spans.push(Span::styled(" ".repeat(pad), value_style));
}
spans
}
pub fn insert_char_at(s: &mut String, cursor: usize, ch: char) {
let byte_idx = s
.char_indices()
.nth(cursor)
.map(|(b, _)| b)
.unwrap_or(s.len());
s.insert(byte_idx, ch);
}
pub fn remove_char_at(s: &mut String, cursor: usize) {
if let Some((byte_idx, ch)) = s.char_indices().nth(cursor) {
s.replace_range(byte_idx..byte_idx + ch.len_utf8(), "");
}
}
fn split_at_char(s: &str, cursor: usize) -> (String, String) {
let byte_idx = s
.char_indices()
.nth(cursor)
.map(|(b, _)| b)
.unwrap_or(s.len());
(s[..byte_idx].to_owned(), s[byte_idx..].to_owned())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn split_at_char_mid_and_past_end() {
assert_eq!(split_at_char("hello", 2), ("he".into(), "llo".into()));
assert_eq!(split_at_char("hi", 5), ("hi".into(), String::new()));
}
#[test]
fn split_at_char_respects_char_boundaries() {
assert_eq!(split_at_char("é!", 1), ("é".into(), "!".into()));
}
#[test]
fn text_field_slot_is_constant_width_across_blink() {
let vis = text_field_spans("note", 2, true, Style::default(), Style::default());
let hid = text_field_spans("note", 2, false, Style::default(), Style::default());
let width = |spans: &[Span<'static>]| -> usize {
spans.iter().map(|s| s.content.chars().count()).sum()
};
assert_eq!(width(&vis), width(&hid));
assert_eq!(vis[1].content.as_ref(), "t");
assert_eq!(hid[1].content.as_ref(), "t");
}
#[test]
fn text_field_cursor_past_end_is_a_space_cell() {
let spans = text_field_spans("hi", 2, true, Style::default(), Style::default());
let text: String = spans.iter().map(|s| s.content.as_ref()).collect();
assert_eq!(text, "hi ");
assert_eq!(spans[1].content.as_ref(), " ");
assert!(spans[2].content.is_empty());
}
#[test]
fn scroll_field_leaves_a_fitting_value_unscrolled() {
assert_eq!(scroll_field("hello", 5, 0, 10), (0, "hello".into()));
assert_eq!(scroll_field("hello", 0, 0, 10), (0, "hello".into()));
}
#[test]
fn scroll_field_keeps_the_end_of_text_cursor_cell_on_screen() {
assert_eq!(scroll_field("abcdefgh", 8, 0, 5), (4, "efgh".into()));
}
#[test]
fn scroll_field_moves_only_as_far_as_the_cursor_forces() {
assert_eq!(scroll_field("abcdefgh", 5, 4, 5), (4, "efgh".into()));
assert_eq!(scroll_field("abcdefgh", 2, 4, 5), (2, "cdefg".into()));
}
#[test]
fn scroll_field_slides_back_when_the_text_shrinks() {
assert_eq!(scroll_field("abcdef", 6, 5, 5), (2, "cdef".into()));
}
#[test]
fn scroll_field_counts_wide_chars_as_two_cells() {
assert_eq!(scroll_field("日本語", 3, 0, 5), (1, "本語".into()));
}
#[test]
fn insert_and_remove_char_at_respect_char_boundaries() {
let mut s = "é!".to_owned();
insert_char_at(&mut s, 1, 'x');
assert_eq!(s, "éx!");
remove_char_at(&mut s, 0);
assert_eq!(s, "x!");
remove_char_at(&mut s, 9);
assert_eq!(s, "x!");
}
#[test]
fn scrolled_field_spans_pad_to_the_full_width() {
let width = |spans: &[Span<'static>]| -> usize { spans.iter().map(Span::width).sum() };
let mut scroll = 0;
let short = scrolled_field_spans(
"hi",
2,
&mut scroll,
10,
true,
Style::default(),
Style::default(),
);
assert_eq!(width(&short), 10);
let long = scrolled_field_spans(
"abcdefghijkl",
12,
&mut scroll,
10,
true,
Style::default(),
Style::default(),
);
assert_eq!(width(&long), 10);
assert_eq!(scroll, 3, "the window followed the cursor to the end");
let wide = scrolled_field_spans(
"日本",
1,
&mut 0,
6,
false,
Style::default(),
Style::default(),
);
assert_eq!(width(&wide), 6);
}
}