Skip to content

Commit

Permalink
Merge pull request #312 from nokyan/cli-arguments
Browse files Browse the repository at this point in the history
Add CLI arguments
  • Loading branch information
nokyan committed Aug 21, 2024
2 parents 82fab21 + 68a110e commit 17e93fc
Show file tree
Hide file tree
Showing 14 changed files with 327 additions and 151 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ edition = "2021"
opt-level = 1

[profile.release]
opt-level = 3
strip = true
codegen-units = 1
lto = true
strip = true
opt-level = 3

[dependencies]
adw = { version = "0.7.0", features = ["v1_5"], package = "libadwaita" }
Expand Down
2 changes: 1 addition & 1 deletion src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl Application {
);
}

ApplicationExtManual::run(self);
ApplicationExtManual::run_with_args::<&str>(self, &[]);
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/bin/resources-adjust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ fn main() {

// find tasks that belong to this process
let tasks_path = PathBuf::from("/proc/").join(pid.to_string()).join("task");
for entry in std::fs::read_dir(tasks_path).unwrap() {
if let Ok(entry) = entry {
let thread_id = entry.file_name().to_string_lossy().parse().unwrap();
for entry in std::fs::read_dir(tasks_path).unwrap().flatten() {
let thread_id = entry.file_name().to_string_lossy().parse().unwrap();

adjust(thread_id, nice, &cpu_set);
}
adjust(thread_id, nice, &cpu_set);
}

std::process::exit(0)
Expand All @@ -49,5 +47,5 @@ fn adjust(id: i32, nice: i32, cpu_set: &CpuSet) {
std::process::exit(error)
}

let _ = sched_setaffinity(Pid::from_raw(id), &cpu_set);
let _ = sched_setaffinity(Pid::from_raw(id), cpu_set);
}
46 changes: 46 additions & 0 deletions src/gui.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,64 @@
use std::ffi::OsString;
use std::sync::LazyLock;

use crate::application;
#[rustfmt::skip]
use crate::config;
use crate::utils::app::DATA_DIRS;
use crate::utils::IS_FLATPAK;

use clap::{command, Parser};
use gettextrs::{gettext, LocaleCategory};
use gtk::{gio, glib};

use self::application::Application;
use self::config::{GETTEXT_PACKAGE, LOCALEDIR, RESOURCES_FILE};

pub static ARGS: LazyLock<Args> = LazyLock::new(Args::parse);

#[derive(Parser, Debug)]
#[command(version, about)]
pub struct Args {
/// Disable GPU monitoring
#[arg(short = 'g', long, default_value_t = false)]
pub disable_gpu_monitoring: bool,

/// Disable network interface monitoring
#[arg(short = 'n', long, default_value_t = false)]
pub disable_network_interface_monitoring: bool,

/// Disable drive monitoring
#[arg(short = 'd', long, default_value_t = false)]
pub disable_drive_monitoring: bool,

/// Disable battery monitoring
#[arg(short = 'b', long, default_value_t = false)]
pub disable_battery_monitoring: bool,

/// Disable CPU monitoring
#[arg(short = 'c', long, default_value_t = false)]
pub disable_cpu_monitoring: bool,

/// Disable memory monitoring
#[arg(short = 'm', long, default_value_t = false)]
pub disable_memory_monitoring: bool,

/// Disable process monitoring
#[arg(short = 'p', long, default_value_t = false)]
pub disable_process_monitoring: bool,

/// Open tab specified by ID.
/// Valid IDs are: "applications", "processes", "cpu", "memory", "gpu-$PCI_SLOT$",
/// "drive-$MODEL_NAME_OR_DEVICE_NAME$", "network-$INTERFACE_NAME$",
/// "battery-$MANUFACTURER$-$MODEL_NAME$-$DEVICE_NAME$"
#[arg(short = 't', long)]
pub open_tab_id: Option<String>,
}

pub fn main() {
// Force args parsing here so we don't start printing logs before printing the help page
std::hint::black_box(ARGS.disable_battery_monitoring);

// Initialize logger
pretty_env_logger::init();

Expand Down
96 changes: 62 additions & 34 deletions src/ui/pages/applications/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ mod imp {
pub filter_model: RefCell<gtk::FilterListModel>,
pub sort_model: RefCell<gtk::SortListModel>,
pub column_view: RefCell<gtk::ColumnView>,
pub open_dialog: RefCell<Option<(Option<String>, ResAppDialog)>>,
pub open_info_dialog: RefCell<Option<(Option<String>, ResAppDialog)>>,
pub info_dialog_closed: Cell<bool>,

pub sender: OnceLock<Sender<Action>>,

Expand Down Expand Up @@ -154,7 +155,8 @@ mod imp {
filter_model: Default::default(),
sort_model: Default::default(),
column_view: Default::default(),
open_dialog: Default::default(),
open_info_dialog: Default::default(),
info_dialog_closed: Default::default(),
sender: Default::default(),
applications_scrolled_window: Default::default(),
end_application_button: Default::default(),
Expand Down Expand Up @@ -239,7 +241,7 @@ mod imp {
if let Some(application_entry) =
res_applications.imp().popped_over_app.borrow().as_ref()
{
res_applications.open_information_dialog(application_entry);
res_applications.open_info_dialog(application_entry);
}
},
);
Expand Down Expand Up @@ -515,7 +517,7 @@ impl ResApplications {
.selected_item()
.map(|object| object.downcast::<ApplicationEntry>().unwrap());
if let Some(selection) = selection_option {
this.open_information_dialog(&selection);
this.open_info_dialog(&selection);
}
}
));
Expand Down Expand Up @@ -564,14 +566,32 @@ impl ResApplications {
}
}

