Fix exit codes in z and fzf

This commit is contained in:
Ajeet D'Souza 2020-03-17 12:34:53 +05:30
parent 146e5709fb
commit 03e8cd47a2
5 changed files with 87 additions and 61 deletions

View File

@ -23,7 +23,7 @@ pub fn main() -> Result<()> {
let opt = Zoxide::from_args();
let env = envy::prefixed("_ZO_")
.from_env::<Env>()
.with_context(|| "Could not parse environment variables")?;
.with_context(|| "could not parse environment variables")?;
match opt {
Zoxide::Add(add) => add.run(&env)?,

View File

@ -108,29 +108,27 @@ struct HookConfig {
const BASH_Z: &str = r#"
_z_cd() {
cd "${@}" > /dev/null
cd "$@" || return "$?"
if [ "${?}" -eq 0 ]; then
if [ -n "$_ZO_ECHO" ]; then
echo "${PWD}"
fi
echo "$PWD"
fi
}
z() {
if [ "${#}" -eq 0 ]; then
_z_cd "${HOME}"
elif [ "${#}" -eq 1 ] && [ "${1}" = '-' ]; then
_z_cd '-'
if [ "$#" -eq 0 ]; then
_z_cd ~ || return "$?"
elif [ "$#" -eq 1 ] && [ "$1" = '-' ]; then
_z_cd ~- || return "$?"
else
local result=$(zoxide query "${@}")
case "${result}" in
result="$(zoxide query $@)" || return "$?"
case "$result" in
"query: "*)
_z_cd "${result:7}"
_z_cd "${result:7}" || return "$?"
;;
*)
if [ -n "${result}" ]; then
echo "${result}"
if [ -n "$result" ]; then
echo "$result"
fi
;;
esac
@ -140,23 +138,27 @@ z() {
const FISH_Z: &str = r#"
function _z_cd
cd "$argv" > /dev/null
cd "$argv"
or return "$status"
if [ "$status" -eq 0 ]
commandline -f repaint
if [ -n "$_ZO_ECHO" ]
echo "$PWD"
end
end
end
function z
set -l argc (count "$argv")
set argc (count "$argv")
if [ "$argc" -eq 0 ]
_z_cd "$HOME"
else if [ "$argc" -eq 1 ]
and [ "$argv[1]" = '-' ]
or return "$status"
else if [ "$argc" -eq 1 ]; and [ "$argv[1]" = '-' ]
_z_cd '-'
or return "$status"
else
# TODO: use string-collect from fish 3.1.0 once it has wider adoption
set -l IFS ''
@ -165,6 +167,7 @@ function z
switch "$result"
case 'query: *'
_z_cd (string sub -s 8 "$result")
or return "$status"
case '*'
if [ -n "$result" ]
echo "$result"

View File

@ -1,6 +1,6 @@
use crate::env::Env;
use crate::util;
use anyhow::Result;
use anyhow::{bail, Result};
use std::path::Path;
use structopt::StructOpt;
@ -15,14 +15,15 @@ pub struct Query {
impl Query {
pub fn run(mut self, env: &Env) -> Result<()> {
let path_opt = if self.interactive {
self.query_interactive(env)
self.query_interactive(env)?
} else {
self.query(env)
}?;
self.query(env)?
};
if let Some(path) = path_opt {
println!("query: {}", path.trim());
}
match path_opt {
Some(path) => println!("query: {}", path.trim()),
None => bail!("no match found"),
};
Ok(())
}
@ -41,11 +42,11 @@ impl Query {
let now = util::get_current_time()?;
if let Some(dir) = util::get_db(env)?.query(&self.keywords, now) {
return Ok(Some(dir.path));
}
Ok(Some(dir.path))
} else {
Ok(None)
}
}
fn query_interactive(&mut self, env: &Env) -> Result<Option<String>> {
let now = util::get_current_time()?;

View File

@ -2,7 +2,7 @@ use crate::db::DB;
use crate::dir::Dir;
use crate::env::Env;
use crate::types::Epoch;
use anyhow::{anyhow, Context, Result};
use anyhow::{anyhow, bail, Context, Result};
use std::io::{Read, Write};
use std::process::{Command, Stdio};
use std::time::SystemTime;
@ -25,15 +25,16 @@ pub fn get_current_time() -> Result<Epoch> {
}
pub fn fzf_helper(now: Epoch, mut dirs: Vec<Dir>) -> Result<Option<String>> {
let fzf = Command::new("fzf")
let mut fzf = Command::new("fzf")
.arg("-n2..")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.with_context(|| anyhow!("could not launch fzf"))?;
let mut fzf_stdin = fzf
let fzf_stdin = fzf
.stdin
.as_mut()
.ok_or_else(|| anyhow!("could not connect to fzf stdin"))?;
for dir in dirs.iter_mut() {
@ -56,8 +57,9 @@ pub fn fzf_helper(now: Epoch, mut dirs: Vec<Dir>) -> Result<Option<String>> {
.with_context(|| anyhow!("could not write into fzf stdin"))?;
}
let mut fzf_stdout = fzf
let fzf_stdout = fzf
.stdout
.as_mut()
.ok_or_else(|| anyhow!("could not connect to fzf stdout"))?;
let mut output = String::new();
@ -65,5 +67,25 @@ pub fn fzf_helper(now: Epoch, mut dirs: Vec<Dir>) -> Result<Option<String>> {
.read_to_string(&mut output)
.with_context(|| anyhow!("could not read from fzf stdout"))?;
Ok(output.get(12..).map(str::to_string))
let status = fzf.wait().with_context(|| "could not wait on fzf")?;
match status.code() {
// normal exit
Some(0) => match output.get(12..) {
Some(path) => Ok(Some(path.to_string())),
None => bail!("fzf returned invalid output"),
},
// no match
Some(1) => Ok(None),
// error
Some(2) => bail!("fzf returned an error"),
// terminated by a signal
Some(128..=254) | None => bail!("fzf was terminated"),
// unknown
_ => bail!("fzf returned an unknown error"),
}
}