Use variable interpolation for println!.

This commit is contained in:
Stephen Chung
2022-10-27 13:38:21 +08:00
parent 6b24cc151e
commit 3c2e031883
24 changed files with 132 additions and 150 deletions

View File

@@ -75,7 +75,7 @@ fn print_current_source(
}
if !src.is_empty() {
// Print just a line number for imported modules
println!("{} @ {:?}", src, pos);
println!("{src} @ {pos:?}");
} else {
// Print the current source line
print_source(lines, pos, 0, window);
@@ -100,17 +100,16 @@ fn print_error(input: &str, mut err: EvalAltResult) {
// Print error position
if pos.is_none() {
// No position
println!("{}", err);
println!("{err}");
} else {
// Specific position - print line text
println!("{}{}", line_no, lines[pos.line().unwrap() - 1]);
println!("{line_no}{}", lines[pos.line().unwrap() - 1]);
// Display position marker
println!(
"{0:>1$} {2}",
"{0:>1$} {err}",
"^",
line_no.len() + pos.position().unwrap(),
err
);
}
}
@@ -247,11 +246,11 @@ fn debug_callback(
BreakPoint::AtPosition { .. } => (),
BreakPoint::AtFunctionName { ref name, .. }
| BreakPoint::AtFunctionCall { ref name, .. } => {
println!("! Call to function {}.", name)
println!("! Call to function {name}.")
}
#[cfg(not(feature = "no_object"))]
BreakPoint::AtProperty { ref name, .. } => {
println!("! Property {} accessed.", name)
println!("! Property {name} accessed.")
}
_ => unreachable!(),
}
@@ -311,8 +310,8 @@ fn debug_callback(
println!("{:?}", node);
} else {
match source {
Some(source) => println!("{:?} {} @ {:?}", node, source, pos),
None => println!("{:?} @ {:?}", node, pos),
Some(source) => println!("{node:?} {source} @ {pos:?}"),
None => println!("{node:?} @ {pos:?}"),
}
}
println!();
@@ -327,7 +326,7 @@ fn debug_callback(
["list" | "l", n] if n.parse::<usize>().is_ok() => {
let num = n.parse::<usize>().unwrap();
if num == 0 || num > lines.len() {
eprintln!("\x1b[31mInvalid line: {}\x1b[39m", num);
eprintln!("\x1b[31mInvalid line: {num}\x1b[39m");
} else {
let pos = Position::new(num as u16, 0);
print_current_source(&mut context, source, pos, lines, (3, 6));
@@ -340,17 +339,17 @@ fn debug_callback(
["next" | "n"] => break Ok(DebuggerCommand::Next),
["scope"] => println!("{}", context.scope()),
["print" | "p", "this"] => match context.this_ptr() {
Some(value) => println!("=> {:?}", value),
Some(value) => println!("=> {value:?}"),
None => println!("`this` pointer is unbound."),
},
["print" | "p", var_name] => match context.scope().get_value::<Dynamic>(var_name) {
Some(value) => println!("=> {:?}", value),
None => eprintln!("Variable not found: {}", var_name),
Some(value) => println!("=> {value:?}"),
None => eprintln!("Variable not found: {var_name}"),
},
["print" | "p"] => {
println!("{}", context.scope().clone_visible());
if let Some(value) = context.this_ptr() {
println!("this = {:?}", value);
println!("this = {value:?}");
}
}
#[cfg(not(feature = "no_module"))]
@@ -379,7 +378,7 @@ fn debug_callback(
.iter()
.rev()
{
println!("{}", frame)
println!("{frame}")
}
}
["info" | "i", "break" | "b"] => Iterator::for_each(
@@ -396,7 +395,7 @@ fn debug_callback(
print!("{}", line_num);
print_source(lines, *pos, line_num.len(), (0, 0));
}
_ => println!("[{}] {}", i + 1, bp),
_ => println!("[{}] {bp}", i + 1),
},
),
["enable" | "en", n] => {
@@ -414,12 +413,12 @@ fn debug_callback(
.get_mut(n - 1)
.unwrap()
.enable(true);
println!("Break-point #{} enabled.", n)
println!("Break-point #{n} enabled.")
} else {
eprintln!("\x1b[31mInvalid break-point: {}\x1b[39m", n);
eprintln!("\x1b[31mInvalid break-point: {n}\x1b[39m");
}
} else {
eprintln!("\x1b[31mInvalid break-point: '{}'\x1b[39m", n);
eprintln!("\x1b[31mInvalid break-point: '{n}'\x1b[39m");
}
}
["disable" | "dis", n] => {
@@ -437,12 +436,12 @@ fn debug_callback(
.get_mut(n - 1)
.unwrap()
.enable(false);
println!("Break-point #{} disabled.", n)
println!("Break-point #{n} disabled.")
} else {
eprintln!("\x1b[31mInvalid break-point: {}\x1b[39m", n);
eprintln!("\x1b[31mInvalid break-point: {n}\x1b[39m");
}
} else {
eprintln!("\x1b[31mInvalid break-point: '{}'\x1b[39m", n);
eprintln!("\x1b[31mInvalid break-point: '{n}'\x1b[39m");
}
}
["delete" | "d", n] => {
@@ -458,12 +457,12 @@ fn debug_callback(
.debugger
.break_points_mut()
.remove(n - 1);
println!("Break-point #{} deleted.", n)
println!("Break-point #{n} deleted.")
} else {
eprintln!("\x1b[31mInvalid break-point: {}\x1b[39m", n);
eprintln!("\x1b[31mInvalid break-point: {n}\x1b[39m");
}
} else {
eprintln!("\x1b[31mInvalid break-point: '{}'\x1b[39m", n);
eprintln!("\x1b[31mInvalid break-point: '{n}'\x1b[39m");
}
}
["delete" | "d"] => {
@@ -481,14 +480,14 @@ fn debug_callback(
args,
enabled: true,
};
println!("Break-point added for {}", bp);
println!("Break-point added for {bp}");
context
.global_runtime_state_mut()
.debugger
.break_points_mut()
.push(bp);
} else {
eprintln!("\x1b[31mInvalid number of arguments: '{}'\x1b[39m", args);
eprintln!("\x1b[31mInvalid number of arguments: '{args}'\x1b[39m");
}
}
// Property name
@@ -498,7 +497,7 @@ fn debug_callback(
name: param[1..].into(),
enabled: true,
};
println!("Break-point added for {}", bp);
println!("Break-point added for {bp}");
context
.global_runtime_state_mut()
.debugger
@@ -521,14 +520,14 @@ fn debug_callback(
pos: Position::new(n as u16, 0),
enabled: true,
};
println!("Break-point added {}", bp);
println!("Break-point added {bp}");
context
.global_runtime_state_mut()
.debugger
.break_points_mut()
.push(bp);
} else {
eprintln!("\x1b[31mInvalid line number: '{}'\x1b[39m", n);
eprintln!("\x1b[31mInvalid line number: '{n}'\x1b[39m");
}
}
// Function name parameter
@@ -537,7 +536,7 @@ fn debug_callback(
name: param.trim().into(),
enabled: true,
};
println!("Break-point added for {}", bp);
println!("Break-point added for {bp}");
context
.global_runtime_state_mut()
.debugger
@@ -551,7 +550,7 @@ fn debug_callback(
pos,
enabled: true,
};
println!("Break-point added {}", bp);
println!("Break-point added {bp}");
context
.global_runtime_state_mut()
.debugger
@@ -588,7 +587,7 @@ fn debug_callback(
fn main() {
let title = format!("Rhai Debugger (version {})", env!("CARGO_PKG_VERSION"));
println!("{}", title);
println!("{title}");
println!("{0:=<1$}", "", title.len());
// Initialize scripting engine

View File

@@ -26,17 +26,16 @@ fn print_error(input: &str, mut err: EvalAltResult) {
// Print error position
if pos.is_none() {
// No position
println!("{}", err);
println!("{err}");
} else {
// Specific position - print line text
println!("{}{}", line_no, lines[pos.line().unwrap() - 1]);
println!("{line_no}{}", lines[pos.line().unwrap() - 1]);
// Display position marker
println!(
"{0:>1$} {2}",
"{0:>1$} {err}",
"^",
line_no.len() + pos.position().unwrap(),
err
);
}
}
@@ -119,7 +118,7 @@ fn load_script_files(engine: &mut Engine) {
for filename in env::args().skip(1) {
let filename = match Path::new(&filename).canonicalize() {
Err(err) => {
eprintln!("Error script file path: {}\n{}", filename, err);
eprintln!("Error script file path: {filename}\n{err}");
exit(1);
}
Ok(f) => {
@@ -164,7 +163,7 @@ fn load_script_files(engine: &mut Engine) {
let filename = filename.to_string_lossy();
eprintln!("{:=<1$}", "", filename.len());
eprintln!("{}", filename);
eprintln!("{filename}");
eprintln!("{:=<1$}", "", filename.len());
eprintln!();
@@ -277,13 +276,13 @@ mod sample_functions {
#[rhai_fn(name = "test")]
pub fn test2(x: &mut INT, y: INT, z: &str) {
*x += y + (z.len() as INT);
println!("{} {} {}", x, y, z);
println!("{x} {y} {z}");
}
}
fn main() {
let title = format!("Rhai REPL tool (version {})", env!("CARGO_PKG_VERSION"));
println!("{}", title);
println!("{title}");
println!("{0:=<1$}", "", title.len());
#[cfg(not(feature = "no_optimize"))]
@@ -338,11 +337,11 @@ fn main() {
history_offset += 1;
}
if input.contains('\n') {
println!("[{}] ~~~~", replacement_index);
println!("{}", input);
println!("[{replacement_index}] ~~~~");
println!("{input}");
println!("~~~~");
} else {
println!("[{}] {}", replacement_index, input);
println!("[{replacement_index}] {input}");
}
replacement_index = 0;
} else {
@@ -374,7 +373,7 @@ fn main() {
Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => break 'main_loop,
Err(err) => {
eprintln!("Error: {:?}", err);
eprintln!("Error: {err:?}");
break 'main_loop;
}
}
@@ -401,12 +400,12 @@ fn main() {
"history" => {
for (i, h) in rl.history().iter().enumerate() {
match &h.split('\n').collect::<Vec<_>>()[..] {
[line] => println!("[{}] {}", history_offset + i, line),
[line] => println!("[{}] {line}", history_offset + i),
lines => {
for (x, line) in lines.iter().enumerate() {
let number = format!("[{}]", history_offset + i);
if x == 0 {
println!("{} {}", number, line.trim_end());
println!("{number} {}", line.trim_end());
} else {
println!("{0:>1$} {2}", "", number.len(), line.trim_end());
}
@@ -439,30 +438,30 @@ fn main() {
continue;
}
"scope" => {
println!("{}", scope);
println!("{scope}");
continue;
}
#[cfg(not(feature = "no_optimize"))]
"astu" => {
// print the last un-optimized AST
println!("{:#?}\n", ast_u);
println!("{ast_u:#?}\n");
continue;
}
"ast" => {
// print the last AST
println!("{:#?}\n", ast);
println!("{ast:#?}\n");
continue;
}
#[cfg(feature = "metadata")]
"functions" => {
// print a list of all registered functions
for f in engine.gen_fn_signatures(false) {
println!("{}", f)
println!("{f}")
}
#[cfg(not(feature = "no_function"))]
for f in main_ast.iter_functions() {
println!("{}", f)
println!("{f}")
}
println!();
@@ -505,7 +504,7 @@ fn main() {
replacement = Some(line.clone());
replacement_index = history_offset + (rl.history().len() - 1 - n);
}
None => eprintln!("History line not found: {}", text),
None => eprintln!("History line not found: {text}"),
}
continue;
}
@@ -561,7 +560,7 @@ fn main() {
engine.eval_ast_with_scope::<Dynamic>(&mut scope, &main_ast)
}) {
Ok(result) if !result.is::<()>() => {
println!("=> {:?}", result);
println!("=> {result:?}");
println!();
}
Ok(_) => (),

View File

@@ -7,12 +7,11 @@ fn eprint_error(input: &str, mut err: EvalAltResult) {
let line = pos.line().unwrap();
let line_no = format!("{line}: ");
eprintln!("{}{}", line_no, lines[line - 1]);
eprintln!("{line_no}{}", lines[line - 1]);
eprintln!(
"{:>1$} {2}",
"{:>1$} {err_msg}",
"^",
line_no.len() + pos.position().unwrap(),
err_msg
);
eprintln!();
}
@@ -24,7 +23,7 @@ fn eprint_error(input: &str, mut err: EvalAltResult) {
if pos.is_none() {
// No position
eprintln!("{}", err);
eprintln!("{err}");
} else {
// Specific position
eprint_line(&lines, pos, &err.to_string())
@@ -37,7 +36,7 @@ fn main() {
for filename in env::args().skip(1) {
let filename = match Path::new(&filename).canonicalize() {
Err(err) => {
eprintln!("Error script file path: {}\n{}", filename, err);
eprintln!("Error script file path: {filename}\n{err}");
exit(1);
}
Ok(f) => match f.strip_prefix(std::env::current_dir().unwrap().canonicalize().unwrap())
@@ -94,7 +93,7 @@ fn main() {
let filename = filename.to_string_lossy();
eprintln!("{:=<1$}", "", filename.len());
eprintln!("{}", filename);
eprintln!("{filename}");
eprintln!("{:=<1$}", "", filename.len());
eprintln!();