Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

feat: Add PR/MR hyperlinks to CI status output

Maximilian Roosf8d7d3185a42
diff --git a/src/commands/list/ci_status.rs b/src/commands/list/ci_status.rs
index f929de085..83169ecf1 100644
--- a/src/commands/list/ci_status.rs
+++ b/src/commands/list/ci_status.rs
@@ -24,6 +24,9 @@ pub struct PrStatus {
pub ci_status: CiStatus,
/// True if local HEAD differs from PR HEAD (unpushed changes)
pub is_stale: bool,
+ /// URL to the PR/MR (if available)
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub url: Option<String>,
}
impl PrStatus {
@@ -98,14 +101,19 @@ impl PrStatus {
return None;
}
- // Get PR info for the branch
+ // Use `gh pr list --head` instead of `gh pr view` to handle numeric branch names correctly.
+ // When branch name is all digits (e.g., "4315"), `gh pr view` interprets it as a PR number,
+ // but `gh pr list --head` correctly treats it as a branch name.
let mut cmd = Command::new("gh");
cmd.args([
"pr",
- "view",
+ "list",
+ "--head",
branch,
+ "--limit",
+ "1",
"--json",
- "state,headRefOid,mergeStateStatus,statusCheckRollup",
+ "state,headRefOid,mergeStateStatus,statusCheckRollup,url",
]);
// Remove environment variables that force color output
@@ -124,7 +132,9 @@ impl PrStatus {
return None;
}
- let pr_info: GitHubPrInfo = serde_json::from_slice(&output.stdout).ok()?;
+ // gh pr list returns an array, take the first (and only) item
+ let pr_list: Vec<GitHubPrInfo> = serde_json::from_slice(&output.stdout).ok()?;
+ let pr_info = pr_list.first()?;
// Only process open PRs
if pr_info.state != "OPEN" {
@@ -147,6 +157,7 @@ impl PrStatus {
Some(PrStatus {
ci_status,
is_stale,
+ url: pr_info.url.clone(),
})
}
@@ -196,6 +207,7 @@ impl PrStatus {
Some(PrStatus {
ci_status,
is_stale,
+ url: None, // GitLab MR URL not currently fetched
})
}
@@ -257,6 +269,7 @@ impl PrStatus {
Some(PrStatus {
ci_status,
is_stale,
+ url: None, // Workflow runs don't have a PR URL
})
}
@@ -298,6 +311,7 @@ impl PrStatus {
Some(PrStatus {
ci_status,
is_stale,
+ url: None, // GitLab pipeline URL not currently fetched
})
}
}
@@ -311,6 +325,7 @@ struct GitHubPrInfo {
merge_state_status: Option<String>,
#[serde(rename = "statusCheckRollup")]
status_check_rollup: Option<Vec<GitHubCheck>>,
+ url: Option<String>,
}
#[derive(Debug, Deserialize)]
diff --git a/src/commands/list/render.rs b/src/commands/list/render.rs
index e4fbd9692..17db0eb4a 100644
--- a/src/commands/list/render.rs
+++ b/src/commands/list/render.rs
@@ -93,12 +93,39 @@ impl PrStatus {
CiStatus::NoCI => "no-ci",
};
- format!("{}● {}{}", style, status_str, style.render_reset())
+ // Create base text
+ let text = format!("● {}", status_str);
+
+ // Wrap with hyperlink if URL is available
+ let content = if let Some(ref url) = self.url {
+ use worktrunk::styling::hyperlink;
+ hyperlink(&text, url)
+ } else {
+ text
+ };
+
+ format!("{}{}{}", style, content, style.render_reset())
}
fn render_indicator(&self) -> StyledLine {
let mut segment = StyledLine::new();
- segment.push_styled("●".to_string(), self.style());
+ let indicator = "●";
+
+ // If we have a URL, wrap the indicator with an OSC 8 hyperlink
+ if let Some(ref url) = self.url {
+ use worktrunk::styling::hyperlink;
+ // Create styled indicator with hyperlink
+ let styled_indicator = format!(
+ "{}{}{}",
+ self.style(),
+ hyperlink(indicator, url),
+ self.style().render_reset()
+ );
+ segment.push_raw(styled_indicator);
+ } else {
+ segment.push_styled(indicator.to_string(), self.style());
+ }
+
segment
}
}
diff --git a/src/styling/line.rs b/src/styling/line.rs
index 2ebb6c859..84684f07c 100644
--- a/src/styling/line.rs
+++ b/src/styling/line.rs
@@ -5,6 +5,11 @@
use anstyle::Style;
use unicode_width::UnicodeWidthStr;
+/// Strip ANSI escape codes (SGR and OSC) to get visual text for width calculation
+fn strip_ansi_codes(text: &str) -> String {
+ String::from_utf8_lossy(&strip_ansi_escapes::strip(text)).to_string()
+}
+
/// A piece of text with an optional style
#[derive(Clone, Debug)]
pub struct StyledString {
@@ -28,9 +33,10 @@ impl StyledString {
Self::new(text, Some(style))
}
- /// Returns the visual width (unicode-aware, no ANSI codes)
+ /// Returns the visual width (unicode-aware, ANSI codes stripped)
pub fn width(&self) -> usize {
- self.text.width()
+ let clean_text = strip_ansi_codes(&self.text);
+ clean_text.width()
}
/// Renders to a string with ANSI escape codes
@@ -92,3 +98,54 @@ impl StyledLine {
self.segments.iter().map(|s| s.render()).collect()
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_width_strips_osc_hyperlinks() {
+ // Text with OSC 8 hyperlink should have visual width of just the text
+ let url = "https://github.com/user/repo/pull/123";
+ let text_content = "●";
+ let hyperlinked = format!("\x1b]8;;{}\x1b\\{}\x1b]8;;\x1b\\", url, text_content);
+
+ let styled_str = StyledString::raw(&hyperlinked);
+ assert_eq!(
+ styled_str.width(),
+ 1,
+ "Hyperlinked '●' should have width 1, not {}",
+ styled_str.width()
+ );
+ }
+
+ #[test]
+ fn test_width_strips_sgr_codes() {
+ // Text with SGR color codes should have visual width of just the text
+ let colored = "\x1b[32m●\x1b[0m"; // Green ●
+
+ let styled_str = StyledString::raw(colored);
+ assert_eq!(
+ styled_str.width(),
+ 1,
+ "Colored '●' should have width 1, not {}",
+ styled_str.width()
+ );
+ }
+
+ #[test]
+ fn test_width_with_combined_ansi_codes() {
+ // Text with both color and hyperlink
+ let url = "https://example.com";
+ let combined = format!("\x1b[33m\x1b]8;;{}\x1b\\● passed\x1b]8;;\x1b\\\x1b[0m", url);
+
+ let styled_str = StyledString::raw(&combined);
+ // "● passed" = 1 + 1 (space) + 6 = 8
+ assert_eq!(
+ styled_str.width(),
+ 8,
+ "Combined styled text should have width 8, not {}",
+ styled_str.width()
+ );
+ }
+}
diff --git a/src/styling/mod.rs b/src/styling/mod.rs
index a4c893a16..f08b2a0b1 100644
--- a/src/styling/mod.rs
+++ b/src/styling/mod.rs
@@ -30,6 +30,14 @@ pub use format::{GUTTER_OVERHEAD, format_bash_with_gutter, format_with_gutter};
pub use highlighting::format_toml;
pub use line::{StyledLine, StyledString};
+/// Wrap text with an OSC 8 hyperlink
+///
+/// OSC 8 format: \x1b]8;;URL\x1b\\TEXT\x1b]8;;\x1b\\
+/// See: <https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda>
+pub fn hyperlink(text: &str, url: &str) -> String {
+ format!("\x1b]8;;{}\x1b\\{}\x1b]8;;\x1b\\", url, text)
+}
+
// Re-export for tests
#[cfg(test)]
use format::wrap_text_at_width;
@@ -382,4 +390,21 @@ command = "npm install"
"Should wrap into multiple lines when visual width (52) > max_width (30)"
);
}
+
+ #[test]
+ fn test_hyperlink() {
+ let text = "Click me";
+ let url = "https://github.com/user/repo/pull/123";
+ let result = super::hyperlink(text, url);
+
+ // OSC 8 format: \x1b]8;;URL\x1b\\TEXT\x1b]8;;\x1b\\
+ let expected = format!("\x1b]8;;{}\x1b\\{}\x1b]8;;\x1b\\", url, text);
+ assert_eq!(result, expected);
+
+ // Verify structure
+ assert!(result.starts_with("\x1b]8;;"));
+ assert!(result.contains(url));
+ assert!(result.contains(text));
+ assert!(result.ends_with("\x1b]8;;\x1b\\"));
+ }
}