Skip to content

Commit

Permalink
feat(hok|cat,home): support candidate selection
Browse files Browse the repository at this point in the history
Signed-off-by: Chawye Hsu <[email protected]>
  • Loading branch information
chawyehsu committed Jul 25, 2023
1 parent 53b1744 commit 28b56c5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 34 deletions.
81 changes: 51 additions & 30 deletions src/cmd/cat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::ArgMatches;
use libscoop::{operation, QueryOption, Session};
use std::{path::Path, process::Command};
use std::{io::Write, path::Path, process::Command};

use crate::Result;

Expand All @@ -10,44 +10,65 @@ pub fn cmd_cat(matches: &ArgMatches, session: &Session) -> Result<()> {
let options = vec![QueryOption::Explicit];
let result = operation::package_query(session, queries, options, false)?;

match result.len() {
0 => eprintln!("Could not find package named '{}'.", query),
1 => {
let package = &result[0];
let cat = match is_program_available("bat.exe") {
true => "bat.exe",
false => "type",
};
let config = session.config();
let cat_args = match cat == "bat.exe" {
false => vec![],
true => {
let cat_style = config.cat_style();
vec!["--no-paging", "--style", cat_style, "--language", "json"]
}
};

let mut child = Command::new("cmd")
.arg("/C")
.arg(cat)
.arg(package.manfest_path())
.args(cat_args)
.spawn()?;
child.wait()?;
}
_ => {
eprintln!("Found multiple packages named '{}':\n", query);
if result.is_empty() {
eprintln!("Could not find package named '{}'.", query)
} else {
let length = result.len();
let package = if length == 1 {
&result[0]
} else {
println!("Found multiple packages named '{}':\n", query);
for (idx, pkg) in result.iter().enumerate() {
println!(
" {}. {}/{} ({})",
idx + 1,
idx,
pkg.bucket(),
pkg.name(),
pkg.homepage()
);
}
eprintln!("\nUse bucket prefix to narrow results.");
print!("\nPlease select one, enter the number to continue: ");
std::io::stdout().flush().unwrap();
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
let parsed = input.trim().parse::<usize>();
if parsed.is_err() {
eprintln!("Invalid input.");
return Ok(());
}

let num = parsed.unwrap();
if num >= result.len() {
eprintln!("Invalid input.");
return Ok(());
}
&result[num]
};

let cat = match is_program_available("bat.exe") {
true => "bat.exe",
false => "type",
};
let config = session.config();
let cat_args = match cat == "bat.exe" {
false => vec![],
true => {
let cat_style = config.cat_style();
vec!["--no-paging", "--style", cat_style, "--language", "json"]
}
};

if length > 1 {
println!();
}

let mut child = Command::new("cmd")
.arg("/C")
.arg(cat)
.arg(package.manfest_path())
.args(cat_args)
.spawn()?;
child.wait()?;
}
}
Ok(())
Expand Down
25 changes: 21 additions & 4 deletions src/cmd/home.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::ArgMatches;
use libscoop::{operation, QueryOption, Session};
use std::process::Command;
use std::{io::Write, process::Command};

use crate::Result;

Expand All @@ -22,17 +22,34 @@ pub fn cmd_home(matches: &ArgMatches, session: &Session) -> Result<()> {
.spawn()?;
}
_ => {
eprintln!("Found multiple packages named '{}':\n", query);
println!("Found multiple packages named '{}':\n", query);
for (idx, pkg) in result.iter().enumerate() {
println!(
" {}. {}/{} ({})",
idx + 1,
idx,
pkg.bucket(),
pkg.name(),
pkg.homepage()
);
}
eprintln!("\nUse bucket prefix to narrow results.");
print!("\nPlease select one, enter the number to continue: ");
std::io::stdout().flush().unwrap();
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
let parsed = input.trim().parse::<usize>();
if let Ok(num) = parsed {
if num < result.len() {
let package = &result[num];
let url = std::ffi::OsStr::new(package.homepage());
Command::new("cmd")
.arg("/C")
.arg("start")
.arg(url)
.spawn()?;
return Ok(());
}
}
eprintln!("Invalid input.");
}
}
}
Expand Down

0 comments on commit 28b56c5

Please sign in to comment.