mirror of
https://github.com/Llewellynvdm/zoxide.git
synced 2025-01-24 07:38:24 +00:00
Improve error handling on Xonsh
This commit is contained in:
parent
3d432105e0
commit
20d62524bf
@ -5,6 +5,10 @@
|
|||||||
import os
|
import os
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
import os.path
|
import os.path
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from subprocess import CalledProcessError
|
||||||
|
|
||||||
{{ SECTION }}
|
{{ SECTION }}
|
||||||
# Utility functions for zoxide.
|
# Utility functions for zoxide.
|
||||||
@ -55,16 +59,35 @@ def __zoxide_z(keywords: [str]):
|
|||||||
elif len(keywords) == 1 and os.path.isdir(keywords[0]):
|
elif len(keywords) == 1 and os.path.isdir(keywords[0]):
|
||||||
__zoxide_cd(keywords[0])
|
__zoxide_cd(keywords[0])
|
||||||
else:
|
else:
|
||||||
__zoxide_result = $(zoxide query -- @(keywords))[:-1]
|
try:
|
||||||
if __zoxide_result:
|
__zoxide_cmd = subprocess.run(["zoxide", "query", "--"] + keywords, check=True, stdout=subprocess.PIPE)
|
||||||
|
except CalledProcessError as e:
|
||||||
|
return e.returncode
|
||||||
|
|
||||||
|
try:
|
||||||
|
__zoxide_result = __zoxide_cmd.stdout[:-1].decode("utf-8")
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
print(f"zoxide: invalid unicode in result: {__zoxide_result}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
__zoxide_cd(__zoxide_result)
|
__zoxide_cd(__zoxide_result)
|
||||||
|
|
||||||
# Jump to a directory using interactive search.
|
# Jump to a directory using interactive search.
|
||||||
def __zoxide_zi(keywords: [str]):
|
def __zoxide_zi(keywords: [str]):
|
||||||
__zoxide_result = $(zoxide query -- @(keywords))[:-1]
|
try:
|
||||||
if __zoxide_result:
|
__zoxide_cmd = subprocess.run(["zoxide", "query", "-i", "--"] + keywords, check=True, stdout=subprocess.PIPE)
|
||||||
|
except CalledProcessError as e:
|
||||||
|
return e.returncode
|
||||||
|
|
||||||
|
try:
|
||||||
|
__zoxide_result = __zoxide_cmd.stdout[:-1].decode("utf-8")
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
print(f"zoxide: invalid unicode in result: {__zoxide_result}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
__zoxide_cd(__zoxide_result)
|
__zoxide_cd(__zoxide_result)
|
||||||
|
|
||||||
|
|
||||||
# Add a new entry to the database.
|
# Add a new entry to the database.
|
||||||
def __zoxide_za(args: [str]):
|
def __zoxide_za(args: [str]):
|
||||||
zoxide add @(args)
|
zoxide add @(args)
|
||||||
@ -83,8 +106,17 @@ def __zoxide_zr(args: [str]):
|
|||||||
|
|
||||||
# Remove an entry from the database using interactive selection.
|
# Remove an entry from the database using interactive selection.
|
||||||
def __zoxide_zri(keywords: [str]):
|
def __zoxide_zri(keywords: [str]):
|
||||||
__zoxide_result = $(zoxide query -- @(keywords))[:-1]
|
try:
|
||||||
if __zoxide_result:
|
__zoxide_cmd = subprocess.run(["zoxide", "query", "--"] + keywords, check=True, stdout=subprocess.PIPE)
|
||||||
|
except CalledProcessError as e:
|
||||||
|
return e.returncode
|
||||||
|
|
||||||
|
try:
|
||||||
|
__zoxide_result = __zoxide_cmd.stdout[:-1].decode("utf-8")
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
print(f"zoxide: invalid unicode in result: {__zoxide_result}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
zoxide remove @(__zoxide_result)
|
zoxide remove @(__zoxide_result)
|
||||||
|
|
||||||
{{ SECTION }}
|
{{ SECTION }}
|
||||||
|
@ -27,7 +27,7 @@ pub fn zo_exclude_dirs() -> Result<Vec<glob::Pattern>> {
|
|||||||
.map(|path| {
|
.map(|path| {
|
||||||
let pattern = path
|
let pattern = path
|
||||||
.to_str()
|
.to_str()
|
||||||
.context("invalid utf-8 sequence in _ZO_EXCLUDE_DIRS")?;
|
.context("invalid unicode in _ZO_EXCLUDE_DIRS")?;
|
||||||
glob::Pattern::new(&pattern)
|
glob::Pattern::new(&pattern)
|
||||||
.with_context(|| format!("invalid glob in _ZO_EXCLUDE_DIRS: {}", pattern))
|
.with_context(|| format!("invalid glob in _ZO_EXCLUDE_DIRS: {}", pattern))
|
||||||
})
|
})
|
||||||
@ -45,7 +45,7 @@ pub fn zo_maxage() -> Result<Rank> {
|
|||||||
Some(maxage_osstr) => {
|
Some(maxage_osstr) => {
|
||||||
let maxage_str = maxage_osstr
|
let maxage_str = maxage_osstr
|
||||||
.to_str()
|
.to_str()
|
||||||
.context("invalid utf-8 sequence in _ZO_MAXAGE")?;
|
.context("invalid unicode in _ZO_MAXAGE")?;
|
||||||
let maxage = maxage_str.parse::<u64>().with_context(|| {
|
let maxage = maxage_str.parse::<u64>().with_context(|| {
|
||||||
format!("unable to parse _ZO_MAXAGE as integer: {}", maxage_str)
|
format!("unable to parse _ZO_MAXAGE as integer: {}", maxage_str)
|
||||||
})?;
|
})?;
|
||||||
|
@ -27,7 +27,7 @@ pub fn current_time() -> Result<Epoch> {
|
|||||||
pub fn path_to_str<P: AsRef<Path>>(path: &P) -> Result<&str> {
|
pub fn path_to_str<P: AsRef<Path>>(path: &P) -> Result<&str> {
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
path.to_str()
|
path.to_str()
|
||||||
.with_context(|| format!("invalid UTF-8 in path: {}", path.display()))
|
.with_context(|| format!("invalid unicode in path: {}", path.display()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolves the absolute version of a path.
|
/// Resolves the absolute version of a path.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user