From d56d31cc6427ed124f19fd26c6a15e5bd645a1d4 Mon Sep 17 00:00:00 2001 From: Hiers Date: Sun, 19 Feb 2023 11:01:24 +0000 Subject: [PATCH] Removed the pager crate in favour of implementing that functionality in jisho-cli. Added windows version of terminal_size function. --- Cargo.lock | 99 ++++++++++++++++++++++++++++++++++++----------------- Cargo.toml | 4 ++- src/main.rs | 70 +++++++++++++++++++++++++++++++------ 3 files changed, 130 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 980e034..e0092a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -69,27 +69,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "errno" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" -dependencies = [ - "errno-dragonfly", - "libc", - "winapi", -] - -[[package]] -name = "errno-dragonfly" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" -dependencies = [ - "cc", - "libc", -] - [[package]] name = "form_urlencoded" version = "1.0.1" @@ -135,10 +114,10 @@ dependencies = [ "atty", "colored", "libc", - "pager", "serde_json", "ureq", "webbrowser", + "windows-sys", ] [[package]] @@ -183,16 +162,6 @@ version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" -[[package]] -name = "pager" -version = "0.16.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2599211a5c97fbbb1061d3dc751fa15f404927e4846e07c643287d6d1f462880" -dependencies = [ - "errno", - "libc", -] - [[package]] name = "percent-encoding" version = "2.1.0" @@ -491,3 +460,69 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" diff --git a/Cargo.toml b/Cargo.toml index e430d6a..292edae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,9 @@ repository = "https://github.com/JojiiOfficial/jisho-cli" license = "GPL-3.0" edition = "2018" +[target.'cfg(windows)'.dependencies] +windows-sys = { version = "0.45.0", features = ["Win32_Foundation", "Win32_System_Console"] } + [dependencies] ureq = { version = "2.1.0", features = ["json"] } libc = "0.2.139" @@ -17,6 +20,5 @@ colored = "2.0.0" argparse = "0.2.2" webbrowser = "0.5.5" atty = "0.2" -pager = "0.16.1" [features] diff --git a/src/main.rs b/src/main.rs index 3bc605e..7f27157 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 { + 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 { } } } + +#[cfg(windows)] +fn terminal_size() -> Result { + 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) + } + } +}