style: simplify string formatting for readability (#15033)

# Objective

The goal of this change is to improve code readability and
maintainability.
This commit is contained in:
Hamir Mahal 2024-09-03 16:35:49 -07:00 committed by GitHub
parent 4ac2a63556
commit ec728c31c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 27 additions and 37 deletions

View file

@ -351,7 +351,7 @@ fn setup(
/// Writes a simple menu item that can be on or off. /// Writes a simple menu item that can be on or off.
fn draw_selectable_menu_item(ui: &mut String, label: &str, shortcut: char, enabled: bool) { fn draw_selectable_menu_item(ui: &mut String, label: &str, shortcut: char, enabled: bool) {
let star = if enabled { "*" } else { "" }; let star = if enabled { "*" } else { "" };
let _ = writeln!(*ui, "({}) {}{}{}", shortcut, star, label, star); let _ = writeln!(*ui, "({shortcut}) {star}{label}{star}");
} }
/// Creates a colorful test pattern /// Creates a colorful test pattern

View file

@ -459,9 +459,9 @@ impl Display for SelectedSectionColorGradingOption {
impl Display for SelectedColorGradingOption { impl Display for SelectedColorGradingOption {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self { match self {
SelectedColorGradingOption::Global(option) => write!(f, "\"{}\"", option), SelectedColorGradingOption::Global(option) => write!(f, "\"{option}\""),
SelectedColorGradingOption::Section(section, option) => { SelectedColorGradingOption::Section(section, option) => {
write!(f, "\"{}\" for \"{}\"", option, section) write!(f, "\"{option}\" for \"{section}\"")
} }
} }
} }
@ -633,7 +633,7 @@ fn update_ui_state(
/// Creates the help text at the top left of the window. /// Creates the help text at the top left of the window.
fn create_help_text(currently_selected_option: &SelectedColorGradingOption) -> String { fn create_help_text(currently_selected_option: &SelectedColorGradingOption) -> String {
format!("Press Left/Right to adjust {}", currently_selected_option) format!("Press Left/Right to adjust {currently_selected_option}")
} }
/// Processes keyboard input to change the value of the currently-selected color /// Processes keyboard input to change the value of the currently-selected color

View file

@ -351,12 +351,11 @@ impl AppStatus {
Text::from_section( Text::from_section(
format!( format!(
"{}\n{}\n{}\n{}\n{}", "{CLICK_TO_MOVE_HELP_TEXT}
CLICK_TO_MOVE_HELP_TEXT, {voxels_help_text}
voxels_help_text, {irradiance_volume_help_text}
irradiance_volume_help_text, {rotation_help_text}
rotation_help_text, {switch_mesh_help_text}"
switch_mesh_help_text
), ),
TextStyle::default(), TextStyle::default(),
) )

View file

@ -533,7 +533,7 @@ fn update(
// Finally saving image to file, this heavy blocking operation is kept here // Finally saving image to file, this heavy blocking operation is kept here
// for example simplicity, but in real app you should move it to a separate task // for example simplicity, but in real app you should move it to a separate task
if let Err(e) = img.save(image_path) { if let Err(e) = img.save(image_path) {
panic!("Failed to save image: {}", e); panic!("Failed to save image: {e}");
}; };
} }
if scene_controller.single_image { if scene_controller.single_image {

View file

@ -69,10 +69,7 @@ fn setup(world: &mut World) {
.on_add(|mut world, entity, component_id| { .on_add(|mut world, entity, component_id| {
// You can access component data from within the hook // You can access component data from within the hook
let value = world.get::<MyComponent>(entity).unwrap().0; let value = world.get::<MyComponent>(entity).unwrap().0;
println!( println!("Component: {component_id:?} added to: {entity:?} with value {value:?}");
"Component: {:?} added to: {:?} with value {:?}",
component_id, entity, value
);
// Or access resources // Or access resources
world world
.resource_mut::<MyComponentIndex>() .resource_mut::<MyComponentIndex>()
@ -96,10 +93,7 @@ fn setup(world: &mut World) {
// since it runs before the component is removed you can still access the component data // since it runs before the component is removed you can still access the component data
.on_remove(|mut world, entity, component_id| { .on_remove(|mut world, entity, component_id| {
let value = world.get::<MyComponent>(entity).unwrap().0; let value = world.get::<MyComponent>(entity).unwrap().0;
println!( println!("Component: {component_id:?} removed from: {entity:?} with value {value:?}");
"Component: {:?} removed from: {:?} with value {:?}",
component_id, entity, value
);
// You can also issue commands through `.commands()` // You can also issue commands through `.commands()`
world.commands().entity(entity).despawn(); world.commands().entity(entity).despawn();
}); });

View file

@ -50,7 +50,7 @@ fn main() {
let mut component_names = HashMap::<String, ComponentId>::new(); let mut component_names = HashMap::<String, ComponentId>::new();
let mut component_info = HashMap::<ComponentId, ComponentInfo>::new(); let mut component_info = HashMap::<ComponentId, ComponentInfo>::new();
println!("{}", PROMPT); println!("{PROMPT}");
loop { loop {
print!("\n> "); print!("\n> ");
let _ = std::io::stdout().flush(); let _ = std::io::stdout().flush();
@ -64,10 +64,10 @@ fn main() {
let Some((first, rest)) = line.trim().split_once(|c: char| c.is_whitespace()) else { let Some((first, rest)) = line.trim().split_once(|c: char| c.is_whitespace()) else {
match &line.chars().next() { match &line.chars().next() {
Some('c') => println!("{}", COMPONENT_PROMPT), Some('c') => println!("{COMPONENT_PROMPT}"),
Some('s') => println!("{}", ENTITY_PROMPT), Some('s') => println!("{ENTITY_PROMPT}"),
Some('q') => println!("{}", QUERY_PROMPT), Some('q') => println!("{QUERY_PROMPT}"),
_ => println!("{}", PROMPT), _ => println!("{PROMPT}"),
} }
continue; continue;
}; };
@ -112,7 +112,7 @@ fn main() {
// Get the id for the component with the given name // Get the id for the component with the given name
let Some(&id) = component_names.get(name) else { let Some(&id) = component_names.get(name) else {
println!("Component {} does not exist", name); println!("Component {name} does not exist");
return; return;
}; };
@ -245,7 +245,7 @@ fn parse_term<Q: QueryData>(
}; };
if !matched { if !matched {
println!("Unable to find component: {}", str); println!("Unable to find component: {str}");
} }
} }

View file

@ -102,7 +102,7 @@ fn send_events(mut events: EventWriter<DebugEvent>, frame_count: Res<FrameCount>
/// Note that some events will be printed twice, because they were sent twice. /// Note that some events will be printed twice, because they were sent twice.
fn debug_events(mut events: EventReader<DebugEvent>) { fn debug_events(mut events: EventReader<DebugEvent>) {
for event in events.read() { for event in events.read() {
println!("{:?}", event); println!("{event:?}");
} }
} }

View file

@ -115,7 +115,7 @@ fn build_ui(
for label in schedule_order { for label in schedule_order {
let schedule = schedules.get(*label).unwrap(); let schedule = schedules.get(*label).unwrap();
text_sections.push(TextSection::new( text_sections.push(TextSection::new(
format!("{:?}\n", label), format!("{label:?}\n"),
TextStyle { TextStyle {
font: asset_server.load(FONT_BOLD), font: asset_server.load(FONT_BOLD),
color: FONT_COLOR, color: FONT_COLOR,

View file

@ -73,8 +73,8 @@ fn setup(mut commands: Commands) {
R: Remove the last control point\n\ R: Remove the last control point\n\
S: Cycle the spline construction being used\n\ S: Cycle the spline construction being used\n\
C: Toggle cyclic curve construction"; C: Toggle cyclic curve construction";
let spline_mode_text = format!("Spline: {}", spline_mode); let spline_mode_text = format!("Spline: {spline_mode}");
let cycling_mode_text = format!("{}", cycling_mode); let cycling_mode_text = format!("{cycling_mode}");
let style = TextStyle::default(); let style = TextStyle::default();
commands commands

View file

@ -87,8 +87,7 @@ impl FromStr for Layout {
"cube" => Ok(Self::Cube), "cube" => Ok(Self::Cube),
"sphere" => Ok(Self::Sphere), "sphere" => Ok(Self::Sphere),
_ => Err(format!( _ => Err(format!(
"Unknown layout value: '{}', valid options: 'cube', 'sphere'", "Unknown layout value: '{s}', valid options: 'cube', 'sphere'"
s
)), )),
} }
} }

View file

@ -37,7 +37,7 @@ fn runner(mut app: App) -> AppExit {
let stdin = io::stdin(); let stdin = io::stdin();
for line in stdin.lock().lines() { for line in stdin.lock().lines() {
if let Err(err) = line { if let Err(err) = line {
println!("read err: {:#}", err); println!("read err: {err:#}");
break; break;
} }
match line.unwrap().as_str() { match line.unwrap().as_str() {

View file

@ -34,8 +34,7 @@ fn main() {
assert_eq!( assert_eq!(
main_app_ambiguities.total(), main_app_ambiguities.total(),
0, 0,
"Main app has unexpected ambiguities among the following schedules: \n{:#?}.", "Main app has unexpected ambiguities among the following schedules: \n{main_app_ambiguities:#?}.",
main_app_ambiguities,
); );
// RenderApp is not checked here, because it is not within the App at this point. // RenderApp is not checked here, because it is not within the App at this point.
@ -43,8 +42,7 @@ fn main() {
assert_eq!( assert_eq!(
render_extract_ambiguities.total(), render_extract_ambiguities.total(),
0, 0,
"RenderExtract app has unexpected ambiguities among the following schedules: \n{:#?}", "RenderExtract app has unexpected ambiguities among the following schedules: \n{render_extract_ambiguities:#?}",
render_extract_ambiguities,
); );
} }