refactor: buffer inspection output

This commit is contained in:
Dandaedre 2026-09-22 00:31:23 +02:00
parent f36a339d5c
commit c5e4cf27d1
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
5 changed files with 59 additions and 31 deletions

View File

@ -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

View File

@ -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::<Vec<_>>();
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<String, AppError> {
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.

View File

@ -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. |

View File

@ -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.

View File

@ -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<T: Displayable> Displayable for Vec<T> {
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::<Vec<_>>()
.join("\n")
}
fn short_display(&self, epoch_info: &EpochInfo) -> String {