diff --git a/src/main.rs b/src/main.rs index 711f3eb..a412f0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1710,6 +1710,56 @@ struct TerminalSession { start_dir: PathBuf, title: String, exit: Option, + /// Mouse selection as anchor and focus cells in screen coordinates. + selection: Option, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct TerminalSelection { + anchor: (u16, u16), + focus: (u16, u16), +} + +impl TerminalSelection { + fn ordered(self) -> ((u16, u16), (u16, u16)) { + if self.anchor <= self.focus { + (self.anchor, self.focus) + } else { + (self.focus, self.anchor) + } + } +} + +/// Copies the selected terminal text to the clipboard. +fn copy_terminal_selection(state: &Moorly, id: u32) -> Task { + let Some(terminal) = state.terminal_sessions.get(&id) else { + return Task::none(); + }; + let Some(selection) = terminal.selection else { + return Task::none(); + }; + + let text = terminal_selection_text(terminal.session.screen(), selection); + + if text.is_empty() { + Task::none() + } else { + iced::clipboard::write(text) + } +} + +/// Text covered by a selection, in reading order and with the end cell +/// included. +fn terminal_selection_text(screen: &vt100::Screen, selection: TerminalSelection) -> String { + let (start, end) = selection.ordered(); + let (_, cols) = screen.size(); + + screen.contents_between( + start.0, + start.1, + end.0, + end.1.saturating_add(1).min(cols.get()), + ) } struct Moorly { @@ -4888,6 +4938,8 @@ enum Message { new_window: bool, }, TerminalTick, + TerminalSelectionStart(u32, u16, u16), + TerminalSelectionUpdate(u32, u16, u16), TerminalKey { id: u32, key: keyboard::Key, @@ -5195,12 +5247,48 @@ fn update(state: &mut Moorly, message: Message) -> Task { Message::TerminalTick => { return terminal_tick(state); } + Message::TerminalSelectionStart(id, row, col) => { + if let Some(terminal) = state.terminal_sessions.get_mut(&id) { + terminal.selection = Some(TerminalSelection { + anchor: (row, col), + focus: (row, col), + }); + } + } + Message::TerminalSelectionUpdate(id, row, col) => { + if let Some(terminal) = state.terminal_sessions.get_mut(&id) + && let Some(selection) = &mut terminal.selection + { + selection.focus = (row, col); + } + } Message::TerminalKey { id, key, text, modifiers, } => { + // Copy the selection with Ctrl+Shift+C, or with Ctrl+C while a + // selection exists (then clear it so the next Ctrl+C interrupts). + let copy = modifiers.command() + && matches!(&key, keyboard::Key::Character(character) if character.eq_ignore_ascii_case("c")) + && state + .terminal_sessions + .get(&id) + .is_some_and(|terminal| terminal.selection.is_some()); + + if copy { + let task = copy_terminal_selection(state, id); + + if !modifiers.shift() + && let Some(terminal) = state.terminal_sessions.get_mut(&id) + { + terminal.selection = None; + } + + return task; + } + if let Some(terminal) = state.terminal_sessions.get_mut(&id) && terminal.exit.is_none() && let Some(bytes) = terminal::key_bytes( @@ -10857,6 +10945,7 @@ fn start_terminal_profile( start_dir, title, exit: None, + selection: None, }, ); @@ -11046,6 +11135,7 @@ fn restart_terminal(state: &mut Moorly, id: u32) { terminal.session = session; terminal.start_dir = start_dir; terminal.exit = None; + terminal.selection = None; } } Err(error) => { @@ -11090,12 +11180,30 @@ fn profile_start_directory(profile: &Profile) -> (PathBuf, Option) { } struct TerminalCanvas<'a> { + id: u32, screen: &'a vt100::Screen, + selection: Option, theme: terminal::Theme, font: Font, font_size: f32, } +impl TerminalCanvas<'_> { + /// Cell under the cursor, clamped to the screen. + fn cell_at(&self, cursor: mouse::Cursor, bounds: Rectangle) -> Option<(u16, u16)> { + let point = cursor.position_in(bounds)?; + let cell = terminal::cell_size(self.font_size); + let (rows, cols) = self.screen.size(); + let row = (point.y / cell.height).floor().max(0.0) as u16; + let col = (point.x / cell.width).floor().max(0.0) as u16; + + Some(( + row.min(rows.get().saturating_sub(1)), + col.min(cols.get().saturating_sub(1)), + )) + } +} + #[derive(Clone, Copy, PartialEq)] struct CellStyle { foreground: Color, @@ -11106,7 +11214,55 @@ struct CellStyle { } impl canvas::Program for TerminalCanvas<'_> { - type State = (); + /// Whether the left mouse button is currently dragging a selection. + type State = bool; + + fn update( + &self, + dragging: &mut Self::State, + event: &canvas::Event, + bounds: Rectangle, + cursor: mouse::Cursor, + ) -> Option> { + match event { + canvas::Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => { + let (row, col) = self.cell_at(cursor, bounds)?; + *dragging = true; + Some( + canvas::Action::publish(Message::TerminalSelectionStart(self.id, row, col)) + .and_capture(), + ) + } + canvas::Event::Mouse(mouse::Event::CursorMoved { .. }) => { + if !*dragging { + return None; + } + + let (row, col) = self.cell_at(cursor, bounds)?; + Some(canvas::Action::publish(Message::TerminalSelectionUpdate( + self.id, row, col, + ))) + } + canvas::Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => { + *dragging = false; + Some(canvas::Action::capture()) + } + _ => None, + } + } + + fn mouse_interaction( + &self, + dragging: &Self::State, + _bounds: Rectangle, + _cursor: mouse::Cursor, + ) -> mouse::Interaction { + if *dragging { + mouse::Interaction::Grabbing + } else { + mouse::Interaction::Text + } + } fn draw( &self, @@ -11178,6 +11334,29 @@ impl canvas::Program for TerminalCanvas<'_> { } } + if let Some(selection) = self.selection { + let (start, end) = selection.ordered(); + + for row in start.0..=end.0 { + let from = if row == start.0 { start.1 } else { 0 }; + let to = if row == end.0 { + end.1 + } else { + cols.get().saturating_sub(1) + }; + + if from > to { + continue; + } + + frame.fill_rectangle( + Point::new(from as f32 * cell.width, row as f32 * cell.height), + Size::new((to - from + 1) as f32 * cell.width, cell.height), + self.theme.selection.scale_alpha(0.6), + ); + } + } + if !self.screen.hide_cursor() { let (row, col) = self.screen.cursor_position(); let position = Point::new(col as f32 * cell.width, row as f32 * cell.height); @@ -11299,7 +11478,9 @@ fn terminal_page<'a>(state: &'a Moorly, window: window::Id, id: u32) -> Element< let theme = terminal::Theme::DARK; let view = canvas::Canvas::new(TerminalCanvas { + id, screen: terminal_session.session.screen(), + selection: terminal_session.selection, theme, font: typography_font(&state.typography.terminal), font_size: state.typography.terminal.size, @@ -12144,6 +12325,32 @@ mod tests { ); } + #[test] + fn terminal_selection_copy_includes_the_end_cell() { + let mut parser = vt100::Parser::new(4.try_into().unwrap(), 20.try_into().unwrap(), 0); + parser.process(b"hello\r\nworld"); + let screen = parser.screen(); + + let selection = super::TerminalSelection { + anchor: (0, 1), + focus: (1, 2), + }; + assert_eq!( + super::terminal_selection_text(screen, selection), + "ello\nwor" + ); + + // Dragging backwards selects the same cells in reading order. + let reversed = super::TerminalSelection { + anchor: (1, 2), + focus: (0, 1), + }; + assert_eq!( + super::terminal_selection_text(screen, reversed), + "ello\nwor" + ); + } + #[test] fn quick_power_shells_are_numbered_separately() { let entries = vec![ @@ -14042,6 +14249,7 @@ start_path = "/tmp" start_dir: std::path::PathBuf::from("/"), title: "Shell".to_owned(), exit: None, + selection: None, }, ); state @@ -14174,6 +14382,7 @@ start_path = "/tmp" start_dir: std::path::PathBuf::from("/"), title: "Shell".to_owned(), exit: None, + selection: None, }, ); state