Display shenenigans
Build Successful The build was successful
Gitea/epoch_forge/pipeline/head This commit looks good
Details
Build Successful The build was successful
Gitea/epoch_forge/pipeline/head This commit looks good
Details
This commit is contained in:
parent
62ff40dc6a
commit
920c299a0c
|
|
@ -293,14 +293,15 @@ pub(crate) fn handle_create_entity(
|
|||
// Show success
|
||||
let vec_entities: &mut Vec<Entity> = epoch_info.vec_entities_mut(e_type);
|
||||
info!("Created character: {:#?}", entity);
|
||||
entity.print_full();
|
||||
let message = &crate::success::SuccessMessages::CreatedEntity(
|
||||
entity.name.clone(),
|
||||
entity.entity_type.to_shortest(),
|
||||
entity.id.to_string(),
|
||||
);
|
||||
vec_entities.push(entity);
|
||||
// TODO: Rather bad
|
||||
vec_entities.push(entity.clone());
|
||||
|
||||
entity.print_full(epoch_info);
|
||||
// Save changes
|
||||
epoch_info.save_with_context("Created entity", &lib_efo::epoch::SaveMode::Erase)?;
|
||||
display_success(message);
|
||||
|
|
@ -361,7 +362,7 @@ fn handle_diff_entity(
|
|||
|
||||
let diff = current_entity.delta(past_entity);
|
||||
display_success(&crate::success::SuccessMessages::DiffBetweenEpoch(id, id2));
|
||||
diff.print_full();
|
||||
diff.print_full(epoch_current);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -374,12 +375,13 @@ fn handle_show_entity(
|
|||
) -> Result<(), EpochError> {
|
||||
let epoch_info = main_epoch.try_load_current()?;
|
||||
if entity_args.identifier.is_none() {
|
||||
for entity in epoch_info.vec_entities(e_type) {
|
||||
entity.print_oneline();
|
||||
}
|
||||
println!(
|
||||
"{}",
|
||||
epoch_info.vec_entities(e_type).short_display(epoch_info)
|
||||
);
|
||||
} else {
|
||||
let entity = epoch_info.find_entity_of_type(&entity_args.identifier, e_type)?;
|
||||
entity.print_full();
|
||||
entity.print_full(epoch_info);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -391,6 +393,7 @@ fn handle_edit_entity(
|
|||
main_epoch: &mut EpochConfig,
|
||||
) -> Result<(), EpochError> {
|
||||
let epoch_info = main_epoch.try_load_current_as_mut()?;
|
||||
let epoch_info1 = &epoch_info.clone(); // Very expensive save of all variables
|
||||
let position = if let Some(position) = &entity_args.common_args.position {
|
||||
Some(epoch_info.resolve_position(position)?)
|
||||
} else {
|
||||
|
|
@ -414,13 +417,14 @@ fn handle_edit_entity(
|
|||
|
||||
helper_functions::update_common_values(&entity_args.common_args, position, entity);
|
||||
|
||||
entity.print_full();
|
||||
let message = &crate::success::SuccessMessages::EditedEntity(
|
||||
entity.name.clone(),
|
||||
entity.entity_type.to_shortest(),
|
||||
entity.id.to_string(),
|
||||
);
|
||||
entity.print_full(epoch_info1);
|
||||
epoch_info.save_with_context("Deleted entity", &lib_efo::epoch::SaveMode::Erase)?;
|
||||
// let epoch_info = main_epoch.try_load_current()?;
|
||||
|
||||
display_success(message);
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
//! Used to display structures with various degree of verbosity
|
||||
|
||||
use clap::Subcommand;
|
||||
use lib_efo::{Displayable, entity::Entity, naming::NAMING};
|
||||
use lib_efo::{Displayable, entity::Entity, epoch::EpochInfo, naming::NAMING};
|
||||
|
||||
use crate::error::AppError;
|
||||
#[derive(Subcommand, Debug)]
|
||||
|
|
@ -36,12 +36,12 @@ pub fn handle_show_cmd(cmd: &ShowCmd) -> Result<(), AppError> {
|
|||
println!("{}", e.oneline_display());
|
||||
println!("{}", e.oneline_display());
|
||||
println!("\n\n\n SHORT");
|
||||
e.print_short();
|
||||
e.short_display(&EpochInfo::default());
|
||||
|
||||
println!("\n\n\n LONG");
|
||||
e.print_full();
|
||||
e.print_full(&EpochInfo::default());
|
||||
println!("\n\n\n TREE");
|
||||
println!("{}", e.to_pretty_tree());
|
||||
println!("{}", e.to_pretty_tree(&EpochInfo::default()));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ use rand::seq::{IndexedRandom, IteratorRandom};
|
|||
use uuid::Uuid; // [3] - colored-rs examples
|
||||
|
||||
use crate::entity::{EffectApplication, EffectBounds, Entity, EntityDuration, EntityIdentifier};
|
||||
use crate::epoch::EpochInfo;
|
||||
use crate::epoch::epoch_traits::Diffable;
|
||||
use crate::error::EpochError;
|
||||
use crate::stats::{
|
||||
|
|
@ -22,14 +23,15 @@ use crate::{Displayable, items_table};
|
|||
// ---------- Implementation ----------
|
||||
impl Displayable for Entity {
|
||||
// Detailed multi-line view (used e.g. in logs)
|
||||
fn print_full(&self) {
|
||||
println!("{}", self.to_pretty_tree(),);
|
||||
fn print_full(&self, epoch_info: &EpochInfo) {
|
||||
println!("{}", self.to_pretty_tree(epoch_info),);
|
||||
if let Ok(c) = self.stats_to_table() {
|
||||
println!("{c}");
|
||||
}
|
||||
}
|
||||
|
||||
/// One-liner used for auto-generation of `Vec<T>` displays
|
||||
// TODO: URGENT: Change this display
|
||||
fn oneline_display(&self) -> String {
|
||||
let capital = self
|
||||
.metadata
|
||||
|
|
@ -142,7 +144,8 @@ impl Entity {
|
|||
///
|
||||
/// # Returns
|
||||
/// A `String` containing the formatted tree.
|
||||
pub fn to_pretty_tree(&self) -> String {
|
||||
// TODO: Deref ids
|
||||
pub fn to_pretty_tree(&self, epoch_info: &EpochInfo) -> String {
|
||||
// Helper that prefixes a line with the correct amount of vertical bars.
|
||||
fn indent(level: usize, line: &str) -> String {
|
||||
let bars = "│ ".repeat(level);
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ use std::str::FromStr;
|
|||
|
||||
use crate::entity::{EffectApplication, EffectBounds, Entity, EntityDuration, EntityIdentifier};
|
||||
use crate::epoch::epoch_traits::Diffable;
|
||||
use crate::epoch::{EpochInfo, epoch_info};
|
||||
use crate::error::EpochError;
|
||||
use crate::stats::{self, EntityType, MetaStats, Position, StatPair, TownId};
|
||||
use std::io::Write;
|
||||
|
|
@ -264,8 +265,9 @@ fn effect_bound_testing() {
|
|||
fn snapshot_full_oneline_display() {
|
||||
init_snapshot_env();
|
||||
let e = full_entity();
|
||||
let epoch_info = EpochInfo::default();
|
||||
assert_snapshot!(e.oneline_display());
|
||||
assert_snapshot!(e.to_pretty_tree());
|
||||
assert_snapshot!(e.to_pretty_tree(&epoch_info));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -273,7 +275,7 @@ fn snapshot_minimal_oneline_display() {
|
|||
init_snapshot_env();
|
||||
let e = Entity::default();
|
||||
assert_snapshot!(e.oneline_display());
|
||||
assert_snapshot!(e.to_pretty_tree());
|
||||
assert_snapshot!(e.to_pretty_tree(&EpochInfo::default()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -291,7 +293,7 @@ fn snapshot_print_full_output() {
|
|||
// // Temporarily swap the real stdout for our buffer.
|
||||
// let original = std::mem::replace(&mut handle, &mut buffer);
|
||||
// Run the method we want to snapshot.
|
||||
e.print_full();
|
||||
e.print_full(&EpochInfo::default());
|
||||
// Restore the original stdout.
|
||||
// std::mem::replace(&mut handle, original);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@
|
|||
#![warn(missing_docs, clippy::missing_docs_in_private_items)]
|
||||
#![deny(clippy::unwrap_used)]
|
||||
|
||||
use crate::epoch::EpochInfo;
|
||||
|
||||
#[macro_use]
|
||||
#[allow(unused_assignments)]
|
||||
pub mod error;
|
||||
|
|
@ -60,24 +62,19 @@ pub mod utils;
|
|||
/// directly on a vector of displayable items.
|
||||
pub trait Displayable {
|
||||
/// Return a fully-described string representation.
|
||||
fn print_full(&self) {
|
||||
self.print_short()
|
||||
fn print_full(&self, epoch_info: &EpochInfo) {
|
||||
println!("{}", self.short_display(epoch_info));
|
||||
}
|
||||
|
||||
/// Return a short, user-friendly string.
|
||||
fn print_short(&self) {
|
||||
println!("{}", self.oneline_display())
|
||||
/// Return a short, user-friendly string. Resolves Ids
|
||||
fn short_display(&self, _epoch_info: &EpochInfo) -> String {
|
||||
self.oneline_display()
|
||||
}
|
||||
|
||||
/// Return a one-liner string, suitable for embedding in a collection
|
||||
/// representation (e.g. a `Vec<T>`). Implementations should avoid
|
||||
/// new-lines to keep the output usable in a single line context.
|
||||
fn oneline_display(&self) -> String;
|
||||
|
||||
/// Print oneline
|
||||
fn print_oneline(&self) {
|
||||
println!("{}", self.oneline_display())
|
||||
}
|
||||
}
|
||||
|
||||
/// Implement `Displayable` for vectors of displayable items.
|
||||
|
|
@ -87,12 +84,15 @@ 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) {
|
||||
self.iter().for_each(|e| e.print_full());
|
||||
fn print_full(&self, epoch_info: &EpochInfo) {
|
||||
self.iter().for_each(|e| e.print_full(epoch_info));
|
||||
}
|
||||
|
||||
fn print_short(&self) {
|
||||
self.iter().for_each(|e| e.print_short());
|
||||
fn short_display(&self, epoch_info: &EpochInfo) -> String {
|
||||
self.iter()
|
||||
.map(|e| e.short_display(epoch_info))
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
fn oneline_display(&self) -> String {
|
||||
|
|
@ -100,7 +100,7 @@ impl<T: Displayable> Displayable for Vec<T> {
|
|||
self.iter()
|
||||
.map(|x| x.oneline_display())
|
||||
.collect::<Vec<_>>()
|
||||
.join(",")
|
||||
.join("\n")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -133,10 +133,10 @@ mod tests {
|
|||
},
|
||||
];
|
||||
|
||||
assert_eq!(vec.oneline_display(), "Alice,Bob,Eve".to_string());
|
||||
vec.print_full();
|
||||
vec.print_short();
|
||||
vec.print_oneline();
|
||||
let efo = EpochInfo::default();
|
||||
vec.print_full(&efo);
|
||||
vec.short_display(&efo);
|
||||
assert_snapshot!("dummy_vec_oneline", vec.oneline_display());
|
||||
assert_snapshot!("dummy_vec_oneline", vec.short_display(&efo));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,17 @@
|
|||
//! Autonaming generator
|
||||
//! Supposed to be loaded with the world if needed
|
||||
use crate::stats::Symbols;
|
||||
use once_cell::sync::Lazy;
|
||||
use rand::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::{
|
||||
fs, io,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use crate::Displayable;
|
||||
use crate::entity::EffectApplication;
|
||||
use crate::entity::EffectBounds;
|
||||
use crate::entity::EntityDuration;
|
||||
use crate::entity::{EffectApplication, EffectBounds, EntityDuration};
|
||||
use crate::error::EpochError;
|
||||
use crate::stats::EntityType;
|
||||
use crate::stats::StatPair;
|
||||
use crate::stats::{EntityType, StatPair, Symbols};
|
||||
|
||||
/// The structure that holds everything we need for name generation.
|
||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
---
|
||||
source: lib_efo/src/lib.rs
|
||||
expression: vec.oneline_display()
|
||||
expression: vec.short_display(&efo)
|
||||
---
|
||||
Alice,Bob,Eve
|
||||
Alice
|
||||
Bob
|
||||
Eve
|
||||
|
|
|
|||
Loading…
Reference in New Issue