pub fn open_information_dialog(&self, app: &ApplicationEntry) {
pub fn open_info_dialog(&self, app: &ApplicationEntry) {
let imp = self.imp();
let app_dialog = ResAppDialog::new();
app_dialog.init(app);
app_dialog.present(Some(&MainWindow::default()));
*imp.open_dialog.borrow_mut() = Some((

if imp.open_info_dialog.borrow().is_some() {
return;
}

imp.info_dialog_closed.set(false);

let dialog = ResAppDialog::new();

dialog.init(app);

dialog.present(Some(&MainWindow::default()));

dialog.connect_closed(clone!(
#[weak(rename_to = this)]
self,
move |_| {
this.imp().info_dialog_closed.set(true);
}
));

*imp.open_info_dialog.borrow_mut() = Some((
app.id().as_ref().map(std::string::ToString::to_string),
app_dialog,
dialog,
));
}

Expand Down Expand Up @@ -602,8 +622,13 @@ impl ResApplications {
pub fn refresh_apps_list(&self, apps_context: &AppsContext) {
let imp = self.imp();

if imp.info_dialog_closed.get() {
let _ = imp.open_info_dialog.take();
imp.info_dialog_closed.set(false);
}

let store = imp.store.borrow_mut();
let mut dialog_opt = &*imp.open_dialog.borrow_mut();
let mut dialog_opt = &*imp.open_info_dialog.borrow_mut();

let mut ids_to_remove = HashSet::new();
let mut already_existing_ids = HashSet::new();
Expand Down Expand Up @@ -671,7 +696,10 @@ impl ResApplications {
// -1 because we don't want to count System Processes
self.set_property(
"tab_usage_string",
i18n_f("Running Apps: {}", &[&(store.n_items() - 1).to_string()]),
i18n_f(
"Running Apps: {}",
&[&(store.n_items().saturating_sub(1)).to_string()],
),
);
}

Expand Down Expand Up @@ -789,8 +817,8 @@ impl ResApplications {
));

name_col_factory.connect_teardown(move |_factory, item| {
let item = item.downcast_ref::<gtk::ListItem>();
item.unwrap().set_child(None::<&ResApplicationNameCell>);
let item = item.downcast_ref::<gtk::ListItem>().unwrap();
item.set_child(None::<&ResApplicationNameCell>);
});

let name_col_sorter = StringSorter::builder()
Expand Down Expand Up @@ -841,8 +869,8 @@ impl ResApplications {
));

memory_col_factory.connect_teardown(move |_factory, item| {
let item = item.downcast_ref::<gtk::ListItem>();
item.unwrap().set_child(None::<&gtk::Inscription>);
let item = item.downcast_ref::<gtk::ListItem>().unwrap();
item.set_child(None::<&gtk::Inscription>);
});

let memory_col_sorter = NumericSorter::builder()
Expand Down Expand Up @@ -904,8 +932,8 @@ impl ResApplications {
));

cpu_col_factory.connect_teardown(move |_factory, item| {
let item = item.downcast_ref::<gtk::ListItem>();
item.unwrap().set_child(None::<&gtk::Inscription>);
let item = item.downcast_ref::<gtk::ListItem>().unwrap();
item.set_child(None::<&gtk::Inscription>);
});

let cpu_col_sorter = NumericSorter::builder()
Expand Down Expand Up @@ -968,8 +996,8 @@ impl ResApplications {
));

read_speed_col_factory.connect_teardown(move |_factory, item| {
let item = item.downcast_ref::<gtk::ListItem>();
item.unwrap().set_child(None::<&gtk::Inscription>);
let item = item.downcast_ref::<gtk::ListItem>().unwrap();
item.set_child(None::<&gtk::Inscription>);
});

let read_speed_col_sorter = NumericSorter::builder()
Expand Down Expand Up @@ -1028,8 +1056,8 @@ impl ResApplications {
));

read_total_col_factory.connect_teardown(move |_factory, item| {
let item = item.downcast_ref::<gtk::ListItem>();
item.unwrap().set_child(None::<&gtk::Inscription>);
let item = item.downcast_ref::<gtk::ListItem>().unwrap();
item.set_child(None::<&gtk::Inscription>);
});

