Skip to content

Commit

Permalink
Add --interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
Morganamilo committed Oct 13, 2023
1 parent 01fa661 commit c55c983
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/command_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ impl Config {
self.mode |= word.parse()?;
}
}
Arg::Long("interactive") => self.interactive = true,
Arg::Long("skipreview") => self.skip_review = true,
Arg::Long("review") => self.skip_review = false,
Arg::Long("gendb") => self.gendb = true,
Expand Down
11 changes: 6 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,10 @@ pub enum Sign {
Key(String),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum Op {
#[default]
Default,
ChrootCtl,
Database,
DepTest,
Expand All @@ -208,7 +210,6 @@ pub enum Op {
Sync,
Upgrade,
Build,
Yay,
}

impl ConfigEnum for Op {
Expand All @@ -225,7 +226,7 @@ impl ConfigEnum for Op {
("sync", Self::Sync),
("upgrade", Self::Upgrade),
("build", Self::Build),
("yay", Self::Yay),
("default", Self::Default),
];
}

Expand Down Expand Up @@ -386,7 +387,6 @@ pub struct Config {

pub cols: Option<usize>,

#[default(Op::Yay)]
pub op: Op,

#[cfg(not(feature = "mock"))]
Expand Down Expand Up @@ -435,6 +435,7 @@ pub struct Config {
#[default(Mode::empty())]
pub mode: Mode,
pub aur_filter: bool,
pub interactive: bool,

#[default = 7]
pub completion_interval: u64,
Expand Down Expand Up @@ -727,7 +728,7 @@ impl Config {

if self.help {
match self.op {
Op::GetPkgBuild | Op::Show | Op::Yay => {
Op::GetPkgBuild | Op::Show | Op::Default => {
help::help();
std::process::exit(0);
}
Expand Down
35 changes: 28 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use std::process::Command;
use ansi_term::Style;
use anyhow::{bail, Error, Result};
use cini::Ini;
use search::{interactive_search, interactive_search_local};
use tr::{tr, tr_init};

#[macro_export]
Expand Down Expand Up @@ -154,13 +155,14 @@ async fn run2<S: AsRef<str>>(config: &mut Config, args: &[S]) -> Result<i32> {
config.parse(Some(name.as_str()), &file)?;
};

log::debug!("{:#?}", config);

if args.is_empty() {
config.parse_args(["-Syu"])?;
} else {
config.parse_args(args)?;
}

log::debug!("{:#?}", config);
handle_cmd(config).await
}

Expand All @@ -181,7 +183,7 @@ async fn handle_cmd(config: &mut Config) -> Result<i32> {
Op::DepTest => handle_test(config).await?,
Op::GetPkgBuild => handle_get_pkg_build(config).await?,
Op::Show => handle_show(config).await?,
Op::Yay => handle_yay(config).await?,
Op::Default => handle_default(config).await?,
Op::RepoCtl => handle_repo(config)?,
Op::ChrootCtl => handle_chroot(config)?,
// _ => bail!("unknown op '{}'", config.op),
Expand Down Expand Up @@ -212,7 +214,13 @@ async fn handle_build(config: &mut Config) -> Result<i32> {

async fn handle_query(config: &mut Config) -> Result<i32> {
let args = &config.args;
if args.has_arg("u", "upgrades") {
if config.interactive {
interactive_search_local(config)?;
for pkg in &config.targets {
println!("{}", pkg);
}
Ok(0)
} else if args.has_arg("u", "upgrades") {
print_upgrade_list(config).await
} else {
Ok(exec::pacman(config, args)?.code())
Expand Down Expand Up @@ -243,7 +251,7 @@ async fn handle_get_pkg_build(config: &mut Config) -> Result<i32> {
}
}

async fn handle_yay(config: &mut Config) -> Result<i32> {
async fn handle_default(config: &mut Config) -> Result<i32> {
if config.gendb {
devel::gendb(config).await?;
Ok(0)
Expand All @@ -262,13 +270,15 @@ async fn handle_yay(config: &mut Config) -> Result<i32> {
Ok(0)
}
} else if !config.targets.is_empty() {
search::search_install(config).await
config.interactive = true;
handle_sync(config).await?;
Ok(0)
} else {
bail!(tr!("no operation specified (use -h for help)"));
}
}

fn handle_remove(config: &Config) -> Result<i32> {
fn handle_remove(config: &mut Config) -> Result<i32> {
remove::remove(config)
}

Expand All @@ -289,13 +299,24 @@ async fn handle_sync(config: &mut Config) -> Result<i32> {
} else if config.args.has_arg("l", "list") {
sync::list(config).await
} else if config.args.has_arg("s", "search") {
search::search(config).await
if config.interactive {
interactive_search(config, false).await?;
for pkg in &config.targets {
println!("{}", pkg);
}
Ok(1)
} else {
search::search(config).await
}
} else if config.args.has_arg("g", "groups")
|| config.args.has_arg("p", "print")
|| config.args.has_arg("p", "print-format")
{
Ok(exec::pacman(config, &config.args)?.code())
} else {
if config.interactive {
search::interactive_search(config, true).await?;
}
let target = std::mem::take(&mut config.targets);
install::install(config, &target).await?;
Ok(0)
Expand Down
7 changes: 6 additions & 1 deletion src/remove.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::devel::{load_devel_info, save_devel_info};
use crate::print_error;
use crate::search::interactive_search_local;
use crate::util::pkg_base_or_name;
use crate::Config;
use crate::{exec, repo};
Expand All @@ -8,7 +9,11 @@ use std::collections::HashMap;

use anyhow::Result;

pub fn remove(config: &Config) -> Result<i32> {
pub fn remove(config: &mut Config) -> Result<i32> {
if config.interactive {
interactive_search_local(config)?;
}

let mut devel_info = load_devel_info(config)?.unwrap_or_default();
let db = config.alpm.localdb();
let bases = config
Expand Down
2 changes: 1 addition & 1 deletion src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn flags(config: &mut Config) -> aur_depends::Flags {
),
YesNoAll::All => flags |= Flags::PROVIDES,
}
if config.op == Op::Yay {
if config.op == Op::Default {
flags.remove(Flags::TARGET_PROVIDES);
}
if config.repos != LocalRepos::None || config.rebuild == YesNoAllTree::Tree || config.chroot {
Expand Down
83 changes: 62 additions & 21 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ use crate::config::SortBy;
use crate::config::{Config, SortMode};
use crate::fmt::{color_repo, print_indent};
use crate::info;
use crate::install::{install, read_repos};
use crate::printtr;
use crate::install::read_repos;
use crate::util::{input, NumberMenu};

use ansi_term::Style;
use anyhow::{ensure, Context, Result};
use anyhow::{bail, ensure, Context, Result};
use aur_depends::Repo;
use indicatif::HumanBytes;
use raur::{Raur, SearchBy};
Expand All @@ -19,7 +18,8 @@ use reqwest::get;
use srcinfo::Srcinfo;
use tr::tr;

enum AnyPkg<'a> {
#[derive(Debug)]
pub enum AnyPkg<'a> {
RepoPkg(alpm::Package<'a>),
AurPkg(&'a raur::Package),
Custom(&'a str, &'a Srcinfo, &'a srcinfo::Package),
Expand All @@ -29,6 +29,7 @@ pub async fn search(config: &Config) -> Result<i32> {
let mut repos = Vec::new();
let mut paths = HashMap::new();
let quiet = config.args.has_arg("q", "quiet");

let repo_pkgs = search_repos(config, &config.targets)?;

let targets = config
Expand Down Expand Up @@ -110,6 +111,23 @@ fn search_custom<'a>(
Ok(ret)
}

fn search_local<'a>(config: &'a Config, targets: &[String]) -> Result<Vec<alpm::Package<'a>>> {
let mut ret = Vec::new();

if targets.is_empty() {
ret.extend(config.alpm.localdb().pkgs());
} else {
let pkgs = config.alpm.localdb().search(targets.iter())?;
ret.extend(pkgs);
};

if config.limit != 0 {
ret.truncate(config.limit);
}

Ok(ret)
}

fn search_repos<'a>(config: &'a Config, targets: &[String]) -> Result<Vec<alpm::Package<'a>>> {
if targets.is_empty() || !config.mode.repo() {
return Ok(Vec::new());
Expand Down Expand Up @@ -381,7 +399,23 @@ fn print_alpm_pkg(config: &Config, pkg: &alpm::Package, quiet: bool) {
}
}

pub async fn search_install(config: &mut Config) -> Result<i32> {
pub fn interactive_search_local(config: &mut Config) -> Result<()> {
let paths = HashMap::new();
let mut all_pkgs = Vec::new();

let repo_pkgs = search_local(config, &config.targets)?;

for pkg in repo_pkgs {
all_pkgs.push(AnyPkg::RepoPkg(pkg));
}
let targs = interactive_menu(config, all_pkgs, &paths, false)?;
ensure!(!targs.is_empty(), "{}", tr!(" there is nothing to do"));
config.targets = targs.clone();
config.args.targets = targs;
Ok(())
}

pub async fn interactive_search(config: &mut Config, install: bool) -> Result<()> {
let mut repos = Vec::new();
let mut paths = HashMap::new();

Expand All @@ -400,11 +434,23 @@ pub async fn search_install(config: &mut Config) -> Result<i32> {
all_pkgs.push(AnyPkg::AurPkg(pkg));
}

let targs = interactive_menu(config, all_pkgs, &paths, install)?;
ensure!(!targs.is_empty(), "{}", tr!(" there is nothing to do"));
config.targets = targs.clone();
config.args.targets = targs;
Ok(())
}

pub fn interactive_menu(
config: &Config,
mut all_pkgs: Vec<AnyPkg<'_>>,
paths: &HashMap<(String, String), PathBuf>,
install: bool,
) -> Result<Vec<String>> {
let pad = all_pkgs.len().to_string().len();

if all_pkgs.is_empty() {
printtr!("no packages match search");
return Ok(1);
bail!("{}", tr!("no packages match search"));
}

let indexes = all_pkgs
Expand Down Expand Up @@ -432,19 +478,22 @@ pub async fn search_install(config: &mut Config) -> Result<i32> {

if config.sort_mode == SortMode::TopDown {
for (n, pkg) in all_pkgs.iter().enumerate() {
print_any_pkg(config, n, pad, pkg, &paths)
print_any_pkg(config, n, pad, pkg, paths)
}
} else {
for (n, pkg) in all_pkgs.iter().enumerate().rev() {
print_any_pkg(config, n, pad, pkg, &paths)
print_any_pkg(config, n, pad, pkg, paths)
}
}

let input = input(config, &tr!("Packages to install (eg: 1 2 3, 1-3):"));
let input = if install {
input(config, &tr!("Packages to install (eg: 1 2 3, 1-3):"))
} else {
input(config, &tr!("Select packages (eg: 1 2 3, 1-3):"))
};

if input.trim().is_empty() {
printtr!(" there is nothing to do");
return Ok(1);
bail!("{}", tr!(" there is nothing to do"));
}

let menu = NumberMenu::new(&input);
Expand Down Expand Up @@ -480,15 +529,7 @@ pub async fn search_install(config: &mut Config) -> Result<i32> {
}
}

if pkgs.is_empty() {
printtr!(" there is nothing to do")
} else {
config.need_root = true;
config.args.remove("x").remove("regex");
install(config, &pkgs).await?;
}

Ok(0)
Ok(pkgs)
}

fn print_any_pkg(
Expand Down

0 comments on commit c55c983

Please sign in to comment.