From 06a8458ca7bb884840ba89dfc511958700475c07 Mon Sep 17 00:00:00 2001 From: peoplemiau1 Date: Tue, 26 May 2026 15:03:40 +0300 Subject: [PATCH] feat: add fmt command --- rust-toolchain.toml | 2 ++ src/cli/fmt.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++ src/cli/help.rs | 1 + src/cli/mod.rs | 1 + src/main.rs | 2 +- 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 rust-toolchain.toml create mode 100644 src/cli/fmt.rs diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..5d56faf --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly" diff --git a/src/cli/fmt.rs b/src/cli/fmt.rs new file mode 100644 index 0000000..1c1ef64 --- /dev/null +++ b/src/cli/fmt.rs @@ -0,0 +1,56 @@ +use crate::utils::log::error; +use crate::utils::text::{colored, BOLD_GREEN}; +use glob::glob; +use std::process::Command; + +pub fn fmt(_args: &[String]) -> i32 { + let patterns = [ + "src/**/*.c", + "src/**/*.cpp", + "src/**/*.h", + "src/**/*.hpp", + "tests/**/*.c", + "tests/**/*.cpp", + "tests/**/*.h", + "tests/**/*.hpp", + ]; + + let mut files = Vec::new(); + for pattern in patterns { + if let Ok(paths) = glob(pattern) { + for path in paths.flatten() { + if let Some(s) = path.to_str() { + files.push(s.to_string()); + } + } + } + } + + if files.is_empty() { + println!(" {} No files to format", colored("Format", BOLD_GREEN)); + return 0; + } + + println!( + " {} {} files", + colored("Formatting", BOLD_GREEN), + files.len() + ); + + let status = Command::new("clang-format").arg("-i").args(&files).status(); + + match status { + Ok(s) if s.success() => { + println!(" {} successful", colored("Format", BOLD_GREEN)); + 0 + } + Ok(s) => { + error(&format!("clang-format failed with status: {}", s)); + 1 + } + Err(e) => { + error(&format!("Failed to execute clang-format: {}", e)); + 1 + } + } +} diff --git a/src/cli/help.rs b/src/cli/help.rs index 10ee2ba..eb791a1 100644 --- a/src/cli/help.rs +++ b/src/cli/help.rs @@ -31,6 +31,7 @@ pub fn help() -> i32 { println!(" run [--profile] Build and run the project (default: --debug)"); println!(" tree Display the dependency tree"); println!(" test Run project tests (alias: tests)"); + println!(" fmt Format all C/C++ source files using clang-format"); println!(" clean Remove the target directory"); println!(" gen Generate IDE integration files"); printc("FLAGS:", BOLD_GREEN); diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 9528ea1..389d98c 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -20,6 +20,7 @@ pub mod build; pub mod clean; pub mod flag_update; pub mod flags; +pub mod fmt; pub mod r#gen; pub mod help; pub mod init; diff --git a/src/main.rs b/src/main.rs index d0dc1dd..fcb0ae7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,6 @@ // // You should have received a copy of the GNU General Public License // along with this program. If not, see . - use crate::utils::log::error; mod cli; mod config; @@ -41,6 +40,7 @@ fn main() { "run" => cli::run::run(rest), "tree" => cli::tree::tree(rest), "test" | "tests" => cli::test::test(rest), + "fmt" => cli::fmt::fmt(rest), "clean" => cli::clean::clean(rest), "gen" => cli::r#gen::r#gen(rest), "--version" => {