Select default viewer based on TTY

Previosuly, the plain text viewer was the default option.  But for
interactive use, rich text output would be a more sensible default.
With this patch, we check whether we are called from a TTY to choose the
default viewer:  rich for interactive use, text for non-interactive use.
This commit is contained in:
Robin Krahl 2020-07-16 23:41:49 +02:00
parent 96df0924ba
commit fdabe11c4a
2 changed files with 13 additions and 3 deletions

View File

@ -24,8 +24,8 @@ struct Opt {
source_paths: Vec<String>,
/// The viewer for the rustdoc documentation
#[structopt(long, default_value = "text", parse(try_from_str = viewer::get_viewer))]
viewer: Box<dyn viewer::Viewer>,
#[structopt(long, parse(try_from_str = viewer::get_viewer))]
viewer: Option<Box<dyn viewer::Viewer>>,
/// Do not search the default documentation sources
///
@ -40,7 +40,8 @@ fn main() -> anyhow::Result<()> {
let sources = load_sources(&opt.source_paths, !opt.no_default_sources)?;
let doc = find_doc(&sources, &opt.keyword)?;
opt.viewer.open(&doc)
let viewer = opt.viewer.unwrap_or_else(viewer::get_default);
viewer.open(&doc)
}
const DEFAULT_SOURCES: &[&str] = &[

View File

@ -5,6 +5,7 @@ mod rich;
mod text;
use std::fmt;
use std::io;
use crate::doc;
@ -19,3 +20,11 @@ pub fn get_viewer(s: &str) -> anyhow::Result<Box<dyn Viewer>> {
_ => Err(anyhow::anyhow!("The viewer {} is not supported", s)),
}
}
pub fn get_default() -> Box<dyn Viewer> {
if termion::is_tty(&io::stdout()) {
Box::new(rich::RichViewer::new())
} else {
Box::new(text::TextViewer::new())
}
}