-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
auto-color
feature and other improvements (#9)
- Loading branch information
Showing
8 changed files
with
173 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use clap_mangen::Man; | ||
use std::{ | ||
env, | ||
fs::File, | ||
io::Error, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
include!("src/cli.rs"); | ||
|
||
fn build_manpages(outdir: &Path) -> Result<(), Error> { | ||
let app = cli(); | ||
|
||
let file = Path::new(&outdir).join("example.1"); | ||
let mut file = File::create(&file)?; | ||
|
||
Man::new(app).render(&mut file)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
println!("cargo:rerun-if-changed=src/cli.rs"); | ||
println!("cargo:rerun-if-changed=man"); | ||
|
||
let outdir = match env::var_os("OUT_DIR") { | ||
None => return Ok(()), | ||
Some(outdir) => outdir, | ||
}; | ||
|
||
// Create `target/assets/` folder. | ||
let out_path = PathBuf::from(outdir); | ||
let mut path = out_path.ancestors().nth(4).unwrap().to_owned(); | ||
path.push("assets"); | ||
std::fs::create_dir_all(&path).unwrap(); | ||
|
||
build_manpages(&path)?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use clap::{value_parser, Arg, ArgAction, Command}; | ||
|
||
pub fn cli() -> Command { | ||
Command::new("hledger-fmt") | ||
.long_about("An opinionated hledger's journal files formatter.") | ||
.override_usage(concat!("hledger-fmt [OPTIONS] [FILES]...\n")) | ||
.arg( | ||
Arg::new("files") | ||
.help(concat!( | ||
"Paths of files to format. To read from STDIN pass '-'.\n", | ||
"\n", | ||
"If not defined, hledger-fmt will search for hledger files in the", | ||
" current directory and its subdirectories (those that have the", | ||
" extensions '.journal', '.hledger' or '.j').", | ||
" If the paths passed are directories, hledger-fmt will search for", | ||
" hledger files in those directories and their subdirectories.", | ||
)) | ||
.action(ArgAction::Append) | ||
.value_parser(value_parser!(String)) | ||
.value_name("FILES") | ||
.num_args(1..), | ||
) | ||
.arg( | ||
Arg::new("fix") | ||
.long("fix") | ||
.help(concat!( | ||
"Fix the files in place. WARNING: this is a potentially destructive", | ||
" operation, make sure to make a backup of your files or print the diff", | ||
" first. If not passed, hledger-fmt will print the diff between the", | ||
" original and the formatted file." | ||
)) | ||
.action(ArgAction::SetTrue), | ||
) | ||
.arg( | ||
Arg::new("no-diff") | ||
.long("no-diff") | ||
.help(concat!( | ||
"Don't print diff between original and formatted files,", | ||
" but formatted content instead." | ||
)) | ||
.action(ArgAction::SetTrue), | ||
) | ||
.disable_help_flag(true) | ||
.arg( | ||
Arg::new("help") | ||
.short('h') | ||
.long("help") | ||
.help("Print help.") | ||
.action(ArgAction::Help), | ||
) | ||
.disable_version_flag(true) | ||
.version(env!("CARGO_PKG_VERSION")) | ||
.arg( | ||
Arg::new("version") | ||
.short('V') | ||
.long("version") | ||
.help("Print version.") | ||
.action(ArgAction::Version), | ||
) | ||
.after_help("To disable colors in the output, use the environment variable NO_COLOR.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
mod cli; | ||
pub(crate) mod common; | ||
pub mod formatter; | ||
pub mod parser; | ||
|
||
pub use cli::cli; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters