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
32 changes: 32 additions & 0 deletions codex-rs/tui/src/diff_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,18 @@ fn render_change(change: &FileChange, out: &mut Vec<RtLine<'static>>, width: usi
}
}

/// Format a path for display relative to the current working directory when
/// possible, keeping output stable in jj/no-`.git` workspaces (e.g. image
/// tool calls should show `example.png` instead of an absolute path).
pub(crate) fn display_path_for(path: &Path, cwd: &Path) -> String {
if path.is_relative() {
return path.display().to_string();
}

if let Ok(stripped) = path.strip_prefix(cwd) {
return stripped.display().to_string();
}

let path_in_same_repo = match (get_git_repo_root(cwd), get_git_repo_root(path)) {
(Some(cwd_repo), Some(path_repo)) => cwd_repo == path_repo,
_ => false,
Expand Down Expand Up @@ -420,6 +431,7 @@ fn style_del() -> Style {
mod tests {
use super::*;
use insta::assert_snapshot;
use pretty_assertions::assert_eq;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::text::Text;
Expand Down Expand Up @@ -459,6 +471,26 @@ mod tests {
assert_snapshot!(name, text);
}

#[test]
fn display_path_prefers_cwd_without_git_repo() {
let cwd = if cfg!(windows) {
PathBuf::from(r"C:\workspace\codex")
} else {
PathBuf::from("/workspace/codex")
};
let path = cwd.join("tui").join("example.png");

let rendered = display_path_for(&path, &cwd);

assert_eq!(
rendered,
PathBuf::from("tui")
.join("example.png")
.display()
.to_string()
);
}

#[test]
fn ui_snapshot_wrap_behavior_insert() {
// Narrow width to force wrapping within our diff line rendering
Expand Down
32 changes: 32 additions & 0 deletions codex-rs/tui2/src/diff_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,18 @@ fn render_change(change: &FileChange, out: &mut Vec<RtLine<'static>>, width: usi
}
}

/// Format a path for display relative to the current working directory when
/// possible, keeping output stable in jj/no-`.git` workspaces (e.g. image
/// tool calls should show `example.png` instead of an absolute path).
pub(crate) fn display_path_for(path: &Path, cwd: &Path) -> String {
if path.is_relative() {
return path.display().to_string();
}

if let Ok(stripped) = path.strip_prefix(cwd) {
return stripped.display().to_string();
}

// Prefer a stable, user-local relative path when the file is under the current working
// directory. This keeps output deterministic in jj-only repos (no `.git`) and matches user
// expectations for "files in this project".
Expand Down Expand Up @@ -431,6 +442,7 @@ fn style_del() -> Style {
mod tests {
use super::*;
use insta::assert_snapshot;
use pretty_assertions::assert_eq;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::text::Text;
Expand Down Expand Up @@ -470,6 +482,26 @@ mod tests {
assert_snapshot!(name, text);
}

#[test]
fn display_path_prefers_cwd_without_git_repo() {
let cwd = if cfg!(windows) {
PathBuf::from(r"C:\workspace\codex")
} else {
PathBuf::from("/workspace/codex")
};
let path = cwd.join("tui").join("example.png");

let rendered = display_path_for(&path, &cwd);

assert_eq!(
rendered,
PathBuf::from("tui")
.join("example.png")
.display()
.to_string()
);
}

#[test]
fn ui_snapshot_wrap_behavior_insert() {
// Narrow width to force wrapping within our diff line rendering
Expand Down
Loading