Skip to content

Commit

Permalink
add hyperlink support to nh search
Browse files Browse the repository at this point in the history
  • Loading branch information
viperML committed Mar 13, 2024
1 parent 3f148b0 commit 216ed4f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 13 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ serde = { version = "1.0.166", features = [
] }
serde_json = "1.0.100"
subprocess = "0.2"
supports-hyperlinks = "3.0.0"
tempfile = "3.5.0"
textwrap = { version = "0.16.0", features = ["terminal_size"] }
thiserror = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion src/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl NHRunnable for interface::CleanMode {
}
debug!("Scanning XDG profiles for users 0, 1000-1100");
for user in unsafe { uzers::all_users() } {
if user.uid() >= 1000 && user.uid() < 1100 || user.uid() == 0 {
if user.uid() >= 1000 && user.uid() < 1100 || user.uid() == 0 {
debug!(?user, "Adding XDG profiles for user");
profiles.extend(profiles_in_dir(
user.home_dir().join(".local/state/nix/profiles"),
Expand Down
4 changes: 0 additions & 4 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ pub struct SearchArgs {
/// Number of search results to display
pub limit: u64,

#[arg(long, short = 'L')]
/// Display more information about each result
pub long: bool,

#[arg(long, short, default_value = "nixos-unstable")]
/// Name of the channel to query (e.g nixos-23.11, nixos-unstable)
pub channel: String,
Expand Down
60 changes: 52 additions & 8 deletions src/search.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::*;
use color_eyre::eyre::Context;
use color_eyre::eyre::{Context, ContextCompat};
use interface::SearchArgs;

use std::time::Instant;
use std::{process::Stdio, time::Instant};
use tracing::{debug, trace};

use elasticsearch_dsl::*;
Expand Down Expand Up @@ -31,10 +31,28 @@ struct SearchResult {
package_position: Option<String>,
}

macro_rules! print_hyperlink {
($text:expr, $link:expr) => {
print!("\x1b]8;;{}\x07", $link);
print!("{}", $text.underline());
println!("\x1b]8;;\x07");
};
}

impl NHRunnable for SearchArgs {
fn run(&self) -> Result<()> {
trace!("args: {self:?}");

let nixpkgs_path = std::thread::spawn(|| {
std::process::Command::new("nix")
.stderr(Stdio::inherit())
.args(["eval", "nixpkgs#path"])
.output()
});

// let mut nixpkgs_path = std::process::Command::new("nix")
// .context("Evaluating the nixpkgs path for results positions")?;

let query = Search::new().from(0).size(self.limit).query(
Query::bool().filter(Query::term("type", "package")).must(
Query::dis_max()
Expand Down Expand Up @@ -112,11 +130,24 @@ impl NHRunnable for SearchArgs {
.documents::<SearchResult>()
.context("parsing search document")?;

let hyperlinks = supports_hyperlinks::supports_hyperlinks();
debug!(?hyperlinks);

let nixpkgs_path = String::from_utf8(
nixpkgs_path
.join()
.unwrap()
.context("Evaluating the nixpkgs path location")?
.stdout,
)
.unwrap();

for elem in documents.iter().rev() {
println!();
use owo_colors::OwoColorize;
trace!("{elem:#?}");
print!("{}", elem.package_attr_name.blue(),);

print!("{}", elem.package_attr_name.blue());
let v = &elem.package_pversion;
if !v.is_empty() {
print!(" ({})", v.green());
Expand All @@ -131,13 +162,26 @@ impl NHRunnable for SearchArgs {
}
}

if self.long {
for url in elem.package_homepage.iter() {
println!(" Homepage: {}", url);
for url in elem.package_homepage.iter() {
print!(" Homepage: ");
if hyperlinks {
print_hyperlink!(url, url);
} else {
println!("{}", url);
}
}

if !elem.package_license_set.is_empty() {
println!(" License: {}", elem.package_license_set.join(", "));
if let Some(position) = &elem.package_position {
print!(" Position: ");
if hyperlinks {
let postion_trimmed = position
.split(':')
.next()
.expect("Removing line number from position");

print_hyperlink!(position, format!("file://{nixpkgs_path}/{postion_trimmed}"));
} else {
println!("{}", position);
}
}
}
Expand Down

0 comments on commit 216ed4f

Please sign in to comment.