Skip to content

Commit b7f06db

Browse files
committed
Remove trailing newline for run_fun!
Do not to call trim() every time, which is the same as in bash
1 parent b5fe661 commit b7f06db

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

README.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ if run_cmd! {
3333
## run_fun! --> FunResult
3434
```rust
3535
let version = run_fun!("rustc --version")?;
36-
info!("Your rust version is {}", version.trim());
36+
info!("Your rust version is {}", version);
3737

3838
// with pipes
3939
let n = run_fun!("echo the quick brown fox jumped over the lazy dog | wc -w")?;
40-
info!("There are {} words in above sentence", n.trim());
40+
info!("There are {} words in above sentence", n);
4141
```
4242

4343
## Run pipe commands in the builder style
@@ -105,7 +105,7 @@ FATAL: Command exit unexpectedly: disk is full
105105
## Complete Example
106106

107107
```rust
108-
use cmd_lib::{info, warn, output, run_cmd, run_fun, CmdResult, FunResult};
108+
use cmd_lib::{info, warn, run_cmd, run_fun, CmdResult, FunResult};
109109

110110
fn foo() -> CmdResult {
111111
let dir = "/var/tmp";
@@ -120,16 +120,18 @@ fn foo() -> CmdResult {
120120
}
121121

122122
fn get_year() -> FunResult {
123-
let year = run_fun!("date +%Y")?;
124-
output!("{}", year.trim())
123+
run_fun!("date +%Y")
125124
}
126125

127126
fn main() -> CmdResult {
127+
run_cmd!(lcd /tmp; ls | wc -l;)?;
128+
run_cmd!("pwd")?;
129+
128130
let name = "rust";
129131
run_cmd!("echo hello, {}", name)?;
130132

131133
let result = run_fun!("du -ah . | sort -hr | head -n 5")?;
132-
info!("Top 5 directories:\n{}", result.trim());
134+
info!("Top 5 directories:\n{}", result);
133135

134136
if foo().is_err() {
135137
warn!("Failed to run foo()");
@@ -147,14 +149,21 @@ fn main() -> CmdResult {
147149

148150
output:
149151
```bash
152+
INFO: Set local current_dir: "/tmp"
153+
INFO: Running "ls | wc -l (cd: /tmp)" ...
154+
42
155+
INFO: Running "pwd" ...
156+
/home/tao/src/rust-shell-script/rust_cmd_lib
150157
INFO: Running "echo hello, rust" ...
151158
hello, rust
152159
INFO: Running "du -ah . | sort -hr | head -n 5" ...
153160
INFO: Top 5 directories:
154-
24K .
155-
16K ./lib.rs
156-
4.0K ./main.rs
157-
INFO: Set process current_dir: "/var/tmp"
161+
488M .
162+
485M ./target
163+
286M ./target/debug
164+
170M ./target/debug/incremental
165+
163M ./target/package
166+
INFO: Set env current_dir: "/var/tmp"
158167
INFO: Running "sleep 3" ...
159168
INFO: Running "ls nofile" ...
160169
ls: cannot access 'nofile': No such file or directory

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,11 @@ impl ProcessResult for FunResult {
261261
if !output.status.success() {
262262
Err(to_io_error(&full_cmd_str, output.status))
263263
} else {
264-
Ok(String::from_utf8_lossy(&output.stdout).to_string())
264+
let mut ans = String::from_utf8_lossy(&output.stdout).to_string();
265+
if ans.ends_with('\n') {
266+
ans.pop();
267+
}
268+
Ok(ans)
265269
}
266270
}
267271
}

0 commit comments

Comments
 (0)