Skip to content

Commit

Permalink
feat: sort search results by full URN (#2164)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi authored Jan 6, 2025
1 parent 05ecbca commit 21f6a92
Show file tree
Hide file tree
Showing 21 changed files with 408 additions and 328 deletions.
43 changes: 10 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion yazi-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ anyhow = { workspace = true }
clap = { workspace = true }
crossterm = { workspace = true }
md-5 = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
toml_edit = "0.22.22"
toml = { version = "0.8.19" }

[build-dependencies]
yazi-shared = { path = "../yazi-shared", version = "0.4.3" }
Expand Down
11 changes: 4 additions & 7 deletions yazi-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,13 @@ async fn run() -> anyhow::Result<()> {
Command::Pack(cmd) => {
package::init()?;
if cmd.install {
package::Package::install_from_config("plugin", false).await?;
package::Package::install_from_config("flavor", false).await?;
package::Package::load().await?.install(false).await?;
} else if cmd.list {
package::Package::list_from_config("plugin").await?;
package::Package::list_from_config("flavor").await?;
package::Package::load().await?.print()?;
} else if cmd.upgrade {
package::Package::install_from_config("plugin", true).await?;
package::Package::install_from_config("flavor", true).await?;
package::Package::load().await?.install(true).await?;
} else if let Some(repo) = cmd.add {
package::Package::add_to_config(&repo).await?;
package::Package::load().await?.add(&repo).await?;
}
}

Expand Down
4 changes: 2 additions & 2 deletions yazi-cli/src/package/add.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::Result;
use yazi_fs::must_exists;

use super::{Git, Package};
use super::{Dependency, Git};

impl Package {
impl Dependency {
pub(super) async fn add(&mut self) -> Result<()> {
self.header("Upgrading package `{name}`")?;

Expand Down
113 changes: 113 additions & 0 deletions yazi-cli/src/package/dependency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use std::{borrow::Cow, io::BufWriter, path::PathBuf};

use anyhow::Result;
use md5::{Digest, Md5};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use yazi_fs::Xdg;

pub(crate) struct Dependency {
pub(crate) repo: String,
pub(crate) child: String,
pub(crate) rev: String,
pub(super) is_flavor: bool,
}

impl Dependency {
pub(super) fn new(url: &str, rev: Option<&str>) -> Self {
let mut parts = url.splitn(2, ':');

let mut repo = parts.next().unwrap_or_default().to_owned();
let child = if let Some(s) = parts.next() {
format!("{s}.yazi")
} else {
repo.push_str(".yazi");
String::new()
};

Self { repo, child, rev: rev.unwrap_or_default().to_owned(), is_flavor: false }
}

#[inline]
pub(super) fn use_(&self) -> Cow<str> {
if self.child.is_empty() {
self.repo.trim_end_matches(".yazi").into()
} else {
format!("{}:{}", self.repo, self.child.trim_end_matches(".yazi")).into()
}
}

#[inline]
pub(super) fn name(&self) -> Option<&str> {
let s = if self.child.is_empty() {
self.repo.split('/').last().filter(|s| !s.is_empty())
} else {
Some(self.child.as_str())
};

s.filter(|s| s.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'z' | b'-' | b'.')))
}

#[inline]
pub(super) fn local(&self) -> PathBuf {
Xdg::state_dir()
.join("packages")
.join(format!("{:x}", Md5::new_with_prefix(self.remote()).finalize()))
}

#[inline]
pub(super) fn remote(&self) -> String {
// Support more Git hosting services in the future
format!("https://github.com/{}.git", self.repo)
}

pub(super) fn header(&self, s: &str) -> Result<()> {
use crossterm::style::{Attribute, Print, SetAttributes};

crossterm::execute!(
BufWriter::new(std::io::stdout()),
Print("\n"),
SetAttributes(Attribute::Reverse.into()),
SetAttributes(Attribute::Bold.into()),
Print(" "),
Print(s.replacen("{name}", self.name().unwrap_or_default(), 1)),
Print(" "),
SetAttributes(Attribute::Reset.into()),
Print("\n\n"),
)?;
Ok(())
}
}

impl<'de> Deserialize<'de> for Dependency {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Shadow {
#[serde(rename = "use")]
use_: String,
rev: Option<String>,
}

let outer = Shadow::deserialize(deserializer)?;
Ok(Self::new(&outer.use_, outer.rev.as_deref()))
}
}

impl Serialize for Dependency {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
#[derive(Serialize)]
struct Shadow<'a> {
#[serde(rename = "use")]
use_: Cow<'a, str>,
rev: Option<&'a String>,
}

Shadow { use_: self.use_(), rev: Some(&self.rev).filter(|&s| !s.is_empty()) }
.serialize(serializer)
}
}
5 changes: 3 additions & 2 deletions yazi-cli/src/package/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use tokio::fs;
use yazi_fs::{Xdg, maybe_exists, must_exists, remove_dir_clean};
use yazi_macro::outln;

use super::Package;
use super::Dependency;

const TRACKER: &str = "DO_NOT_MODIFY_ANYTHING_IN_THIS_DIRECTORY";

impl Package {
impl Dependency {
pub(super) async fn deploy(&mut self) -> Result<()> {
let Some(name) = self.name().map(ToOwned::to_owned) else { bail!("Invalid package url") };
let from = self.local().join(&self.child);
Expand All @@ -36,6 +36,7 @@ For safety, please manually delete it from your plugin/flavor directory and re-r
let files = if self.is_flavor {
&["flavor.toml", "tmtheme.xml", "README.md", "preview.png", "LICENSE", "LICENSE-tmtheme"][..]
} else {
// TODO: init.lua
&["init.lua", "README.md", "LICENSE"][..]
};

Expand Down
4 changes: 2 additions & 2 deletions yazi-cli/src/package/install.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::Result;
use yazi_fs::must_exists;

use super::{Git, Package};
use super::{Dependency, Git};

impl Package {
impl Dependency {
pub(super) async fn install(&mut self) -> Result<()> {
self.header("Installing package `{name}`")?;

Expand Down
2 changes: 1 addition & 1 deletion yazi-cli/src/package/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::module_inception)]

yazi_macro::mod_flat!(add deploy git install package parser upgrade);
yazi_macro::mod_flat!(add dependency deploy git install package upgrade);

use anyhow::Context;
use yazi_fs::Xdg;
Expand Down
Loading

0 comments on commit 21f6a92

Please sign in to comment.