use std::io::Write;
use std::process::{Command, Stdio};
use crate::{
macros::{config::trixy::TrixyConfig, VIM_LINE_C},
parser::command_spec::CommandSpec,
};
pub mod c;
pub fn format_c(input: String) -> String {
let mut clang_format = Command::new("clang-format")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.args(["--style", "GNU"])
.spawn()
.expect("`clang-format` is a dependency and should be provided by the user");
let mut stdin = clang_format
.stdin
.take()
.expect("We should be able to take stdin");
std::thread::spawn(move || {
stdin
.write_all(input.as_bytes())
.expect("Should be able to write to stdin");
});
let output = clang_format
.wait_with_output()
.expect("Should be able to read stdout");
String::from_utf8(output.stdout).expect("The input was utf8, it should not have changed")
}
pub fn generate(trixy: &CommandSpec, config: &TrixyConfig) -> String {
let c_code = c::generate(trixy, config);
let c_code = format_c(c_code);
format!(
"\
{}\n\
{}",
c_code, VIM_LINE_C
)
}