use super::*;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Selection {
pub anchor: (i32, u16),
pub head: (i32, u16),
pub block: bool,
}
impl Selection {
pub fn new(line: i32, col: u16) -> Self {
Self {
anchor: (line, col),
head: (line, col),
block: false,
}
}
pub fn normalised(&self) -> (i32, u16, i32, u16) {
let (a_l, a_c) = self.anchor;
let (b_l, b_c) = self.head;
if (a_l, a_c) <= (b_l, b_c) {
(a_l, a_c, b_l, b_c)
} else {
(b_l, b_c, a_l, a_c)
}
}
pub fn block_bounds(&self) -> (i32, u16, i32, u16) {
let (a_l, a_c) = self.anchor;
let (b_l, b_c) = self.head;
(a_l.min(b_l), a_c.min(b_c), a_l.max(b_l), a_c.max(b_c))
}
pub fn has_area(&self) -> bool {
self.anchor != self.head
}
}
impl PtyTerminal {
pub(super) fn stamp_selection_clock(&mut self) {
self.sel_scrolled = self.tick_clock();
let term = self.term.lock();
self.alt_sel = if term.mode().contains(TermMode::ALT_SCREEN) {
self.selection.and_then(|s| capture_alt_anchor(&term, s))
} else {
None
};
}
pub(super) fn rebase_alt_selection(&mut self) {
if self.selection.is_none() {
self.alt_sel = None;
return;
}
let Some(mut anchor) = self.alt_sel.take() else {
return;
};
let term = self.term.lock();
let rows_vis = term.screen_lines() as i32;
let old_top = anchor.top;
let k = anchor.rows.len() as i32;
let grid_rows: Vec<(String, Vec<usize>)> =
(0..rows_vis).map(|l| row_text_and_cols(&term, l)).collect();
let k_rows = anchor.rows.len();
let min_exact = k_rows.min(2);
let mut best: Option<(i32, usize, usize)> = None;
for top in 1 - k..rows_vis {
let (mut exact, mut nonblank) = (0usize, false);
for (i, want) in anchor.rows.iter().enumerate() {
let line = top + i as i32;
if (0..rows_vis).contains(&line) && grid_rows[line as usize].0 == *want {
exact += 1;
nonblank |= !want.trim().is_empty();
}
}
let mut ctx = 0usize;
for (want, line) in [
(anchor.ctx_above.as_ref(), top - 1),
(anchor.ctx_below.as_ref(), top + k),
] {
if let Some(want) = want
&& (0..rows_vis).contains(&line)
&& grid_rows[line as usize].0 == *want
{
ctx += 1;
}
}
if exact >= min_exact
&& nonblank
&& best.is_none_or(|(btop, bexact, bctx)| {
exact > bexact
|| (exact == bexact && ctx > bctx)
|| (exact == bexact
&& ctx == bctx
&& (top - old_top).abs() < (btop - old_top).abs())
})
{
best = Some((top, exact, ctx));
}
}
match best {
Some((top, _, _)) => {
let d = top - old_top;
anchor.top = top;
if d != 0
&& let Some(s) = self.selection.as_mut()
{
s.anchor.0 += d;
s.head.0 += d;
}
anchor.dormant = false;
let mut clips: Vec<(i32, u16, u16)> = Vec::new();
let mut all_exact = true;
for (i, want) in anchor.rows.iter().enumerate() {
let line = top + i as i32;
if !(0..rows_vis).contains(&line) {
all_exact = false;
continue;
}
let (got, colmap) = &grid_rows[line as usize];
if got == want {
clips.push((line, 0, u16::MAX));
} else {
all_exact = false;
for (lo, hi) in partial_overlap_cols(want, got, colmap) {
clips.push((line, lo, hi));
}
}
}
anchor.visible = (!all_exact).then_some(clips);
if all_exact && let Some(fresh) = capture_alt_anchor(&term, self.selection.unwrap())
{
anchor = fresh;
}
}
None if self.drag_selecting => {
anchor.dormant = false;
anchor.visible = None;
if let Some(fresh) = capture_alt_anchor(&term, self.selection.unwrap()) {
anchor = fresh;
}
}
None => anchor.dormant = true,
}
drop(term);
self.alt_sel = Some(anchor);
}
pub(super) fn rebase_selection(&mut self) {
if self.selection.is_none() && self.copy_cursor.is_none() {
return;
}
if self.term.lock().mode().contains(TermMode::ALT_SCREEN) {
self.rebase_alt_selection();
return;
}
if self.alt_sel.take().is_some() {
self.selection = None;
self.copy_cursor = None;
return;
}
let now = self.tick_clock();
let top = self.term.lock().grid().topmost_line().0;
let delta = (now - self.sel_scrolled) as i32;
self.sel_scrolled = now;
if delta != 0 {
if let Some(sel) = self.selection.as_mut() {
sel.anchor.0 -= delta;
sel.head.0 -= delta;
}
if let Some(cur) = self.copy_cursor.as_mut() {
cur.0 -= delta;
}
}
if self
.selection
.is_some_and(|s| s.anchor.0 < top && s.head.0 < top)
{
self.selection = None;
} else if let Some(sel) = self.selection.as_mut() {
sel.anchor.0 = sel.anchor.0.max(top);
sel.head.0 = sel.head.0.max(top);
}
if let Some(cur) = self.copy_cursor.as_mut() {
cur.0 = cur.0.max(top);
}
}
pub fn start_selection_at(&mut self, col: u16, row: u16) {
if let Some((r, c)) = self.cell_at(col, row) {
let line = r as i32 - self.display_offset();
self.selection = Some(Selection::new(line, c));
self.drag_selecting = true;
self.stamp_selection_clock();
}
}
#[cfg(test)]
pub fn debug_sel_state(&self) -> (Option<Selection>, i64, i64, Option<i32>, bool) {
(
self.selection,
self.sel_scrolled,
self.clock.lock().unwrap().base,
self.alt_sel.as_ref().map(|a| a.top),
self.term.lock().mode().contains(TermMode::ALT_SCREEN),
)
}
pub fn end_drag(&mut self) {
if self.drag_selecting {
self.drag_selecting = false;
self.stamp_selection_clock();
}
}
pub fn extend_selection_to(&mut self, col: u16, row: u16) {
self.drag_selecting = true;
self.rebase_selection();
let cell = self.cell_at(col, row);
let off = self.display_offset();
if let (Some(sel), Some((r, c))) = (self.selection.as_mut(), cell) {
sel.head = (r as i32 - off, c);
}
}
pub fn drag_select_to(&mut self, col: u16, row: u16) -> i32 {
let inner = self.last_inner;
if inner.width == 0 || inner.height == 0 {
return 0;
}
self.drag_selecting = true;
let top = inner.y;
let bottom = inner.y + inner.height - 1;
let (vp_row, dir) = if row < top {
(0u16, -1)
} else if row > bottom {
(inner.height - 1, 1)
} else {
(row - inner.y, 0)
};
let max_x = inner.x + inner.width - 1;
let c = col.clamp(inner.x, max_x) - inner.x;
self.rebase_selection();
let off = self.display_offset();
if let Some(sel) = self.selection.as_mut() {
sel.head = (vp_row as i32 - off, c);
}
dir
}
pub fn autoscroll_select(&mut self, dir: i32, col: u16) {
if dir == 0 {
return;
}
if self.term.lock().mode().contains(TermMode::ALT_SCREEN) {
let inner = self.last_inner;
if inner.width == 0 || inner.height == 0 || !self.mouse_reporting() {
return;
}
let edge_row = if dir < 0 {
inner.y
} else {
inner.y + inner.height - 1
};
let ccol = col.clamp(inner.x, inner.x + inner.width - 1);
let button = if dir < 0 {
MouseButtonKind::WheelUp
} else {
MouseButtonKind::WheelDown
};
self.report_mouse(
button,
MouseAction::Press,
ccol,
edge_row,
MouseMods::default(),
);
self.rebase_selection();
if let Some(sel) = self.selection.as_mut() {
sel.head = (i32::from(edge_row - inner.y), ccol - inner.x);
}
return;
}
let scrolled = if dir < 0 {
self.scroll_up(1)
} else {
self.scroll_down(1)
};
if !scrolled {
return;
}
let inner = self.last_inner;
if inner.width == 0 || inner.height == 0 {
return;
}
let max_x = inner.x + inner.width - 1;
let c = col.clamp(inner.x, max_x) - inner.x;
let vp_row = if dir < 0 { 0u16 } else { inner.height - 1 };
self.rebase_selection();
let off = self.display_offset();
if let Some(sel) = self.selection.as_mut() {
sel.head = (vp_row as i32 - off, c);
}
}
pub fn clear_selection(&mut self) {
self.selection = None;
self.alt_sel = None;
}
pub fn select_word_at(&mut self, col: u16, row: u16) {
let Some((r, c)) = self.cell_at(col, row) else {
return;
};
let term = self.term.lock();
let display_offset = term.grid().display_offset();
let Some((anchor, head)) =
select_word_at_in_term(&term, display_offset, r as usize, c as usize)
else {
return;
};
drop(term);
let off = display_offset as i32;
let to_abs = |(vr, vc): (u16, u16)| (vr as i32 - off, vc);
self.selection = Some(Selection {
anchor: to_abs(anchor),
head: to_abs(head),
block: false,
});
self.stamp_selection_clock();
}
pub fn selection(&self) -> Option<Selection> {
let mut sel = self.selection?;
let delta = (self.clock_now(&self.term.lock()) - self.sel_scrolled) as i32;
sel.anchor.0 -= delta;
sel.head.0 -= delta;
Some(sel)
}
pub fn set_selection(&mut self, sel: Option<Selection>) {
self.rebase_selection();
self.selection = sel;
self.stamp_selection_clock();
self.pty_dirty.store(true, Ordering::Release);
}
pub fn set_copy_cursor(&mut self, cur: Option<(i32, u16)>) {
self.rebase_selection();
self.copy_cursor = cur;
self.stamp_selection_clock();
self.pty_dirty.store(true, Ordering::Release);
}
pub fn cursor_line_col(&self) -> (i32, u16) {
let term = self.term.lock();
let p = term.grid().cursor.point;
(p.line.0, p.column.0 as u16)
}
pub fn selection_and_clock(&mut self) -> (Option<Selection>, i64) {
let mut term = self.term.lock();
let now = self.clock.lock().unwrap().tick(&mut term);
let sel = self.selection.map(|mut s| {
let delta = (now - self.sel_scrolled) as i32;
s.anchor.0 -= delta;
s.head.0 -= delta;
s
});
(sel, now)
}
#[allow(clippy::type_complexity)]
pub fn cursor_bounds_and_clock(&mut self) -> ((i32, u16), (i32, i32, u16, u16), i64) {
let mut term = self.term.lock();
let now = self.clock.lock().unwrap().tick(&mut term);
let p = term.grid().cursor.point;
let top = term.grid().topmost_line().0;
let bottom = term.screen_lines() as i32 - 1;
(
(p.line.0, p.column.0 as u16),
(top, bottom, self.cols, self.rows),
now,
)
}
pub fn selection_text(&self) -> String {
if let Some(anchor) = self
.alt_sel
.as_ref()
.filter(|a| a.dormant || a.visible.is_some())
{
return anchor.text.clone();
}
let Some(mut sel) = self.selection else {
return String::new();
};
let term = self.term.lock();
let delta = (self.clock_now(&term) - self.sel_scrolled) as i32;
sel.anchor.0 -= delta;
sel.head.0 -= delta;
if sel.block {
let (rl, cl, rh, ch) = sel.block_bounds();
return block_selection_text(&term, rl, cl as usize, rh, ch as usize);
}
let (sr, sc, er, ec) = sel.normalised();
extract_selection_text(&term, sr, sc as usize, er, ec as usize)
}
}
pub(super) fn partial_overlap_cols(want: &str, got: &str, colmap: &[usize]) -> Vec<(u16, u16)> {
let want = want.trim_end();
let got = got.trim_end();
let want_n = want.chars().count();
let got_n = got.chars().count();
if want_n == 0 || got_n == 0 {
return Vec::new();
}
let prefix = want
.chars()
.zip(got.chars())
.take_while(|(a, b)| a == b)
.count();
let suffix = want
.chars()
.rev()
.zip(got.chars().rev())
.take_while(|(a, b)| a == b)
.count();
let solid =
|chars: &mut dyn Iterator<Item = char>| chars.filter(|c| !c.is_whitespace()).count() >= 4;
let good_prefix = prefix > 0 && solid(&mut want.chars().take(prefix));
let good_suffix = suffix > 0 && solid(&mut want.chars().rev().take(suffix));
let mut spans = Vec::new();
if good_prefix && good_suffix && prefix + suffix >= got_n {
spans.push((colmap[0] as u16, colmap[got_n - 1] as u16));
} else {
if good_prefix {
spans.push((colmap[0] as u16, colmap[prefix - 1] as u16));
}
if good_suffix {
spans.push((colmap[got_n - suffix] as u16, colmap[got_n - 1] as u16));
}
}
spans
}
pub(super) fn capture_alt_anchor(
term: &Term<VoidListener>,
sel: Selection,
) -> Option<AltSelAnchor> {
let rows_vis = term.screen_lines() as i32;
let (lo, hi) = (sel.anchor.0.min(sel.head.0), sel.anchor.0.max(sel.head.0));
if lo < 0 || hi >= rows_vis {
return None;
}
let rows = (lo..=hi).map(|l| row_text_and_cols(term, l).0).collect();
let ctx_above = (lo > 0).then(|| row_text_and_cols(term, lo - 1).0);
let ctx_below = (hi + 1 < rows_vis).then(|| row_text_and_cols(term, hi + 1).0);
let text = if sel.block {
let (rl, cl, rh, ch) = sel.block_bounds();
block_selection_text(term, rl, cl as usize, rh, ch as usize)
} else {
let (sr, sc, er, ec) = sel.normalised();
extract_selection_text(term, sr, sc as usize, er, ec as usize)
};
Some(AltSelAnchor {
rows,
top: lo,
text,
dormant: false,
visible: None,
ctx_above,
ctx_below,
})
}
pub fn extract_selection_text(
term: &Term<VoidListener>,
sr: i32,
sc: usize,
er: i32,
ec: usize,
) -> String {
let cols = term.columns();
let max_line = term.screen_lines() as i32 - 1;
let min_line = term.grid().topmost_line().0;
let sr = sr.max(min_line);
let er = er.min(max_line);
let mut out = String::new();
let mut line_idx = sr;
while line_idx <= er {
let row_start = if line_idx == sr { sc } else { 0 };
let row_end = if line_idx == er {
ec.min(cols.saturating_sub(1))
} else {
cols.saturating_sub(1)
};
let mut line = String::new();
for col in row_start..=row_end {
let p = Point::new(Line(line_idx), Column(col));
let cell = &term.grid()[p];
if cell
.flags
.intersects(Flags::WIDE_CHAR_SPACER | Flags::LEADING_WIDE_CHAR_SPACER)
{
continue;
}
let c = cell.c;
if c == '\0' {
line.push(' ');
} else {
line.push(c);
}
if let Some(marks) = cell.zerowidth() {
line.extend(marks.iter());
}
}
let wrapped = line_idx != er
&& cols > 0
&& term.grid()[Point::new(Line(line_idx), Column(cols - 1))]
.flags
.contains(Flags::WRAPLINE);
out.push_str(if wrapped { &line } else { line.trim_end() });
if line_idx != er && !wrapped {
out.push('\n');
}
line_idx += 1;
}
out
}
pub fn select_word_at_in_term(
term: &Term<VoidListener>,
display_offset: usize,
row: usize,
col: usize,
) -> Option<((u16, u16), (u16, u16))> {
let cols = term.columns();
if col >= cols {
return None;
}
let row_idx = row as i32 - display_offset as i32;
let cell_char = |c: usize| -> char {
let p = Point::new(Line(row_idx), Column(c));
let ch = term.grid()[p].c;
if ch == '\0' { ' ' } else { ch }
};
if !is_terminal_word_char(cell_char(col)) {
return None;
}
let mut start = col;
while start > 0 && is_terminal_word_char(cell_char(start - 1)) {
start -= 1;
}
let mut end = col;
while end + 1 < cols && is_terminal_word_char(cell_char(end + 1)) {
end += 1;
}
Some(((row as u16, start as u16), (row as u16, end as u16)))
}
pub fn is_terminal_word_char(c: char) -> bool {
c.is_alphanumeric() || c == '_'
}
pub fn block_selection_text(
term: &Term<VoidListener>,
rl: i32,
cl: usize,
rh: i32,
ch: usize,
) -> String {
let max_line = term.screen_lines() as i32 - 1;
let min_line = term.grid().topmost_line().0;
let rl = rl.max(min_line);
let rh = rh.min(max_line);
let mut rows = Vec::new();
for line in rl..=rh {
rows.push(extract_selection_text(term, line, cl, line, ch));
}
rows.join("\n")
}
pub fn cell_in_block_selection(row: i32, col: u16, rl: i32, cl: u16, rh: i32, ch: u16) -> bool {
row >= rl && row <= rh && col >= cl && col <= ch
}
pub fn cell_in_selection(row: i32, col: u16, sr: i32, sc: u16, er: i32, ec: u16) -> bool {
if row < sr || row > er {
return false;
}
if row == sr && col < sc {
return false;
}
if row == er && col > ec {
return false;
}
true
}