use super::session::TernBackend;
use crate::backend::*;
pub(super) fn default_command() -> String {
crate::backend::select::TERN_BINARY.to_string()
}
const WINDOW_KEY: &str = "TERN_WINDOW_KEY";
impl TernBackend {
pub(super) fn cli_env(&self) -> BTreeMap<String, String> {
self.env
.iter()
.filter(|(key, _)| key.starts_with("TERN_"))
.map(|(key, value)| (key.clone(), value.clone()))
.collect()
}
pub(super) fn window_args(&self) -> Vec<String> {
match self.env.get(WINDOW_KEY) {
Some(key) if !key.trim().is_empty() => {
vec!["--window".into(), key.trim().to_string()]
}
_ => Vec::new(),
}
}
pub(super) fn json(&self, mut args: Vec<String>) -> Result<Value> {
let window = self.window_args();
if let Some(separator) = args.iter().position(|arg| arg == "--") {
args.splice(separator..separator, window);
} else if !window.is_empty() {
args.extend(window);
}
run_json(
self.runner.as_ref(),
&self.command,
&args,
None,
&self.cli_env(),
)
}
}
pub(super) fn absolute_cwd(cwd: &Path) -> String {
std::path::absolute(cwd)
.unwrap_or_else(|_| cwd.to_path_buf())
.to_string_lossy()
.into_owned()
}
const GONE_BLOCK: &str = "no block is called";
const GONE_SESSION: &str = "no session is called";
pub(super) fn block_is_gone(error: &anyhow::Error) -> bool {
mentions(error, GONE_BLOCK)
}
pub(super) fn session_is_gone(error: &anyhow::Error) -> bool {
mentions(error, GONE_SESSION)
}
fn mentions(error: &anyhow::Error, needle: &str) -> bool {
error.to_string().contains(needle)
}