Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BottomUp=auto config option #865

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions man/paru.conf.5
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ your user's config and then defining options.
Options belonging to the [options] section.

.TP
.B BottomUp
Print search results from bottom to top. AUR results will be printed first.
.B BottomUp [= auto]
Print search results from bottom to top. AUR results will be printed first. If
set to auto, prints results from bottom to top if standard output is a tty,
otherwise top to bottom.

.TP
.B AurOnly
Expand Down
25 changes: 22 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,29 @@ impl ConfigEnum for raur::SearchBy {
pub enum SortMode {
BottomUp,
TopDown,
Auto,
}

impl SortMode {
//! Turns [SortMode::Auto] into one of the other variants.
pub fn resolve(&self) -> Self {
if matches!(self, SortMode::Auto) {
match isatty(stdout().as_raw_fd()) {
Ok(true) => SortMode::BottomUp,
_ => SortMode::TopDown,
}
} else {
*self
}
}
}

impl ConfigEnum for SortMode {
const VALUE_LOOKUP: ConfigEnumValues<Self> =
&[("bottomup", Self::BottomUp), ("topdown", Self::TopDown)];
const VALUE_LOOKUP: ConfigEnumValues<Self> = &[
("bottomup", Self::BottomUp),
("topdown", Self::TopDown),
("auto", Self::Auto),
];
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -950,7 +968,7 @@ impl Config {

match key {
"SkipReview" => self.skip_review = true,
"BottomUp" => self.sort_mode = SortMode::BottomUp,
"BottomUp" => self.sort_mode = SortMode::BottomUp.default_or(key, value)?,
"AurOnly" => self.mode = Mode::Aur,
"RepoOnly" => self.mode = Mode::Repo,
"SudoLoop" => {
Expand Down Expand Up @@ -1017,6 +1035,7 @@ impl Config {
"AurUrl" => self.aur_url = value?.parse()?,
"AurRpcUrl" => self.aur_rpc_url = Some(value?.parse()?),
"BuildDir" | "CloneDir" => self.build_dir = PathBuf::from(value?),
"BottomUp" => self.sort_mode = ConfigEnum::from_str(key, value?.as_str())?,
"Redownload" => self.redownload = ConfigEnum::from_str(key, value?.as_str())?,
"Rebuild" => self.rebuild = ConfigEnum::from_str(key, value?.as_str())?,
"RemoveMake" => self.remove_make = ConfigEnum::from_str(key, value?.as_str())?,
Expand Down
2 changes: 1 addition & 1 deletion src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ pub async fn show_comments(config: &mut Config) -> Result<i32> {

let iter = titles.zip(comments).collect::<Vec<_>>();

if config.sort_mode == SortMode::TopDown {
if config.sort_mode.resolve() == SortMode::TopDown {
for (title, comment) in iter.into_iter() {
print_indent(c.bold, 0, 0, config.cols, " ", title.split_whitespace());

Expand Down
6 changes: 3 additions & 3 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub async fn search(config: &Config) -> Result<i32> {
}
};

if config.sort_mode == SortMode::TopDown {
if config.sort_mode.resolve() == SortMode::TopDown {
for pkg in &repo_pkgs {
print_alpm_pkg(config, pkg, quiet);
}
Expand Down Expand Up @@ -434,7 +434,7 @@ pub async fn search_install(config: &mut Config) -> Result<i32> {
all_pkgs.insert(0, pkg);
}

if config.sort_mode == SortMode::TopDown {
if config.sort_mode.resolve() == SortMode::TopDown {
for (n, pkg) in all_pkgs.iter().enumerate() {
print_any_pkg(config, n, pad, pkg, &paths)
}
Expand All @@ -454,7 +454,7 @@ pub async fn search_install(config: &mut Config) -> Result<i32> {
let menu = NumberMenu::new(&input);
let mut pkgs = Vec::new();

if config.sort_mode == SortMode::TopDown {
if config.sort_mode.resolve() == SortMode::TopDown {
for (n, pkg) in all_pkgs.iter().enumerate() {
if menu.contains(n + 1, "") {
match pkg {
Expand Down