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

Interface interactive #274

Merged
merged 13 commits into from
May 26, 2022
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
96 changes: 96 additions & 0 deletions src/interactive.rs
Original file line number Diff line number Diff line change
@@ -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}"),
}
}
}
124 changes: 124 additions & 0 deletions src/interface.rs
Original file line number Diff line number Diff line change
@@ -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,
}
Loading