1
0

Add format option to the CLI

This commit is contained in:
Jeff 2024-10-13 07:56:11 -04:00
parent 02ee7d126c
commit 565d3c54f1
2 changed files with 28 additions and 23 deletions

View File

@ -460,7 +460,6 @@ impl<'a> ChunkDisassembler<'a> {
disassembly.push('│'); disassembly.push('│');
} }
disassembly.push_str(&line_length.to_string());
disassembly.push('\n'); disassembly.push('\n');
if !remainder.is_empty() { if !remainder.is_empty() {
@ -642,12 +641,6 @@ impl<'a> ChunkDisassembler<'a> {
push_border(&bottom_border, &mut disassembly); push_border(&bottom_border, &mut disassembly);
if let Some(source) = self.source {
let formatted = Formatter::new(source).origin(self.name).format();
disassembly.push_str(&formatted);
}
let expected_length = self.predict_length(); let expected_length = self.predict_length();
let actual_length = disassembly.len(); let actual_length = disassembly.len();

View File

@ -2,7 +2,7 @@ use std::{fs::read_to_string, io::Write};
use clap::Parser; use clap::Parser;
use colored::Colorize; use colored::Colorize;
use dust_lang::{parse, run}; use dust_lang::{parse, run, Formatter};
use log::Level; use log::Level;
#[derive(Parser)] #[derive(Parser)]
@ -10,6 +10,9 @@ struct Cli {
#[arg(short, long)] #[arg(short, long)]
command: Option<String>, command: Option<String>,
#[arg(short, long)]
format: bool,
#[arg(short, long)] #[arg(short, long)]
parse: bool, parse: bool,
@ -43,24 +46,33 @@ fn main() {
let args = Cli::parse(); let args = Cli::parse();
if let Some(command) = &args.command { let source = if let Some(path) = &args.path {
if args.parse { &read_to_string(path).expect("Failed to read file")
parse_and_display(command, args.styled); } else if let Some(command) = &args.command {
command
} else { } else {
run_and_display(command); eprintln!("No input provided");
} return;
} else if let Some(path) = &args.path { };
let source = read_to_string(path).expect("Failed to read file");
if args.parse { if args.parse {
parse_and_display(&source, args.styled); parse_source(source, args.styled);
} else {
run_and_display(&source);
} }
if args.format {
format_source(source);
}
if !args.format && !args.parse {
run_source(source);
} }
} }
fn parse_and_display(source: &str, styled: bool) { fn format_source(source: &str) {
println!("{}", Formatter::new(source).format())
}
fn parse_source(source: &str, styled: bool) {
match parse(source) { match parse(source) {
Ok(chunk) => println!( Ok(chunk) => println!(
"{}", "{}",
@ -76,7 +88,7 @@ fn parse_and_display(source: &str, styled: bool) {
} }
} }
fn run_and_display(source: &str) { fn run_source(source: &str) {
match run(source) { match run(source) {
Ok(Some(value)) => println!("{}", value), Ok(Some(value)) => println!("{}", value),
Ok(_) => {} Ok(_) => {}