Removed the pager crate in favour of implementing that functionality in jisho-cli. Added windows version of terminal_size function.

This commit is contained in:
Hiers 2023-02-19 11:01:24 +00:00
parent bc8a29fcf3
commit d56d31cc64
3 changed files with 130 additions and 43 deletions

View file

@ -1,15 +1,14 @@
use std::{
io::{stdin, stdout, Write},
process::{Command, Stdio},
thread::{self, JoinHandle},
env,
};
use libc::{c_ushort, ioctl, STDOUT_FILENO, TIOCGWINSZ};
use argparse::{ArgumentParser, List, Print, Store, StoreTrue};
use colored::*;
use serde_json::Value;
use atty::Stream;
use pager::Pager;
macro_rules! JISHO_URL {
() => {
@ -48,7 +47,6 @@ fn main() -> Result<(), ureq::Error> {
term_size = 0;
}
let mut lines_output = 0;
let options = parse_args();
let mut query = {
@ -66,6 +64,9 @@ fn main() -> Result<(), ureq::Error> {
};
loop {
let mut lines_output = 0;
let mut output = String::new();
if options.kanji {
// Open kanji page here
let threads = query
@ -105,7 +106,6 @@ fn main() -> Result<(), ureq::Error> {
println!();
}
let mut output = String::new();
// Iterate over meanings and print them
for (i, entry) in body.iter().enumerate() {
if i >= options.limit && options.limit != 0 {
@ -124,13 +124,32 @@ fn main() -> Result<(), ureq::Error> {
lines_output -= 1;
}
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 */
env::set_var("CLICOLOR_FORCE", "1");
Pager::with_pager("less -R").setup();
}
print!("{}", output);
}
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
env::set_var("CLICOLOR_FORCE", "1");
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 {
print!("{}", output);
}
if !options.interactive {
@ -342,7 +361,10 @@ fn parse_args() -> Options {
options
}
#[cfg(unix)]
fn terminal_size() -> Result<usize, i16> {
use libc::{c_ushort, ioctl, STDOUT_FILENO, TIOCGWINSZ};
unsafe {
let mut size: c_ushort = 0;
if ioctl(STDOUT_FILENO, TIOCGWINSZ.into(), &mut size as *mut _) != 0 {
@ -352,3 +374,31 @@ fn terminal_size() -> Result<usize, i16> {
}
}
}
#[cfg(windows)]
fn terminal_size() -> Result<usize, i16> {
use windows_sys::Win32::System::Console::*;
unsafe {
let handle = GetStdHandle(STD_OUTPUT_HANDLE) as windows_sys::Win32::Foundation::HANDLE;
// Unlike the linux function, rust will complain if only part of the struct is sent
let mut window = CONSOLE_SCREEN_BUFFER_INFO {
dwSize: COORD { X: 0, Y: 0},
dwCursorPosition: COORD { X: 0, Y: 0},
wAttributes: 0,
dwMaximumWindowSize: COORD {X: 0, Y: 0},
srWindow: SMALL_RECT {
Top: 0,
Bottom: 0,
Left: 0,
Right: 0
}
};
if GetConsoleScreenBufferInfo(handle, &mut window) == 0 {
Err(0)
} else {
Ok((window.srWindow.Bottom - window.srWindow.Top) as usize)
}
}
}