Initial public release
Luxe 0.1.0: a coding agent harness for the terminal. See README.md to get started and CHANGELOG.md for releases.
This commit is contained in:
144
crates/luxe-tui/src/view/guided_review/github_post/items.rs
Normal file
144
crates/luxe-tui/src/view/guided_review/github_post/items.rs
Normal file
@@ -0,0 +1,144 @@
|
||||
//! Post items — what the post will carry, where each item lands on GitHub, and the list of them.
|
||||
|
||||
use crate::app::App;
|
||||
use crate::guided_review::diff::clip_terminal_cells_ellipsis;
|
||||
use luxe_core::guided_review::{BodyReason, DiffSide, FindingId, Placement, PostPlan};
|
||||
use ratatui::text::{Line, Span};
|
||||
|
||||
/// One thing the review will post: the review's own text, or a comment on a line.
|
||||
pub(super) struct Item {
|
||||
/// What the pane heads it with: `Summary`, or where on the diff it lands
|
||||
pub(super) place: String,
|
||||
/// Where a line comment sits, `None` for the summary
|
||||
pub(super) on: Option<OnLines>,
|
||||
/// The text GitHub will show, as markdown; empty for a review with no words of its own
|
||||
pub(super) body: String,
|
||||
}
|
||||
|
||||
/// The lines a comment sits on.
|
||||
pub(super) struct OnLines {
|
||||
pub(super) path: String,
|
||||
pub(super) side: DiffSide,
|
||||
pub(super) start: u32,
|
||||
pub(super) end: u32,
|
||||
}
|
||||
|
||||
impl OnLines {
|
||||
fn of(place: &Placement) -> Option<Self> {
|
||||
match place {
|
||||
Placement::Line { path, line, side } => {
|
||||
Some(Self { path: path.clone(), side: *side, start: *line, end: *line })
|
||||
}
|
||||
Placement::Lines { path, start, end, side } => {
|
||||
Some(Self { path: path.clone(), side: *side, start: *start, end: *end })
|
||||
}
|
||||
Placement::Body(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// What the list calls it under its file's name: `line 13`, `lines 12–14 (before)`.
|
||||
fn short(&self) -> String {
|
||||
let before = if self.side == DiffSide::Old { " (before)" } else { "" };
|
||||
if self.start == self.end {
|
||||
format!("line {}{before}", self.end)
|
||||
} else {
|
||||
format!("lines {}–{}{before}", self.start, self.end)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The review, then each comment on a line, grouped by file the way GitHub lists them: files in
|
||||
/// the order their first comment was written, and down each file by line.
|
||||
///
|
||||
/// A comment placed in the review's body is part of the review's text on GitHub, under a heading
|
||||
/// that says where it was written, so it is read there rather than listed a second time.
|
||||
pub(super) fn items(app: &App, plan: &PostPlan) -> Vec<Item> {
|
||||
let body_of = |id: &FindingId| {
|
||||
app.review
|
||||
.session()
|
||||
.and_then(|session| session.findings.iter().find(|finding| &finding.id == id))
|
||||
.map_or_else(String::new, |finding| finding.body.trim().to_owned())
|
||||
};
|
||||
let mut lines: Vec<Item> = plan
|
||||
.placed
|
||||
.iter()
|
||||
.filter_map(|placed| {
|
||||
let on = OnLines::of(&placed.place)?;
|
||||
Some(Item {
|
||||
place: place(&placed.place),
|
||||
on: Some(on),
|
||||
body: body_of(&placed.finding_id),
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let mut files: Vec<String> = Vec::new();
|
||||
for item in &lines {
|
||||
let path = &item.on.as_ref().expect("a line comment").path;
|
||||
if !files.contains(path) {
|
||||
files.push(path.clone());
|
||||
}
|
||||
}
|
||||
lines.sort_by_key(|item| {
|
||||
let on = item.on.as_ref().expect("a line comment");
|
||||
(files.iter().position(|file| file == &on.path), on.start)
|
||||
});
|
||||
let summary =
|
||||
Item { place: "Summary".to_owned(), on: None, body: plan.review.body.trim().to_owned() };
|
||||
std::iter::once(summary).chain(lines).collect()
|
||||
}
|
||||
|
||||
/// Where a comment lands, as a reader of GitHub's page would look for it.
|
||||
fn place(place: &Placement) -> String {
|
||||
let before = |side: &DiffSide| if *side == DiffSide::Old { " (before)" } else { "" };
|
||||
match place {
|
||||
Placement::Line { path, line, side } => format!("{path}:{line}{}", before(side)),
|
||||
Placement::Lines { path, start, end, side } => {
|
||||
format!("{path}:{start}–{end}{}", before(side))
|
||||
}
|
||||
Placement::Body(BodyReason::WholeReview) => "on the review".to_owned(),
|
||||
Placement::Body(BodyReason::Chapter(title)) => format!("under “{title}”"),
|
||||
Placement::Body(BodyReason::OffDiff { path, lines }) => {
|
||||
format!("{path}:{lines}, outside GitHub's diff")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The list's rows: the summary, then each file's name over the comments on it. Each row carries
|
||||
/// the item it selects, or `None` for a file's name, which selects nothing.
|
||||
pub(super) fn list_lines(
|
||||
app: &App,
|
||||
items: &[Item],
|
||||
selected: usize,
|
||||
width: usize,
|
||||
) -> Vec<(Option<usize>, Line<'static>)> {
|
||||
let styles = &app.theme_styles;
|
||||
let room = width.saturating_sub(crate::widgets::SELECTION_FRAME);
|
||||
let mut rows = Vec::new();
|
||||
let mut file: Option<&str> = None;
|
||||
for (index, item) in items.iter().enumerate() {
|
||||
let label = match &item.on {
|
||||
None => item.place.clone(),
|
||||
Some(on) => {
|
||||
if file != Some(on.path.as_str()) {
|
||||
file = Some(on.path.as_str());
|
||||
let name = clip_terminal_cells_ellipsis(&on.path, room);
|
||||
// In the column a selected row's label starts in
|
||||
let name = format!("{}{name}", " ".repeat(crate::widgets::SELECTION_INSET));
|
||||
rows.push((None, Line::from(Span::styled(name, styles.muted))));
|
||||
}
|
||||
format!(" {}", on.short())
|
||||
}
|
||||
};
|
||||
let label = clip_terminal_cells_ellipsis(&label, room);
|
||||
rows.push((
|
||||
Some(index),
|
||||
crate::widgets::selection_row(
|
||||
vec![Span::styled(label, styles.text)],
|
||||
width,
|
||||
index == selected,
|
||||
styles,
|
||||
),
|
||||
));
|
||||
}
|
||||
rows
|
||||
}
|
||||
135
crates/luxe-tui/src/view/guided_review/github_post/preview.rs
Normal file
135
crates/luxe-tui/src/view/guided_review/github_post/preview.rs
Normal file
@@ -0,0 +1,135 @@
|
||||
//! Post preview — the selected item as GitHub will show it: its Markdown and the code it is on.
|
||||
|
||||
use super::items::{Item, OnLines};
|
||||
use crate::app::App;
|
||||
use crate::guided_review::diff::clip_terminal_cells_ellipsis;
|
||||
use luxe_core::guided_review::DiffSide;
|
||||
use ratatui::style::Style;
|
||||
use ratatui::text::{Line, Span};
|
||||
|
||||
/// Diff lines above a line comment, its own included, the way GitHub shows a comment's context.
|
||||
const EXCERPT_LINES: usize = 4;
|
||||
|
||||
/// The most lines of a comment on a run of lines the excerpt shows, ending at the run's last.
|
||||
const EXCERPT_MOST: usize = 8;
|
||||
|
||||
/// The selected item's text as rows of `width`: where it goes, the diff it sits under when it sits
|
||||
/// on lines, then the words.
|
||||
pub(super) fn pane_lines(app: &App, item: &Item, width: usize) -> Vec<Line<'static>> {
|
||||
let styles = &app.theme_styles;
|
||||
let mut lines =
|
||||
vec![Line::from(Span::styled(item.place.clone(), styles.accent)), Line::from("")];
|
||||
if let Some(on) = &item.on {
|
||||
let excerpt = excerpt(app, on, width);
|
||||
if !excerpt.is_empty() {
|
||||
lines.extend(excerpt);
|
||||
lines.push(Line::from(""));
|
||||
}
|
||||
}
|
||||
let post = app.guided_review_workspace.github_post.as_ref();
|
||||
let summary = item.on.is_none();
|
||||
let drafting = post.and_then(|post| post.drafting_since).filter(|_| summary);
|
||||
if let Some(since) = drafting {
|
||||
lines.push(drafting_line(app, since));
|
||||
if !item.body.is_empty() {
|
||||
lines.push(Line::from(""));
|
||||
}
|
||||
}
|
||||
if item.body.is_empty() {
|
||||
if drafting.is_none() {
|
||||
lines.push(Line::from(Span::styled("No summary · c write one", styles.dim)));
|
||||
}
|
||||
} else {
|
||||
lines.extend(crate::markdown::render_markdown(&item.body, styles));
|
||||
}
|
||||
let mut lines: Vec<Line<'static>> =
|
||||
lines.iter().flat_map(|line| crate::widgets::wrap_line(line, width)).collect();
|
||||
if let Some(note) = post.and_then(|post| post.draft_note.as_deref()).filter(|_| summary) {
|
||||
lines.push(Line::from(""));
|
||||
lines.extend(crate::github_detail::draft_note_lines(note, width, styles));
|
||||
}
|
||||
lines
|
||||
}
|
||||
|
||||
/// The wait for a drafted summary, drawn where the summary will land: who is writing it, and a
|
||||
/// count, because a minute of a still line reads as a hang.
|
||||
fn drafting_line(app: &App, since: std::time::Instant) -> Line<'static> {
|
||||
let styles = &app.theme_styles;
|
||||
let model = match app.status.model.trim() {
|
||||
"" => "The agent",
|
||||
model => model,
|
||||
};
|
||||
Line::from(vec![
|
||||
Span::styled(
|
||||
format!("{} drafting ", crate::statusline::spinner(app.guided_review_workspace.frame)),
|
||||
styles.accent,
|
||||
),
|
||||
Span::styled(
|
||||
format!(
|
||||
"\u{b7} {model} is drafting the review summary \u{b7} {}",
|
||||
crate::statusline::format_elapsed_compact(
|
||||
app.frame_now().saturating_duration_since(since)
|
||||
)
|
||||
),
|
||||
styles.muted,
|
||||
),
|
||||
])
|
||||
}
|
||||
|
||||
/// The diff a line comment sits under, as GitHub shows it: up to [`EXCERPT_LINES`] lines ending at
|
||||
/// the comment's last, numbered and colored the way the review draws them, with its own lines
|
||||
/// marked. Empty when the lines are not in the review, which leaves the heading to say where.
|
||||
fn excerpt(app: &App, on: &OnLines, width: usize) -> Vec<Line<'static>> {
|
||||
let styles = &app.theme_styles;
|
||||
let number = |line: &luxe_core::guided_review::ReviewLine| match on.side {
|
||||
DiffSide::Old => line.old_line,
|
||||
DiffSide::New => line.new_line,
|
||||
};
|
||||
let Some(hunk_lines) = app.guided_review.as_ref().and_then(|review| {
|
||||
let file = review.files.iter().find(|file| {
|
||||
let path = match on.side {
|
||||
DiffSide::Old => file.old_path.as_deref(),
|
||||
DiffSide::New => file.new_path.as_deref(),
|
||||
};
|
||||
path.is_some_and(|path| luxe_core::paths::display(path) == on.path)
|
||||
})?;
|
||||
file.hunks.iter().find_map(|hunk| {
|
||||
let end = hunk.lines.iter().position(|line| number(line) == Some(on.end))?;
|
||||
let start = hunk.lines.iter().position(|line| number(line) == Some(on.start));
|
||||
let from = start
|
||||
.unwrap_or(end)
|
||||
.min(end.saturating_sub(EXCERPT_LINES - 1))
|
||||
.max((end + 1).saturating_sub(EXCERPT_MOST));
|
||||
Some(&hunk.lines[from..=end])
|
||||
})
|
||||
}) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let digits = hunk_lines
|
||||
.iter()
|
||||
.filter_map(|line| line.new_line.or(line.old_line))
|
||||
.max()
|
||||
.map_or(1, |most| most.to_string().len());
|
||||
hunk_lines
|
||||
.iter()
|
||||
.map(|line| {
|
||||
use luxe_core::guided_review::ReviewLineKind;
|
||||
let (sign, fill) = match line.kind {
|
||||
ReviewLineKind::Addition => ("+", styles.diff_add_bg),
|
||||
ReviewLineKind::Deletion => ("-", styles.diff_del_bg),
|
||||
_ => (" ", Style::default()),
|
||||
};
|
||||
let commented = number(line).is_some_and(|n| (on.start..=on.end).contains(&n));
|
||||
let shown = line.new_line.or(line.old_line).map_or_else(String::new, |n| n.to_string());
|
||||
let lead = format!("{shown:>digits$} {sign} ");
|
||||
let room = width.saturating_sub(1 + lead.chars().count());
|
||||
let text = clip_terminal_cells_ellipsis(&line.text.replace('\t', " "), room);
|
||||
let pad = room.saturating_sub(unicode_width::UnicodeWidthStr::width(text.as_str()));
|
||||
Line::from(vec![
|
||||
Span::styled(if commented { "▎" } else { " " }, styles.accent),
|
||||
Span::styled(lead, styles.muted.patch(fill)),
|
||||
Span::styled(format!("{text}{}", " ".repeat(pad)), styles.text.patch(fill)),
|
||||
])
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
Reference in New Issue
Block a user