feat: add arguments for max responses

This commit is contained in:
Ashley Wulber 2025-01-23 17:59:07 -05:00 committed by Michael Murphy
parent 9f8f17a783
commit 0e01b09ddd
6 changed files with 180 additions and 9 deletions

117
Cargo.lock generated
View file

@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
version = 4
[[package]]
name = "addr2line"
@ -41,6 +41,56 @@ dependencies = [
"libc",
]
[[package]]
name = "anstream"
version = "0.6.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9"
[[package]]
name = "anstyle-parse"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c"
dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e"
dependencies = [
"anstyle",
"once_cell",
"windows-sys 0.59.0",
]
[[package]]
name = "anyhow"
version = "1.0.94"
@ -437,6 +487,52 @@ dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "clap"
version = "4.5.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "769b0145982b4b48713e01ec42d61614425f27b7058bda7180a3a41f30104796"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b26884eb4b57140e4d2d93652abfa49498b938b3c9179f9fc487b0acc3edad7"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54b755194d6389280185988721fffba69495eed5ee9feeee9a599b53db80318c"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 2.0.90",
]
[[package]]
name = "clap_lex"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6"
[[package]]
name = "colorchoice"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]]
name = "concurrent-queue"
version = "2.5.0"
@ -985,6 +1081,12 @@ version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hermit-abi"
version = "0.3.9"
@ -1342,6 +1444,12 @@ version = "2.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708"
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "itoa"
version = "1.0.14"
@ -1828,6 +1936,7 @@ dependencies = [
"anyhow",
"async-oneshot",
"async-trait",
"clap",
"dirs",
"flume",
"futures",
@ -2878,6 +2987,12 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "uuid"
version = "1.11.0"

View file

@ -18,7 +18,7 @@ enum Event {
}
pub async fn main() {
let (event_tx, event_rx) = flume::bounded::<Event>(8);
let (event_tx, event_rx) = flume::bounded::<Event>(20);
// Channel for cancelling searches that are in progress.
let (interrupt_tx, interrupt_rx) = flume::bounded::<()>(1);

View file

@ -99,7 +99,7 @@ impl App {
}
async fn reload(&mut self) {
let (tx, rx) = flume::bounded::<ScriptInfo>(8);
let (tx, rx) = flume::bounded::<ScriptInfo>(20);
let mut queue = VecDeque::new();

View file

@ -8,6 +8,7 @@ edition.workspace = true
anyhow.workspace = true
async-oneshot = "0.5.9"
async-trait = "0.1.83"
clap = { version = "4.5.27", features = ["derive"] }
dirs.workspace = true
futures.workspace = true
futures_codec = "0.4.1"

View file

@ -8,6 +8,8 @@ use tokio::io::{AsyncBufReadExt, AsyncWriteExt};
use tokio::process;
use tokio_stream::wrappers::LinesStream;
use crate::Args;
#[derive(Debug)]
pub struct IpcClient {
pub child: process::Child,
@ -15,8 +17,16 @@ pub struct IpcClient {
}
impl IpcClient {
pub fn new() -> io::Result<(Self, impl Stream<Item = Response>)> {
pub fn new_with_args(args: Args) -> io::Result<(Self, impl Stream<Item = Response>)> {
let mut child = process::Command::new("pop-launcher")
.args(&[
"--max-open",
args.max_open.to_string().as_str(),
"--max-files",
args.max_files.to_string().as_str(),
"--max-search",
args.max_search.to_string().as_str(),
])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.spawn()?;
@ -46,6 +56,10 @@ impl IpcClient {
Ok((client, responses))
}
pub fn new() -> io::Result<(Self, impl Stream<Item = Response>)> {
Self::new_with_args(Args::default())
}
pub async fn send(&mut self, request: Request) -> io::Result<()> {
let mut request_json = serde_json::to_string(&request)
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;

View file

@ -15,6 +15,7 @@ use crate::plugins::{
};
use crate::priority::Priority;
use crate::recent::RecentUseStorage;
use clap::Parser;
use flume::{Receiver, Sender};
use futures::{future, SinkExt, Stream, StreamExt};
use pop_launcher::{
@ -23,6 +24,7 @@ use pop_launcher::{
};
use regex::Regex;
use slab::Slab;
use std::usize;
use std::{
cmp::Ordering,
collections::{HashMap, HashSet},
@ -32,6 +34,33 @@ use std::{
pub type PluginKey = usize;
/// Bounds for the number of service responses.
#[derive(Parser, Debug, Clone, Copy)]
#[command(version, about, long_about = None)]
pub struct Args {
/// Max number of files in a response.
#[arg(short, long, default_value_t = 100)]
pub max_files: usize,
/// Max number of open windows in a response.
#[arg(short, long, default_value_t = 8)]
pub max_open: usize,
/// Max number of generic items in a response.
#[arg(short, long, default_value_t = 8)]
pub max_search: usize,
}
impl Default for Args {
fn default() -> Self {
Args {
max_files: 100,
max_open: 8,
max_search: 8,
}
}
}
pub enum Event {
Request(Request),
Response((PluginKey, PluginResponse)),
@ -67,6 +96,7 @@ pub fn store_cache(storage: &RecentUseStorage) {
}
pub async fn main() {
let args = Args::parse();
let cachepath = ensure_cache_path();
let read_recent = || -> Result<RecentUseStorage, Box<dyn std::error::Error>> {
let cachepath = std::fs::File::open(cachepath?)?;
@ -94,7 +124,9 @@ pub async fn main() {
let (output_tx, output_rx) = flume::bounded(16);
// Service will operate for as long as it is being awaited
let service = Service::new(output_tx.into_sink(), recent).exec(input_stream);
let service = Service::new(output_tx.into_sink(), recent)
.with_args(args)
.exec(input_stream);
// Responses from the service will be streamed to stdout
let responder = async move {
@ -119,6 +151,7 @@ pub struct Service<O> {
plugins: Slab<PluginConnector>,
search_scheduled: bool,
recent: RecentUseStorage,
args: Args,
}
impl<O: futures::Sink<Response> + Unpin> Service<O> {
@ -133,9 +166,15 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
plugins: Slab::new(),
search_scheduled: false,
recent,
args: Args::default(),
}
}
fn with_args(mut self, args: Args) -> Self {
self.args = args;
self
}
pub async fn exec(mut self, input: impl Stream<Item = Request>) {
let (service_tx, service_rx) = flume::bounded(1);
let stream = plugins::external::load::from_paths();
@ -284,7 +323,7 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
regex,
isolate_with,
Box::new(move || {
let (request_tx, request_rx) = flume::bounded(8);
let (request_tx, request_rx) = flume::bounded(20);
let init = init.clone();
let service_tx = service_tx.clone();
@ -593,10 +632,12 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
});
}
let take = if last_query.starts_with('/') | last_query.starts_with('~') {
100
let take = if last_query.is_empty() {
self.args.max_open
} else if last_query.starts_with('/') | last_query.starts_with('~') {
self.args.max_files
} else {
8
self.args.max_search
};
let mut windows = Vec::with_capacity(take);