Skip to content

Commit

Permalink
Update cli argment for auto correct
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Jun 29, 2021
1 parent 30d85cf commit 04207b1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT"
name = "autocorrect"
readme = "README.md"
repository = "https://github.com/huacnlee/auto-correct.rs"
version = "0.5.0"
version = "0.5.1"

[lib]
name = "autocorrect"
Expand Down
41 changes: 10 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,53 +27,32 @@ after that, you will get `/usr/local/bin/autocorrect` command.

```bash
$ autocorrect -h
AutoCorrect 0.5.0
AutoCorrect 0.5.1
Jason Lee <[email protected]
Automatically add whitespace between CJK (Chinese, Japanese, Korean) and half-width characters (alphabetical letters,
numerical digits and symbols).

USAGE:
autocorrect [OPTIONS] [text]
autocorrect [FLAGS] [file]...

FLAGS:
-A, --auto-correct-all Auto-correct and rewrite file.
--fix Automatically fix problems and rewrite file.
-h, --help Prints help information
-V, --version Prints version information

OPTIONS:
-t, --type <type> File content type [text, html, yaml], default detect with file extension.

ARGS:
<text> Target filepath or string (Plain text) for format
<file>... Target filepath or dir for format
```

## Usage

```bash
$ autocorrect 你好Hello世界
你好 Hello 世界
$ autocorrect text.md
$ autocorrect text.txt
你好 Hello 世界

$ autocorrect text.md > text-new.md
```

### Format HTML

```bash
$ autocorrect --type html '<p>你好hello世界</p><p>你好world世界</p>'
<p>你好 hello 世界</p><p>你好 world 世界</p>

$ autocorrect text.html
```

### Format YAML

````bash
$ autocorrect --type yaml 'foo: "你好hello"'
foo: "你好 hello"

$ autocorrect /path/to/yml/zh-CN.yml
$ autocorrect --fix text.txt
$ autocorrect --fix zh-CN.yml
$ autocorrect --fix ./
```

## Usage in Rust
Expand All @@ -82,8 +61,8 @@ In your Cargo.toml

```toml
[dependencies]
autocorrect = "0.4.0"
````
autocorrect = "0.5.0"
```

Use `autocorrect::format` to format plain text.

Expand Down
113 changes: 60 additions & 53 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,77 +53,84 @@ pub fn main() {
.arg(
Arg::with_name("file").help("Target filepath or dir for format").takes_value(true).required(false).multiple(true)
)
.arg(
Arg::with_name("type").long("type").short("t").help("File content type [text, html, yaml], default detect with file extension.").takes_value(true)
)
.arg(
Arg::with_name("fix").long("fix").help("Automatically fix problems and rewrite file.")
)
.get_matches();

let fix = matches.is_present("fix");
if let Some(file_name) = matches.value_of("file") {
for f in glob(file_name).unwrap() {
let path: String;
match f {
Ok(_path) => path = String::from(_path.to_str().unwrap()),
Err(_e) => break,
}
let raw = fs::read_to_string(path.as_str()).unwrap();
let mut ext = get_file_extension(path.as_str());
if let Some(_type) = matches.value_of("type") {
ext = _type;

if let Some(file_names) = matches.values_of("file") {
for file_name in file_names {
let filepath = Path::new(file_name);
let mut file_name = String::from(file_name);

if !filepath.is_file() {
file_name.push_str("/**/*");
}

format_and_output(path.as_str(), fix, ext, raw.as_str());
file_name = file_name.replace("//", "/");

for f in glob(file_name.as_str()).unwrap() {
match f {
Ok(_path) => format_and_output(_path.to_str().unwrap(), fix),
Err(_e) => break,
}
}
}
}
}

fn format_and_output(path: &str, fix: bool, ext: &str, raw: &str) {
let mut out = String::from(raw);
if EXT_MAPS.contains_key(ext) {
match EXT_MAPS[ext] {
"html" => {
out = format_html(raw);
}
"yaml" => {
out = format_yaml(raw);
}
"sql" => {
out = format_sql(raw);
}
"rust" => {
out = format_rust(raw);
}
"ruby" => {
out = format_ruby(raw);
}
"go" => {
out = format_go(raw);
}
"javascript" => {
out = format_javascript(raw);
}
"text" => {
out = format(raw);
fn format_and_output(path: &str, fix: bool) {
if let Ok(raw) = fs::read_to_string(path) {
println!("{}", path);

let raw = raw.as_str();
let mut out = String::from(raw);
let ext = get_file_extension(path);

if EXT_MAPS.contains_key(ext) {
match EXT_MAPS[ext] {
"html" => {
out = format_html(raw);
}
"yaml" => {
out = format_yaml(raw);
}
"sql" => {
out = format_sql(raw);
}
"rust" => {
out = format_rust(raw);
}
"ruby" => {
out = format_ruby(raw);
}
"go" => {
out = format_go(raw);
}
"javascript" => {
out = format_javascript(raw);
}
"text" => {
out = format(raw);
}
_ => {}
}
_ => {}
}
}

if fix && path.len() > 0 {
fs::write(Path::new(path), out).unwrap();
} else {
println!("{}", out);
if fix && path.len() > 0 {
fs::write(Path::new(path), out).unwrap();
} else {
println!("{}", out);
}
}
}

fn get_file_extension(filepath: &str) -> &str {
let ext = Path::new(filepath)
.extension()
.and_then(OsStr::to_str)
.unwrap();
if let Some(ext) = Path::new(filepath).extension().and_then(OsStr::to_str) {
return ext;
}

return ext;
return "";
}

0 comments on commit 04207b1

Please sign in to comment.