Fix alfred commands (#394)

This commit is contained in:
Denis Isidoro 2020-08-29 16:49:35 -03:00 committed by GitHub
parent 87eca1a0d0
commit d79f181b4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,7 @@
use crate::common::shell::BashSpawnError;
use crate::display;
use crate::fetcher::Fetcher;
use crate::filesystem;
use crate::structures::cheat::Suggestion;
use crate::structures::config::Config;
use anyhow::Context;
@ -7,15 +9,17 @@ use anyhow::Error;
use std::env;
use std::process::{Command, Stdio};
pub fn main(_config: Config) -> Result<(), Error> {
pub fn main(config: Config) -> Result<(), Error> {
let mut child = Command::new("cat").stdin(Stdio::piped()).spawn().context("Unable to create child")?;
let _stdin = child.stdin.as_mut().context("Unable to get stdin")?;
let _writer = display::alfred::Writer::new();
let stdin = child.stdin.as_mut().context("Unable to get stdin")?;
let mut writer = display::alfred::Writer::new();
display::alfred::print_items_start(None);
// filesystem::read_all(&config, stdin, &mut writer)
// .context("Failed to parse variables intended for finder")?;
let fetcher = filesystem::Fetcher::new(config.path);
fetcher
.fetch(stdin, &mut writer)
.context("Failed to parse variables intended for finder")?;
// make sure everything was printed to stdout before attempting to close the items vector
let _ = child.wait_with_output().context("Failed to wait for fzf")?;
@ -40,25 +44,28 @@ fn prompt_with_suggestions(suggestion: &Suggestion) -> Result<String, Error> {
Ok(suggestions)
}
pub fn suggestions(_config: Config, dry_run: bool) -> Result<(), Error> {
pub fn suggestions(config: Config, dry_run: bool) -> Result<(), Error> {
let mut child = Command::new("cat")
.stdin(Stdio::piped())
.stdout(Stdio::null())
.spawn()
.context("Unable to create child")?;
let _stdin = child.stdin.as_mut().context("Unable to get stdin")?;
let stdin = child.stdin.as_mut().context("Unable to get stdin")?;
let mut writer = display::alfred::Writer::new();
// let variables = filesystem::read_all(&config, stdin, &mut writer).context("Failed to parse variables intended for finder")?;
let fetcher = filesystem::Fetcher::new(config.path);
let variables = fetcher
.fetch(stdin, &mut writer)
.context("Failed to parse variables intended for finder")?
.expect("Empty variable map");
let _tags = env::var("tags").context(r#"The env var "tags" isn't set"#)?;
let tags = env::var("tags").context(r#"The env var "tags" isn't set"#)?;
let snippet = env::var("snippet").context(r#"The env var "snippet" isn't set"#)?;
let capture = display::VAR_REGEX.captures_iter(&snippet).next();
let bracketed_varname = &(capture.expect("Invalid capture"))[0];
let varname = &bracketed_varname[1..bracketed_varname.len() - 1];
// let command = variables.get_suggestion(&tags, &varname);
let command = None;
let command = variables.get_suggestion(&tags, &varname);
if dry_run {
if command.is_none() {