This commit is contained in:
Al Farhaan khan I Inamdar 2026-05-23 11:45:43 +08:00 committed by GitHub
commit 43d31eb876
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 2 deletions

View File

@ -8,6 +8,17 @@ use crate::db::{Database, Epoch, Stream, StreamOptions};
use crate::error::BrokenPipeHandler;
use crate::util::{self, Fzf, FzfChild};
fn expand_tilde(path: &str) -> String {
if let Some(stripped) = path.strip_prefix('~') {
if let Some(home) = dirs::home_dir() {
if let Some(home_str) = home.to_str() {
return format!("{}{}", home_str, stripped);
}
}
}
path.to_string()
}
impl Run for Query {
fn run(&self) -> Result<()> {
let mut db = crate::db::Database::open()?;
@ -47,7 +58,8 @@ impl Query {
print!("{selection}");
} else {
let path = selection.get(7..).context("could not read selection from fzf")?;
print!("{path}");
let expanded_path = expand_tilde(path);
print!("{expanded_path}");
}
Ok(())
}

View File

@ -5,6 +5,21 @@ use serde::{Deserialize, Serialize};
use crate::util::{DAY, HOUR, WEEK};
fn path_with_tilde(path: &str) -> Cow<'_, str> {
if let Some(home) = dirs::home_dir() {
if let Some(home_str) = home.to_str() {
if let Some(suffix) = path.strip_prefix(home_str) {
if suffix.is_empty() {
return Cow::Borrowed("~");
} else if suffix.starts_with('/') || suffix.starts_with('\\') {
return Cow::Owned(format!("~{}", suffix));
}
}
}
}
Cow::Borrowed(path)
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Dir<'a> {
#[serde(borrow)]
@ -61,7 +76,7 @@ impl Display for DirDisplay<'_> {
let score = self.dir.score(now).clamp(0.0, 9999.0);
write!(f, "{score:>6.1}{}", self.separator)?;
}
write!(f, "{}", self.dir.path)
write!(f, "{}", path_with_tilde(&self.dir.path))
}
}