diff options
| author | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-04-06 22:05:00 +0300 |
|---|---|---|
| committer | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-04-06 22:56:01 +0300 |
| commit | 6ccf156a3dd23f419c03892ddc1136e72957f66a (patch) | |
| tree | 1d5465e61d03344a63683ad3b9bbdec9a4476a5c /src | |
| parent | 62b751b2b551d866e2aebfdecf89f7a003d64ebc (diff) | |
refactor: use libghostty-native selection text reading
Diffstat (limited to 'src')
| -rw-r--r-- | src/ghostty/mod.rs | 90 | ||||
| -rw-r--r-- | src/pane.rs | 45 | ||||
| -rw-r--r-- | src/selection.rs | 4 | ||||
| -rw-r--r-- | src/workspace.rs | 27 |
4 files changed, 115 insertions, 51 deletions
diff --git a/src/ghostty/mod.rs b/src/ghostty/mod.rs index a4846db4..e9ca1f86 100644 --- a/src/ghostty/mod.rs +++ b/src/ghostty/mod.rs @@ -223,6 +223,23 @@ struct WritePtyCallbackState { callback: Box<dyn FnMut(&[u8]) + Send>, } +#[repr(C)] +struct GhosttyTerminalSelection { + start: ffi::GhosttyPoint, + end: ffi::GhosttyPoint, + rectangle: bool, +} + +unsafe extern "C" { + fn ghostty_terminal_read_text( + terminal: ffi::GhosttyTerminal_ptr, + selection: GhosttyTerminalSelection, + allocator: *const ffi::GhosttyAllocator, + out_ptr: *mut *mut u8, + out_len: *mut usize, + ) -> ffi::GhosttyResult; +} + unsafe extern "C" fn write_pty_trampoline( _terminal: ffi::GhosttyTerminal_ptr, userdata: *mut c_void, @@ -438,6 +455,46 @@ impl Terminal { Ok(buffer) } + pub fn read_text_viewport( + &self, + start: (u16, u32), + end: (u16, u32), + rectangle: bool, + ) -> Result<String, Error> { + let selection = GhosttyTerminalSelection { + start: ghostty_viewport_point(start.0, start.1), + end: ghostty_viewport_point(end.0, end.1), + rectangle, + }; + let mut out_ptr = ptr::null_mut(); + let mut out_len = 0usize; + unsafe { + ghostty_terminal_read_text( + self.raw, + selection, + ptr::null(), + &mut out_ptr, + &mut out_len, + ) + .into_result()?; + } + + let text = if out_len == 0 { + String::new() + } else { + let bytes = unsafe { slice::from_raw_parts(out_ptr.cast_const(), out_len) }; + String::from_utf8_lossy(bytes).into_owned() + }; + + if !out_ptr.is_null() { + unsafe { + ffi::ghostty_free(ptr::null(), out_ptr, out_len); + } + } + + Ok(text) + } + pub fn scroll_viewport_bottom(&mut self) { let viewport = ffi::GhosttyTerminalScrollViewport { tag: ffi::GhosttyTerminalScrollViewportTag_GHOSTTY_SCROLL_VIEWPORT_BOTTOM, @@ -504,6 +561,15 @@ impl Drop for Terminal { } } +fn ghostty_viewport_point(x: u16, y: u32) -> ffi::GhosttyPoint { + ffi::GhosttyPoint { + tag: ffi::GhosttyPointTag_GHOSTTY_POINT_TAG_VIEWPORT, + value: ffi::GhosttyPointValue { + coordinate: ffi::GhosttyPointCoordinate { x, y }, + }, + } +} + pub struct RenderState { raw: ffi::GhosttyRenderState_ptr, } @@ -1104,6 +1170,30 @@ mod tests { } #[test] + fn terminal_read_text_viewport_unwraps_soft_wrapped_selection() { + let mut terminal = Terminal::new(5, 3, 0).unwrap(); + terminal.write("1ABCD2EFGH3IJKL".as_bytes()); + + let text = terminal.read_text_viewport((0, 1), (2, 2), false).unwrap(); + assert_eq!(text, "2EFGH3IJ"); + } + + #[test] + fn terminal_read_text_viewport_handles_wide_chars() { + let mut terminal = Terminal::new(5, 3, 0).unwrap(); + terminal.write("1A⚡".as_bytes()); + + let full = terminal.read_text_viewport((0, 0), (3, 0), false).unwrap(); + assert_eq!(full, "1A⚡"); + + let through_wide_head = terminal.read_text_viewport((0, 0), (2, 0), false).unwrap(); + assert_eq!(through_wide_head, "1A⚡"); + + let wide_only = terminal.read_text_viewport((3, 0), (3, 0), false).unwrap(); + assert_eq!(wide_only, "⚡"); + } + + #[test] fn terminal_and_render_state_smoke_test() { let mut terminal = Terminal::new(8, 3, 100).unwrap(); assert_eq!(terminal.cols().unwrap(), 8); diff --git a/src/pane.rs b/src/pane.rs index 314554e1..a170d295 100644 --- a/src/pane.rs +++ b/src/pane.rs @@ -1059,47 +1059,12 @@ fn ghostty_extract_selection( core: &mut GhosttyPaneCore, selection: &crate::selection::Selection, ) -> Result<String, crate::ghostty::Error> { - let GhosttyPaneCore { - terminal, - render_state, - } = core; - render_state.update(terminal)?; - let mut row_iterator = crate::ghostty::RowIterator::new()?; - let mut row_cells = crate::ghostty::RowCells::new()?; - let mut rows = render_state.populate_row_iterator(&mut row_iterator)?; - let mut lines = Vec::new(); - let mut row_index = 0u16; - let ((start_row, _), (end_row, _)) = selection.ordered_cells(); - while rows.next() { - if row_index > end_row { - break; - } - if row_index >= start_row { - let mut cells = rows.populate_cells(&mut row_cells)?; - let (start_col, end_col) = selection_cols_for_row(selection, row_index); - let mut line = String::new(); - for x in start_col..=end_col { - cells.select(x)?; - line.push_str(&ghostty_cell_symbol(&cells)?); - } - lines.push(line.trim_end().to_string()); - } - row_index += 1; - } - Ok(lines.join("\n")) -} - -fn selection_cols_for_row(selection: &crate::selection::Selection, row: u16) -> (u16, u16) { let ((start_row, start_col), (end_row, end_col)) = selection.ordered_cells(); - if start_row == end_row { - (start_col, end_col) - } else if row == start_row { - (start_col, selection.pane_width().saturating_sub(1)) - } else if row == end_row { - (0, end_col) - } else { - (0, selection.pane_width().saturating_sub(1)) - } + core.terminal.read_text_viewport( + (start_col, u32::from(start_row)), + (end_col, u32::from(end_row)), + false, + ) } fn ghostty_screen_row( diff --git a/src/selection.rs b/src/selection.rs index 5531a189..bb5a76e8 100644 --- a/src/selection.rs +++ b/src/selection.rs @@ -104,10 +104,6 @@ impl Selection { self.ordered() } - pub(crate) fn pane_width(&self) -> u16 { - self.pane_inner.width - } - /// Check whether a pane-relative cell (row, col) is inside the selection. pub fn contains(&self, row: u16, col: u16) -> bool { if !self.is_visible() { diff --git a/src/workspace.rs b/src/workspace.rs index 25de6568..01a96b3f 100644 --- a/src/workspace.rs +++ b/src/workspace.rs @@ -670,13 +670,26 @@ pub fn derive_label_from_cwd(cwd: &Path) -> String { } pub fn git_branch(cwd: &Path) -> Option<String> { - let repo_root = git_repo_root(cwd)?; - let head_path = repo_root.join(".git").join("HEAD"); - let content = std::fs::read_to_string(head_path).ok()?; - let trimmed = content.trim(); - trimmed - .strip_prefix("ref: refs/heads/") - .map(|s| s.to_string()) + git_repo_root(cwd)?; + + let output = std::process::Command::new("git") + .arg("-C") + .arg(cwd) + .args(["rev-parse", "--abbrev-ref", "HEAD"]) + .output() + .ok()?; + + if !output.status.success() { + return None; + } + + let stdout = String::from_utf8(output.stdout).ok()?; + let branch = stdout.trim(); + if branch.is_empty() || branch == "HEAD" { + None + } else { + Some(branch.to_string()) + } } fn git_repo_root(start: &Path) -> Option<PathBuf> { |
