Avoid long variable names
This commit is contained in:
parent
dad0f60b28
commit
edf3c68a7c
|
@ -34,15 +34,12 @@ pub fn zo_exclude_dirs() -> Vec<PathBuf> {
|
|||
|
||||
pub fn zo_maxage() -> Result<Rank> {
|
||||
match env::var_os("_ZO_MAXAGE") {
|
||||
Some(maxage_osstr) => match maxage_osstr.to_str() {
|
||||
Some(maxage_str) => {
|
||||
Some(maxage_osstr) => {
|
||||
let maxage_str = maxage_osstr.to_str().context("invalid utf-8 sequence in _ZO_MAXAGE")?;
|
||||
let maxage = maxage_str.parse::<u64>().with_context(|| {
|
||||
format!("unable to parse _ZO_MAXAGE as integer: {}", maxage_str)
|
||||
})?;
|
||||
|
||||
Ok(maxage as Rank)
|
||||
}
|
||||
None => bail!("invalid utf-8 sequence in _ZO_MAXAGE"),
|
||||
},
|
||||
None => Ok(1000.0),
|
||||
}
|
||||
|
|
|
@ -194,6 +194,7 @@ impl Dir {
|
|||
.and_then(|query_last| Path::new(query_last).file_name())
|
||||
{
|
||||
if let Some(dir_name) = Path::new(&path_lower).file_name() {
|
||||
// <https://github.com/rust-lang/rust/issues/49802>
|
||||
// unwrap is safe here because we've already handled invalid UTF-8
|
||||
let dir_name_str = dir_name.to_str().unwrap();
|
||||
let query_name_str = query_name.to_str().unwrap();
|
||||
|
|
|
@ -43,7 +43,7 @@ impl Fzf {
|
|||
}
|
||||
|
||||
pub fn wait_selection(mut self) -> Result<Option<String>> {
|
||||
// unwrap() here is safe since we have captured `stdin`
|
||||
// unwrap() is safe here since we have captured `stdin`
|
||||
let stdin = self.child.stdin.as_mut().unwrap();
|
||||
|
||||
self.lines.sort_unstable_by(|line1, line2| line2.cmp(line1));
|
||||
|
|
|
@ -6,11 +6,12 @@ use anyhow::{Context, Result};
|
|||
use structopt::StructOpt;
|
||||
|
||||
use std::env;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
#[structopt(about = "Add a new directory or increment its rank")]
|
||||
pub struct Add {
|
||||
path: Option<String>,
|
||||
path: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl Add {
|
||||
|
@ -20,36 +21,37 @@ impl Add {
|
|||
Some(path) => path,
|
||||
None => {
|
||||
current_dir = env::current_dir().context("unable to fetch current directory")?;
|
||||
path_to_str(¤t_dir)?
|
||||
¤t_dir
|
||||
}
|
||||
};
|
||||
|
||||
add(path)
|
||||
add(&path)
|
||||
}
|
||||
}
|
||||
|
||||
fn add(path: &str) -> Result<()> {
|
||||
let path_abs = dunce::canonicalize(path)
|
||||
.with_context(|| format!("could not resolve directory: {}", path))?;
|
||||
fn add<P: AsRef<Path>>(path: P) -> Result<()> {
|
||||
let path = path.as_ref();
|
||||
let path = dunce::canonicalize(path)
|
||||
.with_context(|| format!("could not resolve directory: {}", path.display()))?;
|
||||
|
||||
let exclude_dirs = config::zo_exclude_dirs();
|
||||
if exclude_dirs
|
||||
.iter()
|
||||
.any(|excluded_path| excluded_path == &path_abs)
|
||||
.any(|excluded_path| excluded_path == &path)
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let path_abs_str = path_to_str(&path_abs)?;
|
||||
let path_str = path_to_str(&path)?;
|
||||
|
||||
let mut db = get_db()?;
|
||||
let now = get_current_time()?;
|
||||
|
||||
let maxage = config::zo_maxage()?;
|
||||
|
||||
match db.dirs.iter_mut().find(|dir| dir.path == path_abs_str) {
|
||||
match db.dirs.iter_mut().find(|dir| dir.path == path_str) {
|
||||
None => db.dirs.push(Dir {
|
||||
path: path_abs_str.to_string(),
|
||||
path: path_str.to_string(),
|
||||
last_accessed: now,
|
||||
rank: 1.0,
|
||||
}),
|
||||
|
|
|
@ -67,19 +67,19 @@ fn import_line(db: &mut Db, line: &str) -> Result<()> {
|
|||
.parse::<f64>()
|
||||
.with_context(|| format!("invalid rank: {}", rank_str))?;
|
||||
|
||||
let path_abs = dunce::canonicalize(path_str)
|
||||
let path = dunce::canonicalize(path_str)
|
||||
.with_context(|| format!("could not resolve path: {}", path_str))?;
|
||||
|
||||
let path_abs_str = path_to_str(&path_abs)?;
|
||||
let path_str = path_to_str(&path)?;
|
||||
|
||||
// If the path exists in the database, add the ranks and set the epoch to
|
||||
// the largest of the parsed epoch and the already present epoch.
|
||||
if let Some(dir) = db.dirs.iter_mut().find(|dir| dir.path == path_abs_str) {
|
||||
if let Some(dir) = db.dirs.iter_mut().find(|dir| dir.path == path_str) {
|
||||
dir.rank += rank;
|
||||
dir.last_accessed = epoch.max(dir.last_accessed);
|
||||
} else {
|
||||
db.dirs.push(Dir {
|
||||
path: path_abs_str.to_string(),
|
||||
path: path_str.to_string(),
|
||||
rank,
|
||||
last_accessed: epoch,
|
||||
});
|
||||
|
|
|
@ -33,17 +33,17 @@ impl Remove {
|
|||
}
|
||||
}
|
||||
|
||||
fn remove(path: &str) -> Result<()> {
|
||||
fn remove(path_str: &str) -> Result<()> {
|
||||
let mut db = get_db()?;
|
||||
|
||||
if let Some(idx) = db.dirs.iter().position(|dir| &dir.path == path) {
|
||||
if let Some(idx) = db.dirs.iter().position(|dir| &dir.path == path_str) {
|
||||
db.dirs.swap_remove(idx);
|
||||
db.modified = true;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let path_abs =
|
||||
dunce::canonicalize(path).with_context(|| format!("could not resolve path: {}", path))?;
|
||||
let path_abs = dunce::canonicalize(path_str)
|
||||
.with_context(|| format!("could not resolve path: {}", path_str))?;
|
||||
let path_abs_str = path_to_str(&path_abs)?;
|
||||
|
||||
if let Some(idx) = db.dirs.iter().position(|dir| dir.path == path_abs_str) {
|
||||
|
@ -52,7 +52,7 @@ fn remove(path: &str) -> Result<()> {
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
bail!("could not find path in database: {}", path)
|
||||
bail!("could not find path in database: {}", path_str)
|
||||
}
|
||||
|
||||
fn remove_interactive(keywords: &[String]) -> Result<()> {
|
||||
|
|
Loading…
Reference in New Issue