Move child process code to separate function and other small changes.
This commit is contained in:
parent
d56d31cc64
commit
1c3c5644d3
1 changed files with 28 additions and 25 deletions
53
src/main.rs
53
src/main.rs
|
@ -39,10 +39,7 @@ fn main() -> Result<(), ureq::Error> {
|
||||||
let term_size;
|
let term_size;
|
||||||
|
|
||||||
if atty::is(Stream::Stdout) {
|
if atty::is(Stream::Stdout) {
|
||||||
match terminal_size() {
|
term_size = terminal_size().unwrap_or(0);
|
||||||
Ok(s) => term_size = s,
|
|
||||||
Err(_e) => term_size = 0
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
term_size = 0;
|
term_size = 0;
|
||||||
}
|
}
|
||||||
|
@ -129,25 +126,7 @@ fn main() -> Result<(), ureq::Error> {
|
||||||
if lines_output >= term_size - 1 && term_size != 0 {
|
if lines_output >= term_size - 1 && term_size != 0 {
|
||||||
// Output is a different process that is not a tty (i.e. less), but we want to keep colour
|
// Output is a different process that is not a tty (i.e. less), but we want to keep colour
|
||||||
env::set_var("CLICOLOR_FORCE", "1");
|
env::set_var("CLICOLOR_FORCE", "1");
|
||||||
|
pipe_to_less(output);
|
||||||
match Command::new("less")
|
|
||||||
.arg("-R")
|
|
||||||
.stdin(Stdio::piped())
|
|
||||||
.spawn() {
|
|
||||||
Ok(mut process) => {
|
|
||||||
if let Err(e) = process.stdin.as_ref().unwrap().write_all(output.as_bytes()) {
|
|
||||||
panic!("couldn't pipe to less: {}", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// We don't care about the return value, only whether wait failed or not
|
|
||||||
if process.wait().is_err() {
|
|
||||||
panic!("wait() was called on non-existent child process\
|
|
||||||
- this should not be possible");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// less not found in PATH; print normally
|
|
||||||
Err(_e) => print!("{}", output)
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
print!("{}", output);
|
print!("{}", output);
|
||||||
}
|
}
|
||||||
|
@ -170,7 +149,6 @@ fn main() -> Result<(), ureq::Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_item(query: &str, value: &Value, output: &mut String) -> Option<usize> {
|
fn print_item(query: &str, value: &Value, output: &mut String) -> Option<usize> {
|
||||||
let mut aux;
|
|
||||||
let japanese = value_to_arr(value.get("japanese")?).get(0)?.to_owned();
|
let japanese = value_to_arr(value.get("japanese")?).get(0)?.to_owned();
|
||||||
|
|
||||||
let reading = japanese
|
let reading = japanese
|
||||||
|
@ -180,7 +158,7 @@ fn print_item(query: &str, value: &Value, output: &mut String) -> Option<usize>
|
||||||
|
|
||||||
let word = value_to_str(japanese.get("word").unwrap_or(japanese.get("reading")?));
|
let word = value_to_str(japanese.get("word").unwrap_or(japanese.get("reading")?));
|
||||||
|
|
||||||
aux = format!("{}[{}] {}\n", word, reading, format_result_tags(value));
|
let mut aux = format!("{}[{}] {}\n", word, reading, format_result_tags(value));
|
||||||
*output += &aux;
|
*output += &aux;
|
||||||
|
|
||||||
// Print senses
|
// Print senses
|
||||||
|
@ -361,6 +339,31 @@ fn parse_args() -> Options {
|
||||||
options
|
options
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn pipe_to_less(output: String) {
|
||||||
|
|
||||||
|
let command = Command::new("less")
|
||||||
|
.arg("-R")
|
||||||
|
.stdin(Stdio::piped())
|
||||||
|
.spawn();
|
||||||
|
|
||||||
|
match command {
|
||||||
|
Ok(mut process) => {
|
||||||
|
if let Err(e) = process.stdin.as_ref().unwrap().write_all(output.as_bytes()) {
|
||||||
|
panic!("couldn't pipe to less: {}", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// We don't care about the return value, only whether wait failed or not
|
||||||
|
if process.wait().is_err() {
|
||||||
|
panic!("wait() was called on non-existent child process\
|
||||||
|
- this should not be possible");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// less not found in PATH; print normally
|
||||||
|
Err(_e) => print!("{}", output)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn terminal_size() -> Result<usize, i16> {
|
fn terminal_size() -> Result<usize, i16> {
|
||||||
use libc::{c_ushort, ioctl, STDOUT_FILENO, TIOCGWINSZ};
|
use libc::{c_ushort, ioctl, STDOUT_FILENO, TIOCGWINSZ};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue