use anyhow::Result;
use ratatui::Frame;
use ratatui::layout::{Constraint, Layout, Rect};
use plannotator_tui_review::Side;
use super::diff::Lines;
use super::files::fit;
use super::{Draft, Focus, Geometry, Mode, Screen};
use crate::ui::compose;
const RAIL_WIDTH: u16 = 36;
pub(super) const RAIL_MIN_WIDTH: u16 = 28;
pub(super) const RAIL_MIN_TOTAL_WIDTH: u16 = 80;
const PR_TITLE_MAX: usize = 48;
impl Screen {
pub(crate) fn draw(&mut self, frame: &mut Frame) -> Result<()> {
self.take_whitespace()?;
self.track_current();
let area = frame.area();
let [header, body, footer] = Layout::vertical([
Constraint::Length(1),
Constraint::Min(1),
Constraint::Length(self.footer_height(area.width)),
])
.areas(area);
let mut rail_width = if self.rail_shown() && area.width >= RAIL_MIN_TOTAL_WIDTH {
let fitted = (area.width * 3 / 10).clamp(RAIL_MIN_WIDTH, RAIL_WIDTH);
self.dividers.notes_width(fitted, body.width, self.files_wanted())
} else {
0
};
let focused = self.focus == Focus::Files;
let wanted = self.files_wanted();
let content = if wanted { self.shown_width() } else { 0 };
let fits = |room: u16, focused| fit::width(content, room, self.files.chosen_width(), focused);
let mut files_width = None;
if wanted {
files_width = fits(body.width.saturating_sub(rail_width + u16::from(rail_width > 0)), focused);
if files_width.is_none() && focused && rail_width > 0 {
rail_width = 0;
files_width = fits(body.width, focused);
}
}
let [main, _gap, rail] = Layout::horizontal([
Constraint::Min(1),
Constraint::Length(u16::from(rail_width > 0)),
Constraint::Length(rail_width),
])
.areas(body);
self.files.no_room = wanted && fits(body.width, true).is_none();
if files_width.is_none() && focused {
self.focus = Focus::Diff;
}
self.files.drawn(files_width.unwrap_or(0));
let (files, diff) = match files_width {
Some(width) => {
let [files, _divider, diff] = Layout::horizontal([
Constraint::Length(width),
Constraint::Length(1),
Constraint::Min(1),
])
.areas(main);
(files, diff)
}
None => (Rect::default(), main),
};
self.geometry = Geometry { screen: area, diff, rail, files, ..Geometry::default() };
self.dividers.notes_drawn(rail_width);
self.dividers.place(files, rail);
self.decide_auto(area.width, diff.width)?;
self.draw_header(frame, header);
let cursor_selected = self.cursor_selected();
if let Some(whole) = &mut self.whole {
whole.draw(frame, diff)?;
} else {
self.sync_boxes();
self.diff.draw(frame, diff)?;
if self.sync_boxes() {
self.diff.draw(frame, diff)?;
}
}
let (middle, code) = match self.active_diff_mut() {
Some(shown) => {
let middle = if shown.split() { shown.split_line() } else { None };
(middle, (diff.top()..diff.bottom()).map(|y| shown.code_row(y)).collect())
}
None => (None, Vec::new()),
};
let code = |y: u16| code.get(usize::from(y - diff.y)).copied().unwrap_or(true);
let selected = super::mouse::tint_selection(frame.buffer_mut(), diff, middle, cursor_selected, code);
self.draw_boxes(frame);
self.tell_saved_below();
if files.width > 0 {
let listing = self.listing();
self.draw_files(frame, files, &listing);
}
if rail_width > 0 {
self.draw_rail(frame, rail);
}
self.dividers.draw(frame);
self.draw_footer(frame, footer);
if !matches!(self.mode, Mode::Compose(_)) {
self.compose_at = None;
}
match &self.mode {
Mode::Compose(draft) => {
let draft = draft.clone();
self.draw_compose(frame, diff, &draft);
}
Mode::Menu(cursor) => self.draw_menu(frame, *cursor),
Mode::Settings(cursor) => self.draw_settings(frame, *cursor),
Mode::PickTarget => {
let root = self.real_root();
self.picker.draw(frame, area, &self.keys, &root, self.start_delivery.as_deref());
}
Mode::Help => self.draw_help(frame, area),
Mode::Pick(pick) => {
let pick = pick.clone();
self.draw_pick(frame, diff, &pick);
}
Mode::Post(confirm) => {
let confirm = confirm.clone();
self.draw_post(frame, area, &confirm);
}
Mode::Browse => self.draw_toolbar(frame, diff, selected),
Mode::Kinds(cursor) => self.draw_kinds(frame, *cursor),
Mode::ConfirmQuit | Mode::FilterFiles => {}
}
self.draw_split_line(frame, diff);
Ok(())
}
pub(super) fn scope_title(&self) -> String {
let Some(pull) = &self.capture.pull else { return self.capture.title() };
let view = &pull.view;
let mut title: String = view.title.chars().take(PR_TITLE_MAX).collect();
if view.title.chars().count() > PR_TITLE_MAX {
title.push('\u{2026}');
}
let title = if title.is_empty() { String::new() } else { format!(" \u{b7} {title}") };
format!("PR #{}{title} \u{b7} {} \u{2190} {}", view.number, view.base_ref, view.head_ref)
}
fn draw_compose(&mut self, frame: &mut Frame, diff: Rect, draft: &Draft) {
let rect = self.compose_rect(diff, draft);
let verb = match draft {
Draft::Comment(lines) => {
format!("comment on {}", lines_label(lines))
}
Draft::Suggestion(lines) => {
format!("suggest code for {}", lines_label(lines))
}
Draft::File(path) => format!("note on {path}"),
Draft::Change => "comment on the whole change".to_owned(),
Draft::Outside(lines, _) => format!("comment on {} (not in the diff)", lines_label(lines)),
Draft::Edit(_) => "edit".to_owned(),
};
let title = self.compose.heading(&verb, self.shift_enter, &self.keys);
let (text, first) = compose::draw(frame, rect, &self.compose, &title);
self.geometry.compose_wrap = usize::from(text.width.saturating_sub(1)).max(1);
self.geometry.compose_text = Some((text, first));
}
}
fn lines_label(lines: &Lines) -> String {
let Lines { path, side, first, last } = lines;
let at = if first == last { format!("{path}:{first}") } else { format!("{path}:{first}-{last}") };
if *side == Side::Old { format!("{at} old") } else { at }
}
pub(super) fn count(notes: usize) -> String {
if notes == 1 { "1 note".to_owned() } else { format!("{notes} notes") }
}