Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions crates/agentdeck-gui/src/pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ impl PaneView {
window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
let cwd = pane.cwd.clone().into();
let terminal = cx.new(|cx| TerminalView::new(pane.id, cwd, connection, sender, window, cx));
let folder = pane.folder.clone().into();
let terminal =
cx.new(|cx| TerminalView::new(pane.id, folder, connection, sender, window, cx));
let titles = cx.subscribe(&terminal, |this, _, event, cx| match event {
TerminalEvent::Title(title) => {
this.live_title = Some(title.clone());
Expand All @@ -170,6 +171,12 @@ impl PaneView {
}
}

/// Its terminal.
#[cfg(test)]
pub fn terminal(&self) -> Entity<TerminalView> {
self.terminal.clone()
}

/// The pane it shows.
pub fn id(&self) -> PaneId {
self.pane.id
Expand All @@ -183,6 +190,11 @@ impl PaneView {
(_, true) => self.left_title = None,
(false, false) => {}
}
if self.pane.folder != pane.folder {
let folder = pane.folder.clone().into();
self.terminal
.update(cx, |terminal, _| terminal.set_folder(folder));
}
self.pane = pane;
cx.notify();
}
Expand Down
80 changes: 67 additions & 13 deletions crates/agentdeck-gui/src/terminal/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
//! underlined and the pointer is a pointing hand.
//!
//! File paths are links too (spec §11): `src/main.rs:42:7` in the text, resolved against the
//! directory the shell last reported with OSC 7, else the pane's launch dir, and a `file://`
//! OSC 8 link for this host. Only an existing regular file is one; a click runs
//! directory the shell last reported with OSC 7, else the pane's current folder as the daemon
//! last read it ([`TerminalView::set_folder`]), and a `file://` OSC 8 link for this host. Only an existing regular file is one; a click runs
//! `terminal.file_opener` on it ([`open`]), and a failure toasts. A URL wins over a path.

use std::ops::{Range, RangeInclusive};
Expand Down Expand Up @@ -116,9 +116,9 @@ pub enum TerminalEvent {
/// A pane's terminal.
pub struct TerminalView {
pane: PaneId,
/// The pane's launch dir, which relative paths resolve against until the shell reports
/// its own with OSC 7.
cwd: PathBuf,
/// The pane's current folder as the daemon last read it (its `folder`): what relative
/// paths resolve against while the shell reports none with OSC 7.
folder: PathBuf,
channel: Channel,
sender: ws::Sender,
attachment: Attachment,
Expand All @@ -140,10 +140,11 @@ pub struct TerminalView {
}

impl TerminalView {
/// Attaches `pane`, launched in `cwd`, over `connection`'s socket, through `sender`.
/// Attaches `pane`, whose current folder is `folder`, over `connection`'s socket, through
/// `sender`.
pub fn new(
pane: PaneId,
cwd: PathBuf,
folder: PathBuf,
connection: &Entity<Connection>,
sender: ws::Sender,
window: &mut Window,
Expand All @@ -158,7 +159,7 @@ impl TerminalView {
let focus = cx.focus_handle();
let view = TerminalView {
pane,
cwd,
folder,
channel,
sender,
attachment: Attachment::Attaching,
Expand All @@ -176,6 +177,22 @@ impl TerminalView {
view
}

/// Takes the pane's current folder from a fresh read of the pane.
pub fn set_folder(&mut self, folder: PathBuf) {
self.folder = folder;
}

/// The folder a relative path resolves against (spec §11): the one the shell last reported
/// with OSC 7, else the pane's current folder as the daemon last read it.
pub fn paths_dir(&self) -> PathBuf {
let reported = match &self.attachment {
Attachment::Live(screen) => screen.reported_cwd(),
_ => None,
};
(reported.and_then(|uri| path::file_uri(uri, path::this_host())))
.unwrap_or_else(|| self.folder.clone())
}

fn attach(&self) {
let _ = self.sender.send(ClientMessage::Attach {
pane: self.pane,
Expand Down Expand Up @@ -474,11 +491,7 @@ impl TerminalView {
});
}
let found = screen.path_at(at)?;
let cwd = screen
.reported_cwd()
.and_then(|uri| path::file_uri(uri, host))
.unwrap_or_else(|| self.cwd.clone());
let file = path::resolve(&found.path.path, &cwd, path::home().as_deref())?;
let file = path::resolve(&found.path.path, &self.paths_dir(), path::home().as_deref())?;
path::is_file(&file).then_some(Hovered {
target: Target::File {
path: file,
Expand Down Expand Up @@ -2422,6 +2435,47 @@ mod tests {
);
}

#[gpui::test]
fn should_resolve_against_the_panes_folder_without_osc_7(cx: &mut TestAppContext) {
let (harness, cx) = showing("notes.txt", cx);
let moved_to = tempfile::tempdir().unwrap();
let file = touch(moved_to.path(), "notes.txt");
touch(harness.cwd.path(), "notes.txt");
// The daemon read the shell's folder from /proc after a `cd`.
let view = harness.view(cx);
view.update(cx, |view, _| view.set_folder(moved_to.path().into()));

ctrl_click(&harness, 0, 2, cx);

assert_eq!(
harness.started(),
[["xdg-open".to_owned(), file.display().to_string()]]
);
}

#[gpui::test]
fn should_prefer_the_dir_the_shell_reported_to_the_panes_folder(
cx: &mut TestAppContext,
) {
let (harness, cx) = showing("", cx);
let reported = tempfile::tempdir().unwrap();
let file = touch(reported.path(), "notes.txt");
let read = tempfile::tempdir().unwrap();
touch(read.path(), "notes.txt");
let view = harness.view(cx);
view.update(cx, |view, _| view.set_folder(read.path().into()));
let text = format!("\x1b]7;file://{}\x1b\\notes.txt", reported.path().display());
harness.deliver(Event::Output(1, text.into_bytes()), cx);
harness.draw(cx);

ctrl_click(&harness, 0, 2, cx);

assert_eq!(
harness.started(),
[["xdg-open".to_owned(), file.display().to_string()]]
);
}

#[gpui::test]
fn should_open_a_file_link_for_this_host_through_the_opener(cx: &mut TestAppContext) {
let (harness, cx) = Harness::live(b"", cx);
Expand Down
32 changes: 32 additions & 0 deletions crates/agentdeck-gui/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,7 @@ impl Render for WorkspaceView {

#[cfg(test)]
mod tests {
use std::path::{Path, PathBuf};
use std::rc::Rc;

use agentdeck_client::types::Pane;
Expand Down Expand Up @@ -1024,6 +1025,12 @@ mod tests {
self.view.read_with(cx, |view, _| view.shown()[n].clone())
}

/// The folder a relative file path in the `n`th pane shown resolves against.
fn paths_dir(&self, n: usize, cx: &mut VisualTestContext) -> PathBuf {
let terminal = self.pane(n, cx).read_with(cx, |pane, _| pane.terminal());
terminal.read_with(cx, |terminal, _| terminal.paths_dir())
}

fn selected(&self, cx: &mut VisualTestContext) -> Option<TabId> {
self.view.read_with(cx, |view, _| view.selected())
}
Expand Down Expand Up @@ -1297,6 +1304,31 @@ mod tests {
assert_eq!(harness.pane(0, cx), before);
}

#[gpui::test]
fn should_resolve_file_paths_against_each_panes_folder_as_last_read(
cx: &mut TestAppContext,
) {
let launched = Pane {
cwd: "/w".into(),
folder: "/w".into(),
..shell_pane(7, id(1))
};
let (harness, cx) = Harness::open(
vec![tab(1, id(1), 0, Some(id(7)))],
vec![launched.clone()],
cx,
);
assert_eq!(harness.paths_dir(0, cx), Path::new("/w"));
harness.api.set_panes(vec![Pane {
folder: "/w/moved".into(),
..launched
}]);

harness.changed(Changed::Pane, cx);

assert_eq!(harness.paths_dir(0, cx), Path::new("/w/moved"));
}

#[gpui::test]
fn should_not_read_again_for_other_changes(cx: &mut TestAppContext) {
let (harness, cx) = Harness::open(
Expand Down
3 changes: 2 additions & 1 deletion docs/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,9 @@ Built on `brrritscold-core-gpui` (`init`, `Root`, theme preference, toasts). Scr
- It opens even while the program has the mouse (vim, tmux, Claude Code), and the program hears nothing of that click. A `secondary`-click off a link is reported as before.
- Only `http`, `https` and `mailto` open with the OS's handler: `terminal.file_opener`'s default, `xdg-open`, with the URL as its one argument, started as a file opener is (below), so its output goes nowhere, never into the terminal the GUI runs from. A handler that can't start toasts "Couldn't open the link" with why. Every URL the GUI opens (these, Copy code & open GitHub, a PR's page) starts this way. A `file://` link opens through `terminal.file_opener` (below) when its host is empty, `localhost` or this machine's hostname and it names an existing regular file, with no confirmation, since the command is one the user set. Any other scheme, or another host's file, is refused silently (a debug log line), so a program can't make a click run a custom URL handler.
- While `secondary` is held over a link, its cells are underlined and the pointer is a pointing hand, until the key is let go or the pointer leaves.
- **File paths:** a run of text with no spaces, quotes or brackets that holds a `/` or a `.` (`src/main.rs`, `./a/b.txt`, `/abs/path`, `~/x`, `Cargo.toml`), with an optional `:line[:col]`, found across soft wraps and without the sentence punctuation around it, as a URL is. Anything after the column (`src/main.rs:42:fn main`) isn't part of it. A relative path resolves against the directory the shell last reported with OSC 7 (`file://host/path`, for this host), else the pane's launch dir; `~/` against `$HOME`. Only an existing regular file is a link: no underline and no hand otherwise. A URL or OSC 8 link over the same text wins.
- **File paths:** a run of text with no spaces, quotes or brackets that holds a `/` or a `.` (`src/main.rs`, `./a/b.txt`, `/abs/path`, `~/x`, `Cargo.toml`), with an optional `:line[:col]`, found across soft wraps and without the sentence punctuation around it, as a URL is. Anything after the column (`src/main.rs:42:fn main`) isn't part of it. A relative path resolves against the directory the shell last reported with OSC 7 (`file://host/path`, for this host), else the pane's `folder` as the GUI last read it (§6: its process's working directory, else its launch dir `cwd`), so a path printed after a `cd` in a shell that sends no OSC 7 (`sh`, `bash` without a prompt hook) resolves where the shell went; `~/` against `$HOME`. Only an existing regular file is a link: no underline and no hand otherwise. A URL or OSC 8 link over the same text wins.
- The GUI reads the OSC 7 directory from the pane's output, the attach snapshot included: the daemon keeps each pane's last OSC 7 URI, as sent, and ends every snapshot with it (§5), so a reattach (a GUI restart, a `lagged` re-attach, switching back to a workspace) resolves against the shell's directory at once.
- The pane's `folder` comes with each read of the workspace's panes, which `changed` for a pane starts; the daemon sends it when the folder moves (§6). The daemon reads `/proc/<pid>/cwd` on the tick after the pane prints, at most once a second, so a path clicked within about a second of the prompt after a `cd` can still resolve against the folder before it. The GUI reads nothing more at click time: whether a click lands on a link is settled as it happens, from what the underline showed, and the click goes on to the program when it doesn't.
- A click runs `terminal.file_opener` (§2) with `{path}` the absolute path and `{line}` / `{col}` from the text, `1` when it has none. The command is split into words before the placeholders are filled, and started straight from those words, never through a shell, detached in its own process group, from the GUI's process, on the user's desktop. A command that doesn't start toasts "Couldn't open <file name>" with why.
- Input methods and dead keys: a composition shows underlined at the cursor, and only committed text reaches the pane. The candidate window opens at the cursor.
- **Colours** use semantic tokens only.
Expand Down