added format command

This commit is contained in:
JOLIMAITRE Matthieu 2022-09-25 23:15:35 +02:00
parent e7433e42db
commit b6962301ea
2 changed files with 23 additions and 2 deletions

View file

@ -4,7 +4,7 @@ use termion::color;
use crate::{ use crate::{
tasks::FormatTask, tasks::FormatTask,
utils::{log_failure, log_process}, utils::{log_failure, log_process, log_success},
}; };
/// TODO: fill with appropriate rules /// TODO: fill with appropriate rules
@ -91,3 +91,14 @@ fn check_formatting(file: String) {
} }
} }
} }
pub fn format(files: Vec<String>) {
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}'"));
}
}

View file

@ -18,12 +18,18 @@ pub struct Arguments {
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
#[derive(Subcommand)] #[derive(Subcommand)]
pub enum Commands { pub enum Commands {
/// Checks a source file for conformance with piscine limitations. /// Checks source files for conformance with piscine limitations.
check { check {
/// File to check. /// File to check.
files: Vec<String>, files: Vec<String>,
}, },
/// Formats source files.
format {
/// Files to format.
files: Vec<String>,
},
/// Runs a set of files or the default target. /// Runs a set of files or the default target.
run { run {
/// Files to run. /// Files to run.
@ -82,6 +88,9 @@ fn main() {
match args.command { match args.command {
Commands::check { files } => check::main(files), Commands::check { files } => check::main(files),
Commands::format { files } => check::format(files),
Commands::run { mut files } => { Commands::run { mut files } => {
if files.is_empty() { if files.is_empty() {
files.push(Config::get_current().main_file().to_string()); files.push(Config::get_current().main_file().to_string());
@ -90,6 +99,7 @@ fn main() {
let args = compilation_args(); let args = compilation_args();
run::main(files, args); run::main(files, args);
} }
Commands::test { Commands::test {
capture, capture,
mut files, mut files,