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> {
|
pub fn zo_maxage() -> Result<Rank> {
|
||||||
match env::var_os("_ZO_MAXAGE") {
|
match env::var_os("_ZO_MAXAGE") {
|
||||||
Some(maxage_osstr) => match maxage_osstr.to_str() {
|
Some(maxage_osstr) => {
|
||||||
Some(maxage_str) => {
|
let maxage_str = maxage_osstr.to_str().context("invalid utf-8 sequence 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)
|
||||||
})?;
|
})?;
|
||||||
|
Ok(maxage as Rank)
|
||||||
Ok(maxage as Rank)
|
|
||||||
}
|
|
||||||
None => bail!("invalid utf-8 sequence in _ZO_MAXAGE"),
|
|
||||||
},
|
},
|
||||||
None => Ok(1000.0),
|
None => Ok(1000.0),
|
||||||
}
|
}
|
||||||
|
|
|
@ -194,6 +194,7 @@ impl Dir {
|
||||||
.and_then(|query_last| Path::new(query_last).file_name())
|
.and_then(|query_last| Path::new(query_last).file_name())
|
||||||
{
|
{
|
||||||
if let Some(dir_name) = Path::new(&path_lower).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
|
// unwrap is safe here because we've already handled invalid UTF-8
|
||||||
let dir_name_str = dir_name.to_str().unwrap();
|
let dir_name_str = dir_name.to_str().unwrap();
|
||||||
let query_name_str = query_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>> {
|
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();
|
let stdin = self.child.stdin.as_mut().unwrap();
|
||||||
|
|
||||||
self.lines.sort_unstable_by(|line1, line2| line2.cmp(line1));
|
self.lines.sort_unstable_by(|line1, line2| line2.cmp(line1));
|
||||||
|
|
|
@ -6,11 +6,12 @@ use anyhow::{Context, Result};
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
#[derive(Debug, StructOpt)]
|
#[derive(Debug, StructOpt)]
|
||||||
#[structopt(about = "Add a new directory or increment its rank")]
|
#[structopt(about = "Add a new directory or increment its rank")]
|
||||||
pub struct Add {
|
pub struct Add {
|
||||||
path: Option<String>,
|
path: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Add {
|
impl Add {
|
||||||
|
@ -20,36 +21,37 @@ impl Add {
|
||||||
Some(path) => path,
|
Some(path) => path,
|
||||||
None => {
|
None => {
|
||||||
current_dir = env::current_dir().context("unable to fetch current directory")?;
|
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<()> {
|
fn add<P: AsRef<Path>>(path: P) -> Result<()> {
|
||||||
let path_abs = dunce::canonicalize(path)
|
let path = path.as_ref();
|
||||||
.with_context(|| format!("could not resolve directory: {}", path))?;
|
let path = dunce::canonicalize(path)
|
||||||
|
.with_context(|| format!("could not resolve directory: {}", path.display()))?;
|
||||||
|
|
||||||
let exclude_dirs = config::zo_exclude_dirs();
|
let exclude_dirs = config::zo_exclude_dirs();
|
||||||
if exclude_dirs
|
if exclude_dirs
|
||||||
.iter()
|
.iter()
|
||||||
.any(|excluded_path| excluded_path == &path_abs)
|
.any(|excluded_path| excluded_path == &path)
|
||||||
{
|
{
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let path_abs_str = path_to_str(&path_abs)?;
|
let path_str = path_to_str(&path)?;
|
||||||
|
|
||||||
let mut db = get_db()?;
|
let mut db = get_db()?;
|
||||||
let now = get_current_time()?;
|
let now = get_current_time()?;
|
||||||
|
|
||||||
let maxage = config::zo_maxage()?;
|
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 {
|
None => db.dirs.push(Dir {
|
||||||
path: path_abs_str.to_string(),
|
path: path_str.to_string(),
|
||||||
last_accessed: now,
|
last_accessed: now,
|
||||||
rank: 1.0,
|
rank: 1.0,
|
||||||
}),
|
}),
|
||||||
|
|
|
@ -67,19 +67,19 @@ fn import_line(db: &mut Db, line: &str) -> Result<()> {
|
||||||
.parse::<f64>()
|
.parse::<f64>()
|
||||||
.with_context(|| format!("invalid rank: {}", rank_str))?;
|
.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))?;
|
.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
|
// 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.
|
// 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.rank += rank;
|
||||||
dir.last_accessed = epoch.max(dir.last_accessed);
|
dir.last_accessed = epoch.max(dir.last_accessed);
|
||||||
} else {
|
} else {
|
||||||
db.dirs.push(Dir {
|
db.dirs.push(Dir {
|
||||||
path: path_abs_str.to_string(),
|
path: path_str.to_string(),
|
||||||
rank,
|
rank,
|
||||||
last_accessed: epoch,
|
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()?;
|
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.dirs.swap_remove(idx);
|
||||||
db.modified = true;
|
db.modified = true;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let path_abs =
|
let path_abs = dunce::canonicalize(path_str)
|
||||||
dunce::canonicalize(path).with_context(|| format!("could not resolve path: {}", path))?;
|
.with_context(|| format!("could not resolve path: {}", path_str))?;
|
||||||
let path_abs_str = path_to_str(&path_abs)?;
|
let path_abs_str = path_to_str(&path_abs)?;
|
||||||
|
|
||||||
if let Some(idx) = db.dirs.iter().position(|dir| dir.path == path_abs_str) {
|
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(());
|
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<()> {
|
fn remove_interactive(keywords: &[String]) -> Result<()> {
|
||||||
|
|
Loading…
Reference in New Issue