let read_total_col_sorter = NumericSorter::builder()
Expand Down Expand Up @@ -1092,8 +1120,8 @@ impl ResApplications {
));

write_speed_col_factory.connect_teardown(move |_factory, item| {
let item = item.downcast_ref::<gtk::ListItem>();
item.unwrap().set_child(None::<&gtk::Inscription>);
let item = item.downcast_ref::<gtk::ListItem>().unwrap();
item.set_child(None::<&gtk::Inscription>);
});

let write_speed_col_sorter = NumericSorter::builder()
Expand Down Expand Up @@ -1154,8 +1182,8 @@ impl ResApplications {
));

write_total_col_factory.connect_teardown(move |_factory, item| {
let item = item.downcast_ref::<gtk::ListItem>();
item.unwrap().set_child(None::<&gtk::Inscription>);
let item = item.downcast_ref::<gtk::ListItem>().unwrap();
item.set_child(None::<&gtk::Inscription>);
});

let write_total_col_sorter = NumericSorter::builder()
Expand Down Expand Up @@ -1213,8 +1241,8 @@ impl ResApplications {
));

gpu_col_factory.connect_teardown(move |_factory, item| {
let item = item.downcast_ref::<gtk::ListItem>();
item.unwrap().set_child(None::<&gtk::Inscription>);
let item = item.downcast_ref::<gtk::ListItem>().unwrap();
item.set_child(None::<&gtk::Inscription>);
});

let gpu_col_sorter = NumericSorter::builder()
Expand Down Expand Up @@ -1273,8 +1301,8 @@ impl ResApplications {
));

encoder_col_factory.connect_teardown(move |_factory, item| {
let item = item.downcast_ref::<gtk::ListItem>();
item.unwrap().set_child(None::<&gtk::Inscription>);
let item = item.downcast_ref::<gtk::ListItem>().unwrap();
item.set_child(None::<&gtk::Inscription>);
});

let encoder_col_sorter = NumericSorter::builder()
Expand Down Expand Up @@ -1333,8 +1361,8 @@ impl ResApplications {
));

decoder_col_factory.connect_teardown(move |_factory, item| {
let item = item.downcast_ref::<gtk::ListItem>();
item.unwrap().set_child(None::<&gtk::Inscription>);
let item = item.downcast_ref::<gtk::ListItem>().unwrap();
item.set_child(None::<&gtk::Inscription>);
});

let decoder_col_sorter = NumericSorter::builder()
Expand Down Expand Up @@ -1390,8 +1418,8 @@ impl ResApplications {
));

gpu_mem_col_factory.connect_teardown(move |_factory, item| {
let item = item.downcast_ref::<gtk::ListItem>();
item.unwrap().set_child(None::<&gtk::Inscription>);
let item = item.downcast_ref::<gtk::ListItem>().unwrap();
item.set_child(None::<&gtk::Inscription>);
});

let gpu_mem_col_sorter = NumericSorter::builder()
Expand Down
5 changes: 3 additions & 2 deletions src/ui/pages/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::i18n::i18n;
use crate::utils::battery::BatteryData;
use crate::utils::units::{convert_energy, convert_power};

pub const TAB_ID_PREFIX: &str = "battery";

mod imp {
use std::cell::{Cell, RefCell};

Expand Down Expand Up @@ -206,7 +208,6 @@ impl Default for ResBattery {
}

impl ResBattery {
const ID_PREFIX: &'static str = "battery";
const MAIN_GRAPH_COLOR: [u8; 3] = [0x33, 0xd1, 0x7a];

pub fn new() -> Self {
Expand All @@ -224,7 +225,7 @@ impl ResBattery {

let tab_id = format!(
"{}-{}-{}-{}",
Self::ID_PREFIX,
TAB_ID_PREFIX,
battery.manufacturer.as_deref().unwrap_or_default(),
battery.model_name.as_deref().unwrap_or_default(),
battery.sysfs_path.file_name().unwrap().to_string_lossy(),
Expand Down
4 changes: 3 additions & 1 deletion src/ui/pages/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::utils::settings::SETTINGS;
use crate::utils::units::{convert_frequency, convert_temperature};
use crate::utils::{cpu, FiniteOr, NUM_CPUS};

pub const TAB_ID: &str = "cpu";

mod imp {
use std::cell::{Cell, RefCell};

Expand Down Expand Up @@ -155,7 +157,7 @@ mod imp {
tab_name: Cell::new(glib::GString::from(i18n("Processor"))),
tab_detail_string: Cell::new(glib::GString::new()),
tab_usage_string: Cell::new(glib::GString::new()),
tab_id: Cell::new(glib::GString::from("cpu")),
tab_id: Cell::new(glib::GString::from(TAB_ID)),
old_total_usage: Cell::default(),
old_thread_usages: RefCell::default(),
logical_cpus_amount: Cell::default(),
Expand Down
Loading

0 comments on commit 17e93fc

Please sign in to comment.