print a plain version when no tty

pull/14/head
chris west 4 years ago
parent c951731e58
commit 8645571d7a

21
Cargo.lock generated

@ -1,5 +1,15 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "autocfg"
version = "0.1.7"
@ -70,6 +80,14 @@ dependencies = [
"wasi 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "hermit-abi"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -144,6 +162,7 @@ dependencies = [
name = "phetch"
version = "0.1.13-dev"
dependencies = [
"atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
"native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"termion 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
@ -339,6 +358,7 @@ dependencies = [
]
[metadata]
"checksum atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
"checksum autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2"
"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5"
@ -350,6 +370,7 @@ dependencies = [
"checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
"checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
"checksum getrandom 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "e7db7ca94ed4cd01190ceee0d8a8052f08a247aa1b469a7f68c6a3b71afcf407"
"checksum hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eff2656d88f158ce120947499e971d743c05dbcbed62e5bd2f38f1698bbc3772"
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
"checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558"
"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"

@ -34,4 +34,5 @@ dev-version-ext = "dev"
termion = "1.5.3"
native-tls = "0.2"
libc = "0.2.66"
tor-stream = "0.2.0"
tor-stream = "0.2.0"
atty = "0.2.14"

@ -1,3 +1,4 @@
use atty;
use phetch::{
config, gopher,
ui::{Mode, UI},
@ -163,6 +164,11 @@ fn run() -> i32 {
return 0;
}
if mode == Mode::Print && !atty::is(atty::Stream::Stdout) {
// not a tty so print an almost-raw version of the response
return print_plain(&cfg.start, cfg.tls, cfg.tor);
}
if mode == Mode::Print {
cfg.cursor = false;
cfg.wide = true;
@ -241,7 +247,40 @@ fn print_raw(url: &str, tls: bool, tor: bool) {
Ok((_, response)) => println!("{}", response),
Err(e) => {
eprintln!("{}", e);
exit(0)
exit(1)
}
}
}
/// Print a colorless, plain version of the response for a non-tty
/// (like a pipe).
fn print_plain(url: &str, tls: bool, tor: bool) -> i32 {
let mut out = String::new();
let (typ, _, _, _) = gopher::parse_url(url);
match gopher::fetch_url(url, tls, tor) {
Ok((_, response)) => match typ {
gopher::Type::Menu => {
// TODO use parse_line()
for line in response.trim_end_matches(".\r\n").lines() {
let line = line.trim_end_matches('\r');
if let Some(desc) = line.splitn(2, "\t").nth(0) {
let desc = desc.trim();
out.push_str(&desc.chars().skip(1).collect::<String>());
out.push('\n');
}
}
}
gopher::Type::Text => println!("{}", response.trim_end_matches(".\r\n")),
_ => {
eprintln!("can't print gopher type: {:?}", typ);
return 1;
}
},
Err(e) => {
eprintln!("{}", e);
return 1;
}
}
print!("{}", out);
0
}

@ -175,7 +175,7 @@ impl Menu {
out.push_str(" ");
} else {
if line.link == self.link && self.show_cursor() {
out.push_str("\x1b[97;1m*\x1b[0m")
out.push_str(&color!("*", Bold))
} else {
out.push(' ');
}

Loading…
Cancel
Save