From 9d7091ad9db489b4dd646ae7b3b753222593154e Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 2 Apr 2020 22:37:35 +0800 Subject: [PATCH] Use formatting commands for padding. --- examples/repl.rs | 9 +++------ examples/rhai_runner.rs | 13 +++++-------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/examples/repl.rs b/examples/repl.rs index 4ba75597..def1b310 100644 --- a/examples/repl.rs +++ b/examples/repl.rs @@ -9,10 +9,6 @@ use std::{ }; fn print_error(input: &str, err: EvalAltResult) { - fn padding(pad: &str, len: usize) -> String { - iter::repeat(pad).take(len).collect::() - } - let lines: Vec<_> = input.trim().split('\n').collect(); let line_no = if lines.len() > 1 { @@ -54,8 +50,9 @@ fn print_error(input: &str, err: EvalAltResult) { }; println!( - "{}^ {}", - padding(" ", line_no.len() + p.position().unwrap() - 1), + "{0:>1$} {2}", + "^", + line_no.len() + p.position().unwrap(), err_text.replace(&pos_text, "") ); } diff --git a/examples/rhai_runner.rs b/examples/rhai_runner.rs index 20f6226e..180f51d6 100644 --- a/examples/rhai_runner.rs +++ b/examples/rhai_runner.rs @@ -5,10 +5,6 @@ use rhai::OptimizationLevel; use std::{env, fs::File, io::Read, iter, process::exit}; -fn padding(pad: &str, len: usize) -> String { - iter::repeat(pad).take(len).collect::() -} - fn eprint_error(input: &str, err: EvalAltResult) { fn eprint_line(lines: &[&str], line: usize, pos: usize, err: &str) { let line_no = format!("{}: ", line); @@ -16,8 +12,9 @@ fn eprint_error(input: &str, err: EvalAltResult) { eprintln!("{}{}", line_no, lines[line - 1]); eprintln!( - "{}^ {}", - padding(" ", line_no.len() + pos - 1), + "{:>1$} {2}", + "^", + line_no.len() + pos, err.replace(&pos_text, "") ); eprintln!(""); @@ -76,9 +73,9 @@ fn main() { } if let Err(err) = engine.consume(false, &contents) { - eprintln!("{}", padding("=", filename.len())); + eprintln!("{:=<1$}", "", filename.len()); eprintln!("{}", filename); - eprintln!("{}", padding("=", filename.len())); + eprintln!("{:=<1$}", "", filename.len()); eprintln!(""); eprint_error(&contents, err);