Use formatting commands for padding.

This commit is contained in:
Stephen Chung 2020-04-02 22:37:35 +08:00
parent 0873bdc152
commit 9d7091ad9d
2 changed files with 8 additions and 14 deletions

View File

@ -9,10 +9,6 @@ use std::{
}; };
fn print_error(input: &str, err: EvalAltResult) { fn print_error(input: &str, err: EvalAltResult) {
fn padding(pad: &str, len: usize) -> String {
iter::repeat(pad).take(len).collect::<String>()
}
let lines: Vec<_> = input.trim().split('\n').collect(); let lines: Vec<_> = input.trim().split('\n').collect();
let line_no = if lines.len() > 1 { let line_no = if lines.len() > 1 {
@ -54,8 +50,9 @@ fn print_error(input: &str, err: EvalAltResult) {
}; };
println!( println!(
"{}^ {}", "{0:>1$} {2}",
padding(" ", line_no.len() + p.position().unwrap() - 1), "^",
line_no.len() + p.position().unwrap(),
err_text.replace(&pos_text, "") err_text.replace(&pos_text, "")
); );
} }

View File

@ -5,10 +5,6 @@ use rhai::OptimizationLevel;
use std::{env, fs::File, io::Read, iter, process::exit}; use std::{env, fs::File, io::Read, iter, process::exit};
fn padding(pad: &str, len: usize) -> String {
iter::repeat(pad).take(len).collect::<String>()
}
fn eprint_error(input: &str, err: EvalAltResult) { fn eprint_error(input: &str, err: EvalAltResult) {
fn eprint_line(lines: &[&str], line: usize, pos: usize, err: &str) { fn eprint_line(lines: &[&str], line: usize, pos: usize, err: &str) {
let line_no = format!("{}: ", line); let line_no = format!("{}: ", line);
@ -16,8 +12,9 @@ fn eprint_error(input: &str, err: EvalAltResult) {
eprintln!("{}{}", line_no, lines[line - 1]); eprintln!("{}{}", line_no, lines[line - 1]);
eprintln!( eprintln!(
"{}^ {}", "{:>1$} {2}",
padding(" ", line_no.len() + pos - 1), "^",
line_no.len() + pos,
err.replace(&pos_text, "") err.replace(&pos_text, "")
); );
eprintln!(""); eprintln!("");
@ -76,9 +73,9 @@ fn main() {
} }
if let Err(err) = engine.consume(false, &contents) { if let Err(err) = engine.consume(false, &contents) {
eprintln!("{}", padding("=", filename.len())); eprintln!("{:=<1$}", "", filename.len());
eprintln!("{}", filename); eprintln!("{}", filename);
eprintln!("{}", padding("=", filename.len())); eprintln!("{:=<1$}", "", filename.len());
eprintln!(""); eprintln!("");
eprint_error(&contents, err); eprint_error(&contents, err);