From b6962301ea2ad266cd7996af2fa78966ba9e250f Mon Sep 17 00:00:00 2001 From: JOLIMAITRE Matthieu Date: Sun, 25 Sep 2022 23:15:35 +0200 Subject: [PATCH] added format command --- src/check.rs | 13 ++++++++++++- src/main.rs | 12 +++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/check.rs b/src/check.rs index 00cd8c4..32990c2 100644 --- a/src/check.rs +++ b/src/check.rs @@ -4,7 +4,7 @@ use termion::color; use crate::{ tasks::FormatTask, - utils::{log_failure, log_process}, + utils::{log_failure, log_process, log_success}, }; /// TODO: fill with appropriate rules @@ -91,3 +91,14 @@ fn check_formatting(file: String) { } } } + +pub fn format(files: Vec) { + for file in files { + let mut formatted = FormatTask::new(file.clone(), FORMAT_CONFIG.into()).run(); + if !formatted.ends_with('\n') { + formatted += "\n"; + } + fs::write(&file, formatted).unwrap(); + log_success(&format!("formatted '{file}'")); + } +} diff --git a/src/main.rs b/src/main.rs index ce79166..5822bea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,12 +18,18 @@ pub struct Arguments { #[allow(non_camel_case_types)] #[derive(Subcommand)] pub enum Commands { - /// Checks a source file for conformance with piscine limitations. + /// Checks source files for conformance with piscine limitations. check { /// File to check. files: Vec, }, + /// Formats source files. + format { + /// Files to format. + files: Vec, + }, + /// Runs a set of files or the default target. run { /// Files to run. @@ -82,6 +88,9 @@ fn main() { match args.command { Commands::check { files } => check::main(files), + + Commands::format { files } => check::format(files), + Commands::run { mut files } => { if files.is_empty() { files.push(Config::get_current().main_file().to_string()); @@ -90,6 +99,7 @@ fn main() { let args = compilation_args(); run::main(files, args); } + Commands::test { capture, mut files,