feat(cli): fmt subcommand
This commit is contained in:
parent
de4ff9aa61
commit
9f1a4c84ee
2 changed files with 47 additions and 0 deletions
37
orgize-cli/src/fmt.rs
Normal file
37
orgize-cli/src/fmt.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
use clap::Args;
|
||||
use orgize::Org;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::diff;
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct Command {
|
||||
path: Vec<PathBuf>,
|
||||
|
||||
#[arg(short, long)]
|
||||
dry_run: bool,
|
||||
}
|
||||
|
||||
impl Command {
|
||||
pub fn run(self) -> anyhow::Result<()> {
|
||||
for path in self.path {
|
||||
if !path.exists() {
|
||||
tracing::error!("{:?} is not existed", path);
|
||||
|
||||
let input = std::fs::read_to_string(&path)?;
|
||||
|
||||
let org = Org::parse(&input);
|
||||
|
||||
let patches = orgize_common::formatting(&org);
|
||||
|
||||
if self.dry_run {
|
||||
diff::print(&input, patches);
|
||||
} else {
|
||||
diff::write_to_file(&input, patches, path)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -1,12 +1,14 @@
|
|||
mod detangle;
|
||||
mod diff;
|
||||
mod execute_src_block;
|
||||
mod fmt;
|
||||
mod tangle;
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
use clap_verbosity_flag::{InfoLevel, LevelFilter as CLevelFilter, Verbosity};
|
||||
use tracing::level_filters::LevelFilter;
|
||||
|
||||
/// Command line utility for org-mode files
|
||||
#[derive(Debug, Parser)]
|
||||
#[clap(name = "orgize-tools", version)]
|
||||
pub struct App {
|
||||
|
|
@ -19,14 +21,21 @@ pub struct App {
|
|||
|
||||
#[derive(Debug, Subcommand)]
|
||||
enum Command {
|
||||
/// Tangle source block contents to destination files
|
||||
#[clap(name = "tangle")]
|
||||
Tangle(tangle::Command),
|
||||
|
||||
/// Insert tangled file contents back to source files
|
||||
#[clap(name = "detangle")]
|
||||
Detangle(detangle::Command),
|
||||
|
||||
/// Execute source block
|
||||
#[clap(name = "execute-src-block")]
|
||||
ExecuteSrcBlock(execute_src_block::Command),
|
||||
|
||||
/// Format org-mode files
|
||||
#[clap(name = "fmt")]
|
||||
Format(fmt::Command),
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
|
|
@ -50,5 +59,6 @@ fn main() -> anyhow::Result<()> {
|
|||
Command::Tangle(cmd) => cmd.run(),
|
||||
Command::Detangle(cmd) => cmd.run(),
|
||||
Command::ExecuteSrcBlock(cmd) => cmd.run(),
|
||||
Command::Format(cmd) => cmd.run(),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue