From c5e4cf27d1672cd12e28e13695fd400c362cc5df Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 22 Sep 2026 00:31:23 +0200 Subject: [PATCH] refactor: buffer inspection output --- AGENTS-TASKS.md | 2 + bin_efo/src/commands/show.rs | 60 +++++++++++++++++---------- docs/issue-audit.md | 2 +- lib_efo/src/entity/implementations.rs | 10 +++-- lib_efo/src/lib.rs | 16 +++++-- 5 files changed, 59 insertions(+), 31 deletions(-) diff --git a/AGENTS-TASKS.md b/AGENTS-TASKS.md index 0f0dfde..8c9dd17 100644 --- a/AGENTS-TASKS.md +++ b/AGENTS-TASKS.md @@ -248,6 +248,8 @@ These require user approval and are not part of the current implementation: buffered CLI renderer (tracker issues #11 and part of #10). - [x] Add ephemeral UUID/name `EntityIndex` lookup and duplicate detection while preserving ordered vectors as the stable repository representation (#6). +- [x] Make full entity, unified show, epoch show, and diff presentation return + buffered strings before the CLI writes them (tracker issue #10). - [ ] Tracker issue #13 is intentionally deferred at the maintainer's request. - The old README described organisation/link/event interfaces absent from the diff --git a/bin_efo/src/commands/show.rs b/bin_efo/src/commands/show.rs index 6f76338..0293ab9 100644 --- a/bin_efo/src/commands/show.rs +++ b/bin_efo/src/commands/show.rs @@ -79,7 +79,8 @@ pub struct ShowArgs { pub fn handle_show_cmd(args: &ShowArgs) -> Result<(), AppError> { let workspace = Workspace::open(Path::new("."))?; if matches!(args.kind, ShowKind::Epoch) { - return render_epochs(args, &workspace); + print!("{}", render_epochs(args, &workspace)?); + return Ok(()); } let world = args .epoch @@ -89,12 +90,13 @@ pub fn handle_show_cmd(args: &ShowArgs) -> Result<(), AppError> { .map(|id| workspace.load_epoch(id)) .transpose()? .unwrap_or_else(|| workspace.worktree.world.clone()); - render_entities(args, &world); + print!("{}", render_entities(args, &world)); Ok(()) } /// Render selected entity collections and filters. -fn render_entities(args: &ShowArgs, world: &EpochInfo) { +fn render_entities(args: &ShowArgs, world: &EpochInfo) -> String { + let mut output = String::new(); let selected: &[(&str, EntityType)] = match args.kind { ShowKind::All => &[ ("Characters", EntityType::Character), @@ -114,31 +116,39 @@ fn render_entities(args: &ShowArgs, world: &EpochInfo) { }; for (section, (title, entity_type)) in selected.iter().enumerate() { if section > 0 { - println!(); + output.push('\n'); } let matches = world .vec_entities(entity_type) .iter() .filter(|entity| matches_filters(entity, args)) .collect::>(); - println!("{title} ({})", matches.len()); + output.push_str(&format!("{title} ({})\n", matches.len())); if matches.is_empty() { - println!(" — none"); + output.push_str(" — none\n"); continue; } for entity in matches { match args.detail { - ShowDetail::Short => println!("{}", entity.short_display(world)), + ShowDetail::Short => { + output.push_str(&entity.short_display(world)); + output.push('\n'); + } ShowDetail::Long => { - println!("{}", entity.short_display(world)); + output.push_str(&entity.short_display(world)); + output.push('\n'); if !entity.description.trim().is_empty() { - println!(" {}", entity.description); + output.push_str(&format!(" {}\n", entity.description)); } } - ShowDetail::Full => entity.print_full(world), + ShowDetail::Full => { + output.push_str(&entity.full_display(world)); + output.push('\n'); + } } } } + output } /// Apply all requested entity predicates. @@ -159,7 +169,8 @@ fn matches_filters(entity: &Entity, args: &ShowArgs) -> bool { } /// Render one epoch or every stored epoch in numeric order. -fn render_epochs(args: &ShowArgs, workspace: &Workspace) -> Result<(), AppError> { +fn render_epochs(args: &ShowArgs, workspace: &Workspace) -> Result { + let mut output = String::new(); let ids = if let Some(identifier) = &args.epoch { vec![resolve(workspace, identifier)?] } else { @@ -178,11 +189,13 @@ fn render_epochs(args: &ShowArgs, workspace: &Workspace) -> Result<(), AppError> " " }; match args.detail { - ShowDetail::Short => println!("{head} epoch {id} — {}", message(&epoch)), + ShowDetail::Short => { + output.push_str(&format!("{head} epoch {id} — {}\n", message(&epoch))); + } ShowDetail::Long => { - println!("{head} epoch {id} — {}", message(&epoch)); - println!( - " parent: {} · entities: {} · tags: {}", + output.push_str(&format!("{head} epoch {id} — {}\n", message(&epoch))); + output.push_str(&format!( + " parent: {} · entities: {} · tags: {}\n", epoch .parent_epoch .map_or_else(|| "—".into(), |id| id.to_string()), @@ -192,18 +205,21 @@ fn render_epochs(args: &ShowArgs, workspace: &Workspace) -> Result<(), AppError> } else { epoch.tags.to_string() } - ); + )); if !epoch.summary.is_empty() { - println!(" summary: {}", epoch.summary); + output.push_str(&format!(" summary: {}\n", epoch.summary)); } } - ShowDetail::Full => println!( - "{}", - serde_json::to_string_pretty(&epoch).map_err(lib_efo::error::StorageError::from)? - ), + ShowDetail::Full => { + output.push_str( + &serde_json::to_string_pretty(&epoch) + .map_err(lib_efo::error::StorageError::from)?, + ); + output.push('\n'); + } } } - Ok(()) + Ok(output) } /// Resolve a numeric epoch ID or exact tag. diff --git a/docs/issue-audit.md b/docs/issue-audit.md index 837ff89..989acd3 100644 --- a/docs/issue-audit.md +++ b/docs/issue-audit.md @@ -17,7 +17,7 @@ absent. | [#13 Add all cards into pre-events](https://proxy.dandaedre.com/gitea/idle-org/epoch_forge/issues/13) | Explicitly deferred by the maintainer for this milestone. | Revisit only when requested. | | [#12 Add fight/war](https://proxy.dandaedre.com/gitea/idle-org/epoch_forge/issues/12) | Open simulation feature. | Model it as configured events/relationships after custom fields and influx semantics settle. | | [#11 Epoch Diffing](https://proxy.dandaedre.com/gitea/idle-org/epoch_forge/issues/11) | Addressed: epoch and layer comparisons share a deterministic, serializable typed diff with changed-field details and importance classification. | Extend the type rather than parsing terminal output when structured formats arrive. | -| [#10 Display functions](https://proxy.dandaedre.com/gitea/idle-org/epoch_forge/issues/10) | Partially addressed: unified show routing and three detail levels are coherent, and every diff is rendered into a buffer before one output write. Older entity renderers still print directly. | Finish the writer/buffer boundary for `show`. | +| [#10 Display functions](https://proxy.dandaedre.com/gitea/idle-org/epoch_forge/issues/10) | Addressed for inspection: unified `show` and diff rendering build complete strings before a single output write, and `Displayable::full_display` no longer forces library output through `println!`. | Apply the same boundary opportunistically to mutation confirmations. | | [#9 Adding influx](https://proxy.dandaedre.com/gitea/idle-org/epoch_forge/issues/9) | Open and underspecified. | Define versioned deterministic severity and avoidance rules before commands. | | [#8 Event diffusion](https://proxy.dandaedre.com/gitea/idle-org/epoch_forge/issues/8) | Planned in the simulation roadmap; blocked on roads, residency, and deterministic routing. | Complete simulation checkpoints 2–3 in order. | | [#6 HashMap/HashSet](https://proxy.dandaedre.com/gitea/idle-org/epoch_forge/issues/6) | Addressed without format churn: `EntityIndex` provides ephemeral UUID/name hash indexes and duplicate-ID detection over the stable vector representation. Repository validation uses the index and hash lookup for relationships. | Rebuild indexes after mutation; never persist them. | diff --git a/lib_efo/src/entity/implementations.rs b/lib_efo/src/entity/implementations.rs index 75a30ca..62afb7e 100644 --- a/lib_efo/src/entity/implementations.rs +++ b/lib_efo/src/entity/implementations.rs @@ -17,11 +17,13 @@ mod methods; // ---------- Implementation ---------- impl Displayable for Entity { // Detailed multi-line view (used e.g. in logs) - fn print_full(&self, epoch_info: &EpochInfo) { - println!("{}", self.to_pretty_tree(epoch_info),); - if let Ok(c) = self.stats_to_table() { - println!("{c}"); + fn full_display(&self, epoch_info: &EpochInfo) -> String { + let mut output = self.to_pretty_tree(epoch_info); + if let Ok(table) = self.stats_to_table() { + output.push('\n'); + output.push_str(&table.to_string()); } + output } /// Compact human summary with stable identity and useful world context. diff --git a/lib_efo/src/lib.rs b/lib_efo/src/lib.rs index 865d31c..000c8ed 100644 --- a/lib_efo/src/lib.rs +++ b/lib_efo/src/lib.rs @@ -67,9 +67,14 @@ pub mod worldgen; /// This allows you to call `full_display`, `short_display` or `oneline_display` /// directly on a vector of displayable items. pub trait Displayable { - /// Return a fully-described string representation. + /// Print the fully-described representation for legacy callers. fn print_full(&self, epoch_info: &EpochInfo) { - println!("{}", self.short_display(epoch_info)); + println!("{}", self.full_display(epoch_info)); + } + + /// Return a fully-described string representation. + fn full_display(&self, epoch_info: &EpochInfo) -> String { + self.short_display(epoch_info) } /// Return a short, user-friendly string. Resolves Ids @@ -90,8 +95,11 @@ pub trait Displayable { /// already implements `Displayable`. This is a **blanket implementation**, /// so you do not need to write a manual impl for each concrete type impl Displayable for Vec { - fn print_full(&self, epoch_info: &EpochInfo) { - self.iter().for_each(|e| e.print_full(epoch_info)); + fn full_display(&self, epoch_info: &EpochInfo) -> String { + self.iter() + .map(|entity| entity.full_display(epoch_info)) + .collect::>() + .join("\n") } fn short_display(&self, epoch_info: &EpochInfo) -> String {