use std::collections::HashSet;
use std::fmt::Write;
use std::io::Write as IoWrite;
use arboard::Clipboard;
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
use crate::app::{CommentTypeDefinition, DiffSource};
use crate::error::{Result, TuicrError};
use crate::model::{CommentType, LineRange, LineSide, ReviewSession};
type CommentEntry<'a> = (String, Option<LineRange>, Option<LineSide>, String, &'a str);
pub fn generate_export_content(
session: &ReviewSession,
diff_source: &DiffSource,
comment_types: &[CommentTypeDefinition],
show_legend: bool,
) -> Result<String> {
if !session.has_comments() {
return Err(TuicrError::NoComments);
}
Ok(generate_markdown(
session,
diff_source,
comment_types,
show_legend,
))
}
pub fn export_to_clipboard(
session: &ReviewSession,
diff_source: &DiffSource,
comment_types: &[CommentTypeDefinition],
show_legend: bool,
) -> Result<String> {
let content = generate_export_content(session, diff_source, comment_types, show_legend)?;
if should_prefer_osc52() {
copy_osc52(&content)?;
return Ok("Review copied to clipboard (via terminal)".to_string());
}
match Clipboard::new().and_then(|mut cb| cb.set_text(&content)) {
Ok(_) => Ok("Review copied to clipboard".to_string()),
Err(_) => {
copy_osc52(&content)?;
Ok("Review copied to clipboard (via terminal)".to_string())
}
}
}
fn should_prefer_osc52() -> bool {
std::env::var("TMUX").is_ok()
|| std::env::var("SSH_TTY").is_ok()
|| std::env::var("ZELLIJ").is_ok()
}
fn copy_osc52(text: &str) -> Result<()> {
if std::env::var("TMUX").is_ok() {
copy_via_tmux(text)
} else {
let mut stdout = std::io::stdout().lock();
write_osc52(&mut stdout, text)
}
}
fn copy_via_tmux(text: &str) -> Result<()> {
use std::process::{Command, Stdio};
let mut child = Command::new("tmux")
.args(["load-buffer", "-w", "-"])
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.map_err(|e| TuicrError::Clipboard(format!("Failed to run tmux: {e}")))?;
if let Some(mut stdin) = child.stdin.take() {
stdin
.write_all(text.as_bytes())
.map_err(|e| TuicrError::Clipboard(format!("Failed to write to tmux: {e}")))?;
}
let status = child
.wait()
.map_err(|e| TuicrError::Clipboard(format!("tmux load-buffer failed: {e}")))?;
if !status.success() {
return Err(TuicrError::Clipboard(
"tmux load-buffer exited with error".to_string(),
));
}
Ok(())
}
fn write_osc52<W: IoWrite>(writer: &mut W, text: &str) -> Result<()> {
let encoded = BASE64.encode(text);
write!(writer, "\x1b]52;c;{encoded}\x07")
.map_err(|e| TuicrError::Clipboard(format!("Failed to write OSC 52: {e}")))?;
writer
.flush()
.map_err(|e| TuicrError::Clipboard(format!("Failed to flush: {e}")))?;
Ok(())
}
fn review_scope_label(diff_source: &DiffSource) -> String {
let scope = match diff_source {
DiffSource::WorkingTree => "working tree changes".to_string(),
DiffSource::StagedAndUnstaged => "staged + unstaged changes".to_string(),
DiffSource::Staged => "staged changes".to_string(),
DiffSource::Unstaged => "unstaged changes".to_string(),
DiffSource::CommitRange(_) => "selected commit range".to_string(),
DiffSource::StagedUnstagedAndCommits(_) => {
"selected commit range + staged/unstaged changes".to_string()
}
};
format!("Review Comment (scope: {scope})")
}
fn generate_markdown(
session: &ReviewSession,
diff_source: &DiffSource,
comment_types: &[CommentTypeDefinition],
show_legend: bool,
) -> String {
let mut md = String::new();
let _ = writeln!(
md,
"I reviewed your code and have the following comments. Please address them."
);
let _ = writeln!(md);
match diff_source {
DiffSource::WorkingTree => {}
DiffSource::Staged => {
let _ = writeln!(md, "Reviewing staged changes");
let _ = writeln!(md);
}
DiffSource::Unstaged => {
let _ = writeln!(md, "Reviewing unstaged changes");
let _ = writeln!(md);
}
DiffSource::StagedAndUnstaged => {
let _ = writeln!(md, "Reviewing staged + unstaged changes");
let _ = writeln!(md);
}
DiffSource::CommitRange(commits) => {
if commits.len() == 1 {
let _ = writeln!(
md,
"Reviewing commit: {}",
&commits[0][..7.min(commits[0].len())]
);
} else {
let short_ids: Vec<&str> = commits.iter().map(|c| &c[..7.min(c.len())]).collect();
let _ = writeln!(md, "Reviewing commits: {}", short_ids.join(", "));
}
let _ = writeln!(md);
}
DiffSource::StagedUnstagedAndCommits(commits) => {
let short_ids: Vec<&str> = commits.iter().map(|c| &c[..7.min(c.len())]).collect();
let _ = writeln!(
md,
"Reviewing staged + unstaged + commits: {}",
short_ids.join(", ")
);
let _ = writeln!(md);
}
}
if show_legend {
let used_ids = collect_used_comment_type_ids(session);
let legend = if comment_types.is_empty() {
let all = ["NOTE", "SUGGESTION", "ISSUE", "PRAISE"];
let filtered: Vec<&str> = if used_ids.is_empty() {
all.to_vec()
} else {
all.iter()
.copied()
.filter(|t| used_ids.contains(&t.to_ascii_lowercase()))
.collect()
};
filtered.join(", ")
} else {
let filtered: Vec<_> = comment_types
.iter()
.filter(|ct| used_ids.is_empty() || used_ids.contains(&ct.id))
.collect();
filtered
.iter()
.map(|comment_type| {
let definition = comment_type
.definition
.as_deref()
.unwrap_or(comment_type.id.as_str());
format!(
"{} ({})",
comment_type.label.to_ascii_uppercase(),
definition
)
})
.collect::<Vec<_>>()
.join(", ")
};
let _ = writeln!(md, "Comment types: {legend}");
let _ = writeln!(md);
}
if let Some(notes) = &session.session_notes {
let _ = writeln!(md, "Summary: {notes}");
let _ = writeln!(md);
}
let mut all_comments: Vec<CommentEntry> = Vec::new();
let review_comment_location = review_scope_label(diff_source);
for comment in &session.review_comments {
all_comments.push((
review_comment_location.clone(),
None,
None,
export_comment_type_label(&comment.comment_type, comment_types),
&comment.content,
));
}
let mut files: Vec<_> = session.files.iter().collect();
files.sort_by_key(|(path, _)| path.to_string_lossy().to_string());
for (path, review) in files {
let path_str = path.display().to_string();
for comment in &review.file_comments {
all_comments.push((
path_str.clone(),
None,
None,
export_comment_type_label(&comment.comment_type, comment_types),
&comment.content,
));
}
let mut line_comments: Vec<_> = review.line_comments.iter().collect();
line_comments.sort_by_key(|(line, _)| *line);
for (line, comments) in line_comments {
for comment in comments {
let line_range = comment
.line_range
.or_else(|| Some(LineRange::single(*line)));
all_comments.push((
path_str.clone(),
line_range,
comment.side,
export_comment_type_label(&comment.comment_type, comment_types),
&comment.content,
));
}
}
}
for (i, (file, line_range, side, comment_type, content)) in all_comments.iter().enumerate() {
let location = match (line_range, side) {
(Some(range), Some(LineSide::Old)) if range.is_single() => {
format!("`{}:~{}`", file, range.start)
}
(Some(range), Some(LineSide::Old)) => {
format!("`{}:~{}-~{}`", file, range.start, range.end)
}
(Some(range), _) if range.is_single() => {
format!("`{}:{}`", file, range.start)
}
(Some(range), _) => {
format!("`{}:{}-{}`", file, range.start, range.end)
}
(None, _) => format!("`{file}`"),
};
let _ = writeln!(
md,
"{}. **[{}]** {} - {}",
i + 1,
comment_type,
location,
content
);
}
md
}
fn collect_used_comment_type_ids(session: &ReviewSession) -> HashSet<String> {
let mut ids = HashSet::new();
for c in &session.review_comments {
ids.insert(c.comment_type.id().to_string());
}
for review in session.files.values() {
for c in &review.file_comments {
ids.insert(c.comment_type.id().to_string());
}
for comments in review.line_comments.values() {
for c in comments {
ids.insert(c.comment_type.id().to_string());
}
}
}
ids
}
fn export_comment_type_label(
comment_type: &CommentType,
comment_types: &[CommentTypeDefinition],
) -> String {
if let Some(definition) = comment_types
.iter()
.find(|definition| definition.id == comment_type.id())
{
return definition.label.to_ascii_uppercase();
}
comment_type.as_str()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::app::CommentTypeDefinition;
use crate::model::{Comment, CommentType, FileStatus, LineRange, LineSide, SessionDiffSource};
use std::path::PathBuf;
fn comment_types() -> Vec<CommentTypeDefinition> {
vec![
CommentTypeDefinition {
id: "note".to_string(),
label: "note".to_string(),
definition: Some("observations".to_string()),
color: None,
},
CommentTypeDefinition {
id: "suggestion".to_string(),
label: "suggestion".to_string(),
definition: Some("improvements".to_string()),
color: None,
},
CommentTypeDefinition {
id: "issue".to_string(),
label: "issue".to_string(),
definition: Some("problems to fix".to_string()),
color: None,
},
CommentTypeDefinition {
id: "praise".to_string(),
label: "praise".to_string(),
definition: Some("positive feedback".to_string()),
color: None,
},
]
}
fn create_test_session() -> ReviewSession {
let mut session = ReviewSession::new(
PathBuf::from("/tmp/test-repo"),
"abc1234def".to_string(),
Some("main".to_string()),
SessionDiffSource::WorkingTree,
);
session.add_file(PathBuf::from("src/main.rs"), FileStatus::Modified);
if let Some(review) = session.get_file_mut(&PathBuf::from("src/main.rs")) {
review.reviewed = true;
review.add_file_comment(Comment::new(
"Consider adding documentation".to_string(),
CommentType::Suggestion,
None,
));
review.add_line_comment(
42,
Comment::new(
"Magic number should be a constant".to_string(),
CommentType::Issue,
Some(LineSide::New),
),
);
}
session
}
#[test]
fn should_generate_valid_markdown() {
let session = create_test_session();
let diff_source = DiffSource::WorkingTree;
let markdown = generate_markdown(&session, &diff_source, &comment_types(), true);
assert!(markdown.contains("I reviewed your code and have the following comments"));
assert!(
markdown.contains("Comment types: SUGGESTION (improvements), ISSUE (problems to fix)")
);
assert!(!markdown.contains("NOTE"));
assert!(!markdown.contains("PRAISE"));
assert!(markdown.contains("[SUGGESTION]"));
assert!(markdown.contains("`src/main.rs`"));
assert!(markdown.contains("Consider adding documentation"));
assert!(markdown.contains("[ISSUE]"));
assert!(markdown.contains("`src/main.rs:42`"));
assert!(markdown.contains("Magic number"));
}
#[test]
fn should_use_configured_label_and_definition_in_export() {
let mut session = ReviewSession::new(
PathBuf::from("/tmp/test-repo"),
"abc1234def".to_string(),
Some("main".to_string()),
SessionDiffSource::WorkingTree,
);
session.add_file(PathBuf::from("src/main.rs"), FileStatus::Modified);
if let Some(review) = session.get_file_mut(&PathBuf::from("src/main.rs")) {
review.add_file_comment(Comment::new(
"Needs clarification".to_string(),
CommentType::Note,
None,
));
}
let custom_types = vec![CommentTypeDefinition {
id: "note".to_string(),
label: "question".to_string(),
definition: Some("ask for clarification".to_string()),
color: None,
}];
let markdown = generate_markdown(&session, &DiffSource::WorkingTree, &custom_types, true);
assert!(markdown.contains("Comment types: QUESTION (ask for clarification)"));
assert!(markdown.contains("**[QUESTION]**"));
}
#[test]
fn should_number_comments_sequentially() {
let session = create_test_session();
let diff_source = DiffSource::WorkingTree;
let markdown = generate_markdown(&session, &diff_source, &comment_types(), true);
assert!(markdown.contains("1. **[SUGGESTION]**"));
assert!(markdown.contains("2. **[ISSUE]**"));
}
#[test]
fn should_include_review_comments_in_export() {
let mut session = create_test_session();
session.review_comments.push(Comment::new(
"Please split this into smaller commits".to_string(),
CommentType::Note,
None,
));
let markdown =
generate_markdown(&session, &DiffSource::WorkingTree, &comment_types(), true);
assert!(markdown
.contains("`Review Comment (scope: working tree changes)` - Please split this into smaller commits"));
}
#[test]
fn should_include_commit_range_scope_for_review_comments() {
let mut session = create_test_session();
session.review_comments.push(Comment::new(
"High-level concern across commits".to_string(),
CommentType::Issue,
None,
));
let markdown = generate_markdown(
&session,
&DiffSource::CommitRange(vec!["abc1234567890".to_string()]),
&comment_types(),
true,
);
assert!(markdown.contains(
"`Review Comment (scope: selected commit range)` - High-level concern across commits"
));
}
#[test]
fn should_fail_export_when_no_comments() {
let session = ReviewSession::new(
PathBuf::from("/tmp/test-repo"),
"abc1234def".to_string(),
Some("main".to_string()),
SessionDiffSource::WorkingTree,
);
let diff_source = DiffSource::WorkingTree;
let result = export_to_clipboard(&session, &diff_source, &comment_types(), true);
assert!(result.is_err());
assert!(matches!(result.unwrap_err(), TuicrError::NoComments));
}
#[test]
fn should_generate_export_content_with_comments() {
let session = create_test_session();
let diff_source = DiffSource::WorkingTree;
let result = generate_export_content(&session, &diff_source, &comment_types(), true);
assert!(result.is_ok());
let content = result.unwrap();
assert!(content.contains("I reviewed your code"));
assert!(content.contains("[SUGGESTION]"));
assert!(content.contains("[ISSUE]"));
}
#[test]
fn should_fail_generate_export_content_when_no_comments() {
let session = ReviewSession::new(
PathBuf::from("/tmp/test-repo"),
"abc1234def".to_string(),
Some("main".to_string()),
SessionDiffSource::WorkingTree,
);
let diff_source = DiffSource::WorkingTree;
let result = generate_export_content(&session, &diff_source, &comment_types(), true);
assert!(result.is_err());
assert!(matches!(result.unwrap_err(), TuicrError::NoComments));
}
#[test]
fn should_include_commit_range_in_markdown() {
let session = create_test_session();
let diff_source = DiffSource::CommitRange(vec![
"abc1234567890".to_string(),
"def4567890123".to_string(),
]);
let markdown = generate_markdown(&session, &diff_source, &comment_types(), true);
assert!(markdown.contains("Reviewing commits: abc1234, def4567"));
}
#[test]
fn should_include_single_commit_in_markdown() {
let session = create_test_session();
let diff_source = DiffSource::CommitRange(vec!["abc1234567890".to_string()]);
let markdown = generate_markdown(&session, &diff_source, &comment_types(), true);
assert!(markdown.contains("Reviewing commit: abc1234"));
}
#[test]
fn should_write_osc52_escape_sequence() {
let text = "Hello, World!";
let mut buffer: Vec<u8> = Vec::new();
write_osc52(&mut buffer, text).unwrap();
let output = String::from_utf8(buffer).unwrap();
assert!(output.starts_with("\x1b]52;c;"));
assert!(output.ends_with("\x07"));
let base64_content = &output[7..output.len() - 1];
assert_eq!(BASE64.encode(text), base64_content);
}
#[test]
fn should_encode_empty_string_in_osc52() {
let text = "";
let mut buffer: Vec<u8> = Vec::new();
write_osc52(&mut buffer, text).unwrap();
let output = String::from_utf8(buffer).unwrap();
assert_eq!(output, "\x1b]52;c;\x07");
}
#[test]
fn should_encode_unicode_in_osc52() {
let text = "こんにちは 🦀";
let mut buffer: Vec<u8> = Vec::new();
write_osc52(&mut buffer, text).unwrap();
let output = String::from_utf8(buffer).unwrap();
let base64_content = &output[7..output.len() - 1];
let decoded = String::from_utf8(BASE64.decode(base64_content).unwrap()).unwrap();
assert_eq!(decoded, text);
}
#[test]
fn should_encode_markdown_content_in_osc52() {
let session = create_test_session();
let diff_source = DiffSource::WorkingTree;
let markdown = generate_markdown(&session, &diff_source, &comment_types(), true);
let mut buffer: Vec<u8> = Vec::new();
write_osc52(&mut buffer, &markdown).unwrap();
let output = String::from_utf8(buffer).unwrap();
assert!(output.starts_with("\x1b]52;c;"));
assert!(output.ends_with("\x07"));
let base64_content = &output[7..output.len() - 1];
let decoded = String::from_utf8(BASE64.decode(base64_content).unwrap()).unwrap();
assert_eq!(decoded, markdown);
}
#[test]
fn should_export_single_line_range_as_single_line() {
let mut session = ReviewSession::new(
PathBuf::from("/tmp/test-repo"),
"abc1234def".to_string(),
Some("main".to_string()),
SessionDiffSource::WorkingTree,
);
session.add_file(PathBuf::from("src/main.rs"), FileStatus::Modified);
if let Some(review) = session.get_file_mut(&PathBuf::from("src/main.rs")) {
let range = LineRange::single(42);
review.add_line_comment(
42,
Comment::new_with_range(
"Single line comment".to_string(),
CommentType::Note,
Some(LineSide::New),
range,
),
);
}
let diff_source = DiffSource::WorkingTree;
let markdown = generate_markdown(&session, &diff_source, &comment_types(), true);
assert!(markdown.contains("`src/main.rs:42`"));
assert!(!markdown.contains("`src/main.rs:42-42`"));
}
#[test]
fn should_export_line_range_with_start_and_end() {
let mut session = ReviewSession::new(
PathBuf::from("/tmp/test-repo"),
"abc1234def".to_string(),
Some("main".to_string()),
SessionDiffSource::WorkingTree,
);
session.add_file(PathBuf::from("src/main.rs"), FileStatus::Modified);
if let Some(review) = session.get_file_mut(&PathBuf::from("src/main.rs")) {
let range = LineRange::new(10, 15);
review.add_line_comment(
15, Comment::new_with_range(
"Multi-line comment".to_string(),
CommentType::Issue,
Some(LineSide::New),
range,
),
);
}
let diff_source = DiffSource::WorkingTree;
let markdown = generate_markdown(&session, &diff_source, &comment_types(), true);
assert!(markdown.contains("`src/main.rs:10-15`"));
assert!(markdown.contains("Multi-line comment"));
}
#[test]
fn should_export_old_side_line_range_with_tilde() {
let mut session = ReviewSession::new(
PathBuf::from("/tmp/test-repo"),
"abc1234def".to_string(),
Some("main".to_string()),
SessionDiffSource::WorkingTree,
);
session.add_file(PathBuf::from("src/main.rs"), FileStatus::Modified);
if let Some(review) = session.get_file_mut(&PathBuf::from("src/main.rs")) {
let range = LineRange::new(20, 25);
review.add_line_comment(
25, Comment::new_with_range(
"Deleted lines comment".to_string(),
CommentType::Suggestion,
Some(LineSide::Old),
range,
),
);
}
let diff_source = DiffSource::WorkingTree;
let markdown = generate_markdown(&session, &diff_source, &comment_types(), true);
assert!(markdown.contains("`src/main.rs:~20-~25`"));
}
#[test]
fn should_export_single_old_side_line_with_tilde() {
let mut session = ReviewSession::new(
PathBuf::from("/tmp/test-repo"),
"abc1234def".to_string(),
Some("main".to_string()),
SessionDiffSource::WorkingTree,
);
session.add_file(PathBuf::from("src/main.rs"), FileStatus::Modified);
if let Some(review) = session.get_file_mut(&PathBuf::from("src/main.rs")) {
let range = LineRange::single(30);
review.add_line_comment(
30,
Comment::new_with_range(
"Single deleted line".to_string(),
CommentType::Note,
Some(LineSide::Old),
range,
),
);
}
let diff_source = DiffSource::WorkingTree;
let markdown = generate_markdown(&session, &diff_source, &comment_types(), true);
assert!(markdown.contains("`src/main.rs:~30`"));
assert!(!markdown.contains("`src/main.rs:~30-~30`"));
}
#[test]
fn should_handle_comment_without_line_range_field() {
let mut session = ReviewSession::new(
PathBuf::from("/tmp/test-repo"),
"abc1234def".to_string(),
Some("main".to_string()),
SessionDiffSource::WorkingTree,
);
session.add_file(PathBuf::from("src/main.rs"), FileStatus::Modified);
if let Some(review) = session.get_file_mut(&PathBuf::from("src/main.rs")) {
review.add_line_comment(
50,
Comment::new(
"Old style comment".to_string(),
CommentType::Note,
Some(LineSide::New),
),
);
}
let diff_source = DiffSource::WorkingTree;
let markdown = generate_markdown(&session, &diff_source, &comment_types(), true);
assert!(markdown.contains("`src/main.rs:50`"));
}
#[test]
fn should_omit_legend_when_show_legend_is_false() {
let session = create_test_session();
let diff_source = DiffSource::WorkingTree;
let markdown = generate_markdown(&session, &diff_source, &comment_types(), false);
assert!(!markdown.contains("Comment types:"));
assert!(markdown.contains("[SUGGESTION]"));
assert!(markdown.contains("[ISSUE]"));
}
#[test]
fn should_only_list_used_comment_types_in_legend() {
let mut session = ReviewSession::new(
PathBuf::from("/tmp/test-repo"),
"abc1234def".to_string(),
Some("main".to_string()),
SessionDiffSource::WorkingTree,
);
session.add_file(PathBuf::from("src/main.rs"), FileStatus::Modified);
if let Some(review) = session.get_file_mut(&PathBuf::from("src/main.rs")) {
review.add_file_comment(Comment::new(
"Great work!".to_string(),
CommentType::Praise,
None,
));
}
let markdown =
generate_markdown(&session, &DiffSource::WorkingTree, &comment_types(), true);
assert!(markdown.contains("Comment types: PRAISE (positive feedback)"));
assert!(!markdown.contains("NOTE"));
assert!(!markdown.contains("SUGGESTION"));
assert!(!markdown.contains("ISSUE"));
}
#[test]
fn should_only_list_used_custom_types_in_legend() {
let mut session = ReviewSession::new(
PathBuf::from("/tmp/test-repo"),
"abc1234def".to_string(),
Some("main".to_string()),
SessionDiffSource::WorkingTree,
);
session.add_file(PathBuf::from("src/main.rs"), FileStatus::Modified);
if let Some(review) = session.get_file_mut(&PathBuf::from("src/main.rs")) {
review.add_file_comment(Comment::new(
"Needs clarification".to_string(),
CommentType::Note,
None,
));
}
let custom_types = vec![
CommentTypeDefinition {
id: "note".to_string(),
label: "question".to_string(),
definition: Some("ask for clarification".to_string()),
color: None,
},
CommentTypeDefinition {
id: "issue".to_string(),
label: "issue".to_string(),
definition: Some("problems to fix".to_string()),
color: None,
},
];
let markdown = generate_markdown(&session, &DiffSource::WorkingTree, &custom_types, true);
assert!(markdown.contains("Comment types: QUESTION (ask for clarification)"));
assert!(!markdown.contains("ISSUE"));
}
}