Skip to content

Commit

Permalink
feat: add in-place mode
Browse files Browse the repository at this point in the history
Resolves #376
  • Loading branch information
bioinformatist committed Apr 18, 2024
1 parent 3bf7f93 commit d5a859c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 38 deletions.
82 changes: 52 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ slug = "0.1.4"
emojis = { version = "0.5.2", optional = true }
arbitrary = { version = "1", optional = true, features = ["derive"] }
derive_builder = "0.12.0"
in-place = "0.2.0"

[dev-dependencies]
ntest = "0.9"
Expand Down
44 changes: 36 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ use std::path::PathBuf;
use std::process;

use clap::{Parser, ValueEnum};
use in_place::InPlace;

const EXIT_SUCCESS: i32 = 0;
const EXIT_PARSE_CONFIG: i32 = 2;
const EXIT_READ_INPUT: i32 = 3;
const EXIT_CHECK_FILE_NUM: i32 = 4;

#[derive(Debug, Parser)]
#[command(about, author, version)]
Expand All @@ -35,6 +37,10 @@ struct Cli {
#[arg(short, long, value_name = "PATH", default_value = get_default_config_path())]
config_file: String,

/// To perform an in-place formatting
#[arg(short, long, conflicts_with_all(["format", "output"]))]
inplace: bool,

/// Treat newlines as hard line breaks
#[arg(long)]
hardbreaks: bool,
Expand Down Expand Up @@ -204,6 +210,18 @@ fn cli_with_config() -> Cli {
fn main() -> Result<(), Box<dyn Error>> {
let cli = cli_with_config();

if cli.inplace {
if let Some(ref files) = cli.files {
if files.len() != 1 {
eprintln!("cannot have more than 1 input file with in-place mode");
process::exit(EXIT_CHECK_FILE_NUM);
}
} else {
eprintln!("no input file specified: cannot use standard input with in-place mode");
process::exit(EXIT_CHECK_FILE_NUM);
}
}

let exts = &cli.extensions;

let mut extension = ExtensionOptionsBuilder::default();
Expand Down Expand Up @@ -272,8 +290,8 @@ fn main() -> Result<(), Box<dyn Error>> {
None => {
std::io::stdin().read_to_end(&mut s)?;
}
Some(fs) => {
for f in &fs {
Some(ref fs) => {
for f in fs {
match fs::File::open(f) {
Ok(mut io) => {
io.read_to_end(&mut s)?;
Expand All @@ -290,19 +308,29 @@ fn main() -> Result<(), Box<dyn Error>> {
let arena = Arena::new();
let root = comrak::parse_document(&arena, &String::from_utf8(s)?, &options);

let formatter = match cli.format {
Format::Html => {
plugins.render.codefence_syntax_highlighter = syntax_highlighter;
comrak::format_html_with_plugins
let formatter = if cli.inplace {
comrak::format_commonmark_with_plugins
} else {
match cli.format {
Format::Html => {
plugins.render.codefence_syntax_highlighter = syntax_highlighter;
comrak::format_html_with_plugins
}
Format::Xml => comrak::format_xml_with_plugins,
Format::CommonMark => comrak::format_commonmark_with_plugins,
}
Format::Xml => comrak::format_xml_with_plugins,
Format::CommonMark => comrak::format_commonmark_with_plugins,
};

if let Some(output_filename) = cli.output {
let mut bw = BufWriter::new(fs::File::create(output_filename)?);
formatter(root, &options, &mut bw, &plugins)?;
bw.flush()?;
} else if cli.inplace {
let inp: in_place::InPlaceFile =
InPlace::new(unsafe { cli.files.unwrap_unchecked().get_unchecked(0) }).open()?;
let mut bw = inp.writer();
formatter(root, &options, &mut bw, &plugins)?;
inp.save()?;
} else {
let stdout = std::io::stdout();
let mut bw = BufWriter::new(stdout.lock());
Expand Down

0 comments on commit d5a859c

Please sign in to comment.