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

Feat#43 add cmd args for optionsfilters #48

Merged
merged 3 commits into from
Aug 13, 2024
Merged
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: 6 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ pub struct Args {
If no prefix is given search will be done by process name"#
)]
pub query: String,
/// On linux threads can be listed as processes which are ignored by default. This flag allows to include them
#[arg(short = 't', long, default_value_t = false)]
pub include_threads_processes: bool,
/// By default pik shows only proceseses owned by current user. This flag allows to show all processes
#[arg(short, long, default_value_t = false)]
pub all_processes: bool,
}
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use args::Args;
use clap::Parser;
use processes::FilterOptions;
use tui::start_app;

mod args;
Expand All @@ -9,5 +10,11 @@ mod tui;

fn main() -> Result<()> {
let args = Args::parse();
start_app(args.query)
start_app(
args.query,
FilterOptions {
ignore_threads: !args.include_threads_processes,
include_all_processes: args.all_processes,
},
)
}
8 changes: 4 additions & 4 deletions src/processes/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl QueryFilter {
pub struct FilterOptions {
//NOTE: On linux threads can be listed as processes and thus needs filtering
pub ignore_threads: bool,
pub user_processes_only: bool,
pub include_all_processes: bool,
}

pub(super) struct OptionsFilter<'a> {
Expand All @@ -78,7 +78,7 @@ impl<'a> OptionsFilter<'a> {
if self.opt.ignore_threads && prc.is_thread() {
return false;
}
if !self.opt.user_processes_only {
if self.opt.include_all_processes {
return true;
}
prc.user_id() == Some(self.current_user_id)
Expand Down Expand Up @@ -267,7 +267,7 @@ pub mod tests {
let current_user_id = Uid::from_str("1000").unwrap();
let filter = OptionsFilter::new(
FilterOptions {
user_processes_only: true,
include_all_processes: false,
..Default::default()
},
&current_user_id,
Expand All @@ -287,7 +287,7 @@ pub mod tests {
let current_user_id = Uid::from_str("1000").unwrap();
let filter = OptionsFilter::new(
FilterOptions {
user_processes_only: false,
include_all_processes: true,
..Default::default()
},
&current_user_id,
Expand Down
11 changes: 4 additions & 7 deletions src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ struct App {
}

impl App {
fn new(search_criteria: String) -> Result<App> {
fn new(search_criteria: String, filter_options: FilterOptions) -> Result<App> {
let mut app = App {
process_manager: ProcessManager::new()?,
search_results: ProcessSearchResults::empty(),
filter_options: FilterOptions {
ignore_threads: true,
user_processes_only: true,
},
filter_options,
tui: Tui::new(search_criteria),
};
app.search_for_processess();
Expand Down Expand Up @@ -75,7 +72,7 @@ impl App {
}
}

pub fn start_app(search_criteria: String) -> Result<()> {
pub fn start_app(search_criteria: String, filter_options: FilterOptions) -> Result<()> {
// setup terminal
enable_raw_mode()?;
let backend = CrosstermBackend::new(io::stdout());
Expand All @@ -87,7 +84,7 @@ pub fn start_app(search_criteria: String) -> Result<()> {
)?;

// create app and run it
let app = App::new(search_criteria)?;
let app = App::new(search_criteria, filter_options)?;
let res = run_app(&mut terminal, app);

// restore terminal
Expand Down