make ProbeHintsBuilders from the environment

Extract the code that builds a `ProbeHintsBuilder` from the environment to a
separate `ProbeHintsBuilder` constructor.  This allows callers to re-use the
environment-based `ProbeHintsBuilder`, but override other aspects of
`ProbeHints`, e.g. to disable mouse handling.
This commit is contained in:
Mark Thomas authored and Wez Furlong committed 2019-05-18 09:12:47 -07:00
1 parent 111f42cf7b
commit a64bf34bb5
1 file changed
+14 -8
+14 -8
View File
@@ -109,6 +109,19 @@ pub struct ProbeHints {
mouse_reporting: Option<bool>,
}
impl ProbeHintsBuilder {
pub fn new_from_env() -> ProbeHintsBuilder {
let mut hints = ProbeHintsBuilder::default();
hints.term(var("TERM").ok());
hints.colorterm(var("COLORTERM").ok());
hints.colorterm_bce(var("COLORTERM_BCE").ok());
hints.term_program(var("TERM_PROGRAM").ok());
hints.term_program_version(var("TERM_PROGRAM_VERSION").ok());
hints.terminfo_db(terminfo::Database::from_env().ok());
hints
}
}
/// Describes the level of color support available
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorLevel {
@@ -150,14 +163,7 @@ impl Capabilities {
/// This function inspects the environment variables to build
/// up configuration hints.
pub fn new_from_env() -> Result<Self, Error> {
let mut hints = ProbeHintsBuilder::default();
hints.term(var("TERM").ok());
hints.colorterm(var("COLORTERM").ok());
hints.colorterm_bce(var("COLORTERM_BCE").ok());
hints.term_program(var("TERM_PROGRAM").ok());
hints.term_program_version(var("TERM_PROGRAM_VERSION").ok());
hints.terminfo_db(terminfo::Database::from_env().ok());
Self::new_with_hints(hints.build().map_err(err_msg)?)
Self::new_with_hints(ProbeHintsBuilder::new_from_env().build().map_err(err_msg)?)
}
/// Build a `Capabilities` object based on the provided `ProbeHints` object.