Skip to content

Commit e520b05

Browse files
authored
Merge pull request #17 from Gadiguibou/add-piping
Add piping
2 parents c6f4d31 + 5f73d87 commit e520b05

File tree

1 file changed

+57
-18
lines changed

1 file changed

+57
-18
lines changed

src/lib.rs

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::env;
99
use std::error::Error;
1010
use std::ffi::OsStr;
1111
use std::fs;
12+
use std::io;
1213
use std::path::*;
1314
use std::time::Instant;
1415

@@ -24,6 +25,7 @@ pub struct Config {
2425
recursive: bool,
2526
include_dir: bool,
2627
quiet: bool,
28+
text: bool,
2729
}
2830

2931
impl Config {
@@ -41,24 +43,6 @@ impl Config {
4143
.required(false)
4244
.index(1),
4345
)
44-
.arg(
45-
Arg::with_name("recursive")
46-
.help("Makes renaming recursive, renaming files in subfolders as well")
47-
.short("r")
48-
.long("recursive"),
49-
)
50-
.arg(
51-
Arg::with_name("directories")
52-
.help("Renames directories as well")
53-
.short("D")
54-
.long("dir")
55-
)
56-
.arg(
57-
Arg::with_name("quiet")
58-
.help("Suppress output")
59-
.short("q")
60-
.long("quiet")
61-
)
6246
.arg(
6347
Arg::with_name("camelCase")
6448
.help("Uses the camelCase naming convention")
@@ -111,6 +95,29 @@ impl Config {
11195
.required(true)
11296
.args(&["camelCase","kebab-case","PascalCase","SCREAMING_SNAKE_CASE","Sentence case","snake_case","Title Case","Train-Case"]),
11397
)
98+
.arg(
99+
Arg::with_name("recursive")
100+
.help("Makes renaming recursive, renaming files in subfolders as well")
101+
.short("r")
102+
.long("recursive"),
103+
)
104+
.arg(
105+
Arg::with_name("directories")
106+
.help("Renames directories as well")
107+
.short("D")
108+
.long("dir")
109+
)
110+
.arg(
111+
Arg::with_name("text")
112+
.help("Reads lines from stdin and translates them to the given convention in stdout until the first empty line")
113+
.long("text")
114+
)
115+
.arg(
116+
Arg::with_name("quiet")
117+
.help("Suppress output")
118+
.short("q")
119+
.long("quiet")
120+
)
114121
.after_help("Full documentation available here: https://github.com/Gadiguibou/stdrename")
115122
.get_matches();
116123

@@ -145,12 +152,15 @@ impl Config {
145152
let recursive = matches.is_present("recursive");
146153
let include_dir = matches.is_present("directories");
147154
let quiet = matches.is_present("quiet");
155+
let text = matches.is_present("text");
156+
148157
Ok(Config {
149158
target_dir,
150159
naming_convention,
151160
recursive,
152161
include_dir,
153162
quiet,
163+
text,
154164
})
155165
}
156166
}
@@ -160,6 +170,35 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
160170

161171
let mut files_renamed: u64 = 0;
162172

173+
// If the text flag is specified, read from stdin and translate to stdout instead of renaming files
174+
if config.text {
175+
let stdin = io::stdin();
176+
177+
loop {
178+
let mut input = String::new();
179+
180+
let len = stdin.read_line(&mut input)?;
181+
182+
if len == 0 || input.trim().is_empty() {
183+
let running_time: f32 = start_time.elapsed().as_micros() as f32 / 1_000_000f32;
184+
185+
if !config.quiet {
186+
println!(
187+
"{} names translated in {} s. See you next time!\n(^ _ ^)/",
188+
files_renamed, running_time
189+
)
190+
};
191+
192+
return Ok(());
193+
} else {
194+
let translation =
195+
change_naming_convention(&PathBuf::from(&input), &config.naming_convention)?;
196+
println!("{}", translation);
197+
files_renamed += 1;
198+
}
199+
}
200+
}
201+
163202
let mut walk_builder = WalkBuilder::new(&config.target_dir);
164203

165204
walk_builder

0 commit comments

Comments
 (0)