use egaku::{Column, SortKey, SortOrder, TableRow, TableView};
use metsuke::{Involvement, PrSummary};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PrRow {
pr: PrSummary,
id: String,
repo: String,
number: String,
title: String,
}
pub const SORT_COLUMN: &str = "UPDATED";
pub const COLUMNS: &[&str] = &["REPO", "PR", "TITLE", "AUTHOR", "STATE", "UPDATED"];
impl PrRow {
#[must_use]
pub fn new(pr: PrSummary) -> Self {
let mut id = pr.repo.to_string();
id.push('#');
id.push_str(&pr.number.0.to_string());
let title = if pr.draft {
let mut t = String::from("[draft] ");
t.push_str(&pr.title);
t
} else {
pr.title.clone()
};
Self {
id,
repo: pr.repo.to_string(),
number: pr.number.to_string(),
title,
pr,
}
}
#[must_use]
pub fn pr(&self) -> &PrSummary {
&self.pr
}
}
impl TableRow for PrRow {
fn identity(&self) -> &str {
&self.id
}
fn cell(&self, field: &str) -> Option<&str> {
match field {
"REPO" => Some(&self.repo),
"PR" => Some(&self.number),
"TITLE" => Some(&self.title),
"AUTHOR" => Some(&self.pr.author),
"STATE" => Some(self.pr.state.label()),
"UPDATED" => Some(&self.pr.updated_at),
_ => None,
}
}
}
pub struct Board {
table: TableView<PrRow>,
lane: Involvement,
}
impl Board {
#[must_use]
pub fn new(lane: Involvement) -> Self {
let cols: Vec<Column> = COLUMNS.iter().map(|n| Column::new(*n, *n)).collect();
let sort = SortKey::new(SORT_COLUMN, SortOrder::Desc);
Self {
table: TableView::new(cols, Vec::new(), sort)
.expect("pauta's own columns must resolve against PrRow"),
lane,
}
}
pub fn set_rows(&mut self, prs: Vec<PrSummary>) {
self.table
.set_rows(prs.into_iter().map(PrRow::new).collect());
}
#[must_use]
pub fn lane(&self) -> Involvement {
self.lane
}
pub fn set_lane(&mut self, lane: Involvement) {
self.lane = lane;
}
#[must_use]
pub fn view(&self) -> &TableView<PrRow> {
&self.table
}
pub fn view_mut(&mut self) -> &mut TableView<PrRow> {
&mut self.table
}
#[must_use]
pub fn selected(&self) -> Option<&PrSummary> {
self.table.selected_row().map(PrRow::pr)
}
#[must_use]
pub fn len(&self) -> usize {
self.table.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
#[cfg(test)]
mod tests {
use super::*;
use metsuke::{PrNumber, PrState, RepoId};
fn pr(n: u64, repo: &str, draft: bool) -> PrSummary {
let mut ts = String::from("2026-08-03T00:00:");
if n < 10 {
ts.push('0');
}
ts.push_str(&n.to_string());
ts.push('Z');
PrSummary {
repo: RepoId::parse(repo).unwrap(),
number: PrNumber(n),
title: "a change".into(),
author: "drzzln".into(),
state: PrState::Open,
draft,
updated_at: ts,
url: String::new(),
}
}
#[test]
fn identity_spans_repos_so_two_same_numbered_prs_stay_distinct() {
let a = PrRow::new(pr(7, "pleme-io/metsuke", false));
let b = PrRow::new(pr(7, "pleme-io/banken", false));
assert_ne!(
a.identity(),
b.identity(),
"a docket spans repos; #7 in two repos must not collide"
);
}
#[test]
fn draft_rides_the_title_not_the_state_column() {
let r = PrRow::new(pr(1, "pleme-io/metsuke", true));
assert!(r.cell("TITLE").unwrap().starts_with("[draft]"));
assert_eq!(
r.cell("STATE").unwrap(),
"open",
"STATE must keep saying open/merged/closed — draft is readiness, not state"
);
}
#[test]
fn the_selection_survives_a_refresh_that_inserts_above_it() {
let mut b = Board::new(Involvement::ReviewRequested);
b.set_rows(vec![pr(1, "a/b", false), pr(2, "a/b", false)]);
b.view_mut().select_next();
let before = b.selected().unwrap().number;
assert_eq!(before, PrNumber(1), "row 1 under UPDATED-desc is #1");
b.set_rows(vec![
pr(9, "a/b", false),
pr(1, "a/b", false),
pr(2, "a/b", false),
]);
assert_eq!(
b.selected().unwrap().number,
PrNumber(1),
"an index-preserving refresh would have moved the cursor onto a \
different PR under the operator's hands"
);
}
#[test]
fn every_column_resolves_to_something() {
let r = PrRow::new(pr(3, "pleme-io/metsuke", false));
for c in COLUMNS {
assert!(
r.cell(c).is_some_and(|v| !v.is_empty()),
"column {c} resolved to nothing — a header with no field behind it"
);
}
}
}