diff --git a/README.md b/README.md index 6d7f6fdd..d6c7bfd6 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,9 @@ cargo install autoclockspeed ## In Action [![image](https://user-images.githubusercontent.com/35516367/151716685-a3ed3c53-07f4-459f-a3ae-e1de1ba16429.png)](https://www.youtube.com/watch?v=T9nN_rQOYsg) +## New Interactive Mode +![image](https://user-images.githubusercontent.com/35516367/170412105-08d9fdfd-b3fe-4a8b-b92b-b6af5580c319.png) + ## Systemd In order to have auto-clock-speed start when you restart your computer you must follow these instructions ```sh diff --git a/src/interactive.rs b/src/interactive.rs new file mode 100644 index 00000000..0fe3c77d --- /dev/null +++ b/src/interactive.rs @@ -0,0 +1,96 @@ +use super::config::{get_config, Config}; +use super::interface::{Get, Getter, Interface, Set, Setter}; +use super::settings::Settings; +use colored::Colorize; +use std::io::{stdin, stdout, Write}; + +pub fn help() { + println!("{}\n", "Help:".bold().green()); + println!("- get"); + println!(" - freq"); + println!(" - cpus"); + println!(" - temp"); + println!(" - govs"); + println!(" - power"); + println!(" - usage"); + println!(" - turbo"); + println!(" - available_governors"); + println!(" "); + println!("- set"); + println!(" - gov"); + println!(" "); + println!("E.g. 'get cpus'"); +} + +pub fn interactive() { + let int = Interface { + set: Set {}, + get: Get {}, + }; + + let mut input; + + println!("{}", "Auto Clock Speed".bold()); + println!("{}", "Interactive Mode".bold().blue()); + + let set_settings = Settings { + verbose: true, + delay_battery: 0, + delay: 0, + edit: false, + no_animation: false, + should_graph: false, + commit: false, + testing: false, + }; + + loop { + print!("{}", "\n> ".bold().green()); + stdout().flush().unwrap(); + + input = String::new(); + + match stdin().read_line(&mut input) { + Ok(_) => { + input.pop(); + let new = input.as_str(); + match new { + "help" => help(), + "get freq" => int.get.freq(false), + "get power" => int.get.power(false), + "get usage" => int.get.usage(false), + "get turbo" => int.get.turbo(false), + "get available_governors" => int.get.available_govs(false), + "get cpus" => int.get.cpus(false), + "get speeds" => int.get.speeds(false), + "get temp" => int.get.temp(false), + "get govs" => int.get.govs(false), + + "set gov performance" => { + let config: Config = get_config(); + + int.set + .gov("performance".to_string(), config, set_settings.clone()); + } + + "set gov powersave" => { + let config: Config = get_config(); + + int.set + .gov("powersave".to_string(), config, set_settings.clone()); + } + + "exit" => { + println!("Bye!"); + return; + } + _ => println!( + "{}", + format!("Command '{}' not found. Use 'help'.", new).red() + ), + }; + } + Err(error) => println!("error: {error}"), + } + } +} diff --git a/src/interface.rs b/src/interface.rs new file mode 100644 index 00000000..4ed18e4a --- /dev/null +++ b/src/interface.rs @@ -0,0 +1,124 @@ +use super::config::Config; +use super::daemon::{daemon_init, Checker}; +use super::display::{ + print_available_governors, print_cpu_governors, print_cpu_speeds, print_cpu_temp, print_cpus, + print_freq, print_power, print_turbo, +}; +use super::power::{read_battery_charge, read_lid_state, read_power_source}; +use super::settings::Settings; +use super::system::{ + check_available_governors, check_cpu_freq, check_cpu_name, check_turbo_enabled, + get_cpu_percent, list_cpu_governors, list_cpu_speeds, list_cpu_temp, list_cpus, +}; + +pub struct Get {} + +pub trait Getter { + fn freq(&self, raw: bool); + fn power(&self, raw: bool); + fn usage(&self, raw: bool); + fn turbo(&self, raw: bool); + fn available_govs(&self, raw: bool); + fn cpus(&self, raw: bool); + fn speeds(&self, raw: bool); + fn temp(&self, raw: bool); + fn govs(&self, raw: bool); +} + +impl Getter for Get { + fn freq(&self, raw: bool) { + let f = check_cpu_freq(); + print_freq(f, raw); + } + + fn power(&self, raw: bool) { + match read_lid_state() { + Ok(lid) => match read_battery_charge() { + Ok(bat) => match read_power_source() { + Ok(plugged) => { + print_power(lid, bat, plugged, raw); + } + Err(_) => eprintln!("Failed to get read power source"), + }, + Err(_) => eprintln!("Failed to get read battery charger"), + }, + Err(_) => eprintln!("Failed to get read lid state"), + }; + } + + fn usage(&self, raw: bool) { + if !raw { + println!("Calculating cpu percentage over 1 second."); + } + match get_cpu_percent() { + Ok(content) => { + if raw { + println!("{}", content) + } else { + println!("CPU is at {}%", content) + } + } + Err(_) => println!("Unable to usage status"), + } + } + + fn turbo(&self, raw: bool) { + match check_turbo_enabled() { + Ok(turbo_enabled) => print_turbo(turbo_enabled, raw), + Err(_) => println!("Failed to get turbo status"), + }; + } + + fn available_govs(&self, raw: bool) { + match check_available_governors() { + Ok(available_governors) => print_available_governors(available_governors, raw), + Err(_) => println!("Failed to get available governors"), + }; + } + + fn cpus(&self, raw: bool) { + let cpus = list_cpus(); + match check_cpu_name() { + Ok(name) => print_cpus(cpus, name, raw), + Err(_) => println!("Failed get list of cpus"), + }; + } + + fn speeds(&self, raw: bool) { + let speeds = list_cpu_speeds(); + print_cpu_speeds(speeds, raw); + } + + fn temp(&self, raw: bool) { + let cpu_temp = list_cpu_temp(); + print_cpu_temp(cpu_temp, raw); + } + + fn govs(&self, raw: bool) { + let govs = list_cpu_governors(); + print_cpu_governors(govs, raw); + } +} + +pub struct Set {} + +pub trait Setter { + fn gov(&self, value: String, config: Config, settings: Settings); +} + +impl Setter for Set { + fn gov(&self, value: String, config: Config, settings: Settings) { + match daemon_init(settings, config) { + Ok(mut d) => match d.set_govs(value.clone()) { + Ok(_) => {} + Err(e) => eprint!("Could not set gov, {:?}", e), + }, + Err(_) => eprint!("Could not run daemon in edit mode"), + } + } +} + +pub struct Interface { + pub get: Get, + pub set: Set, +} diff --git a/src/main.rs b/src/main.rs index 99de9894..116d40ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,17 +3,11 @@ use structopt::StructOpt; use config::{config_dir_exists, get_config}; use daemon::{daemon_init, Checker}; -use display::{ - print_available_governors, print_cpu_governors, print_cpu_speeds, print_cpu_temp, print_cpus, - print_freq, print_power, print_turbo, show_config, -}; +use display::show_config; use error::Error; -use power::{read_battery_charge, read_lid_state, read_power_source}; +use interactive::interactive; +use interface::{Get, Getter, Interface, Set, Setter}; use settings::Settings; -use system::{ - check_available_governors, check_cpu_freq, check_cpu_name, check_turbo_enabled, - get_cpu_percent, list_cpu_governors, list_cpu_speeds, list_cpu_temp, list_cpus, -}; pub mod config; pub mod cpu; @@ -21,6 +15,8 @@ pub mod daemon; pub mod display; pub mod error; pub mod graph; +pub mod interactive; +pub mod interface; pub mod logger; pub mod power; pub mod settings; @@ -110,7 +106,7 @@ enum SetType { )] enum ACSCommand { /// Get a specific value or status - #[structopt(name = "get")] + #[structopt(name = "get", alias = "g")] Get { /// The type of value to request #[structopt(subcommand)] @@ -118,12 +114,16 @@ enum ACSCommand { }, /// Set a specific value - #[structopt(name = "set")] + #[structopt(name = "set", alias = "s")] Set { #[structopt(subcommand)] set: SetType, }, + /// Interactive mode for auto clock speed commands + #[structopt(name = "interactive", alias = "i")] + Interactive {}, + /// Show the current config in use #[structopt(name = "showconfig", alias = "conf")] ShowConfig {}, @@ -184,7 +184,6 @@ enum ACSCommand { fn parse_args(config: config::Config) { let mut daemon: daemon::Daemon; - // default settings used by set command let set_settings = Settings { verbose: true, delay_battery: 0, @@ -196,89 +195,55 @@ fn parse_args(config: config::Config) { testing: false, }; + let int = Interface { + set: Set {}, + get: Get {}, + }; + match ACSCommand::from_args() { - // Everything starting with "get" ACSCommand::Get { get } => match get { GetType::Freq { raw } => { - let f = check_cpu_freq(); - print_freq(f, raw); + int.get.freq(raw); } - GetType::Power { raw } => match read_lid_state() { - Ok(lid) => match read_battery_charge() { - Ok(bat) => match read_power_source() { - Ok(plugged) => { - print_power(lid, bat, plugged, raw); - } - Err(_) => eprintln!("Failed to get read power source"), - }, - Err(_) => eprintln!("Failed to get read battery charger"), - }, - Err(_) => eprintln!("Failed to get read lid state"), - }, - + GetType::Power { raw } => { + int.get.power(raw); + } GetType::Usage { raw } => { - if !raw { - println!("Calculating cpu percentage over 1 second."); - } - match get_cpu_percent() { - Ok(content) => { - if raw { - println!("{}", content) - } else { - println!("CPU is at {}%", content) - } - } - Err(_) => println!("Unable to usage status"), - } + int.get.usage(raw); } - GetType::Turbo { raw } => match check_turbo_enabled() { - Ok(turbo_enabled) => print_turbo(turbo_enabled, raw), - Err(_) => println!("Failed to get turbo status"), - }, - - GetType::AvailableGovs { raw } => match check_available_governors() { - Ok(available_governors) => print_available_governors(available_governors, raw), - Err(_) => println!("Failed to get available governors"), - }, - + GetType::Turbo { raw } => { + int.get.turbo(raw); + } + GetType::AvailableGovs { raw } => { + int.get.available_govs(raw); + } GetType::CPUS { raw } => { - let cpus = list_cpus(); - match check_cpu_name() { - Ok(name) => print_cpus(cpus, name, raw), - Err(_) => println!("Failed get list of cpus"), - }; + int.get.cpus(raw); } GetType::Speeds { raw } => { - let speeds = list_cpu_speeds(); - print_cpu_speeds(speeds, raw); + int.get.speeds(raw); } GetType::Temp { raw } => { - let cpu_temp = list_cpu_temp(); - print_cpu_temp(cpu_temp, raw); + int.get.temp(raw); } GetType::Govs { raw } => { - let govs = list_cpu_governors(); - print_cpu_governors(govs, raw); + int.get.govs(raw); } }, - // Everything starting with "set" ACSCommand::Set { set } => match set { - SetType::Gov { value } => match daemon_init(set_settings, config) { - Ok(mut d) => match d.set_govs(value.clone()) { - Ok(_) => {} - Err(e) => eprint!("Could not set gov, {:?}", e), - }, - Err(_) => eprint!("Could not run daemon in edit mode"), - }, + SetType::Gov { value } => { + int.set.gov(value, config, set_settings); + } }, ACSCommand::ShowConfig {} => show_config(), + ACSCommand::Interactive {} => interactive(), // Run command ACSCommand::Run { diff --git a/src/settings.rs b/src/settings.rs index 58f46082..bbcdd33f 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,3 +1,4 @@ +#[derive(Clone)] pub struct Settings { pub verbose: bool, pub delay: u64,