Draw ui
This commit is contained in:
		@@ -6,20 +6,24 @@ use std::path::{Path, PathBuf};
 | 
			
		||||
pub enum ConsumptionBackend {
 | 
			
		||||
    /// Constant consumption value
 | 
			
		||||
    Constant {
 | 
			
		||||
        /// The constant value to use
 | 
			
		||||
        #[clap(short, long, default_value_t = 500)]
 | 
			
		||||
        value: i32,
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    /// Generate random consumption value
 | 
			
		||||
    Random {
 | 
			
		||||
        /// Minimum acceptable generated value
 | 
			
		||||
        #[clap(long, default_value_t = -5000)]
 | 
			
		||||
        min: i32,
 | 
			
		||||
        /// Maximum acceptable generated value
 | 
			
		||||
        #[clap(long, default_value_t = 20000)]
 | 
			
		||||
        max: i32,
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    /// Read consumption value in a file, on the filesystem
 | 
			
		||||
    File {
 | 
			
		||||
        /// The path to the file that will be read to process consumption values
 | 
			
		||||
        #[clap(short, long, default_value = "/dev/shm/consumption.txt")]
 | 
			
		||||
        path: String,
 | 
			
		||||
    },
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								custom_consumption/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								custom_consumption/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
.idea
 | 
			
		||||
target
 | 
			
		||||
							
								
								
									
										3815
									
								
								custom_consumption/Cargo.lock
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										3815
									
								
								custom_consumption/Cargo.lock
									
									
									
										generated
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										11
									
								
								custom_consumption/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								custom_consumption/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
[package]
 | 
			
		||||
name = "custom_consumption"
 | 
			
		||||
version = "0.1.0"
 | 
			
		||||
edition = "2021"
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
env_logger = "0.11.3"
 | 
			
		||||
clap = { version = "4.5.8", features = ["derive", "env"] }
 | 
			
		||||
egui = "0.27.2"
 | 
			
		||||
eframe = "0.27.2"
 | 
			
		||||
lazy_static = "1.5.0"
 | 
			
		||||
							
								
								
									
										6
									
								
								custom_consumption/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								custom_consumption/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
# Custom consumption
 | 
			
		||||
 | 
			
		||||
## Install dependencies
 | 
			
		||||
```bash
 | 
			
		||||
sudo apt-get install libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev libxkbcommon-dev libssl-dev
 | 
			
		||||
```
 | 
			
		||||
							
								
								
									
										48
									
								
								custom_consumption/src/app_config.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								custom_consumption/src/app_config.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,48 @@
 | 
			
		||||
use clap::Parser;
 | 
			
		||||
use std::path::{Path, PathBuf};
 | 
			
		||||
 | 
			
		||||
/// Solar system central backend
 | 
			
		||||
#[derive(Parser, Debug)]
 | 
			
		||||
#[command(version, about, long_about = None)]
 | 
			
		||||
pub struct AppConfig {
 | 
			
		||||
    /// The path to the file to write to update the value
 | 
			
		||||
    #[clap(short, long, default_value = "/dev/shm/consumption.txt")]
 | 
			
		||||
    file_path: String,
 | 
			
		||||
 | 
			
		||||
    /// Minimal value
 | 
			
		||||
    #[clap( long, default_value_t = -5000)]
 | 
			
		||||
    pub min: i64,
 | 
			
		||||
 | 
			
		||||
    /// Maximal value
 | 
			
		||||
    #[clap(long, default_value_t = 5000)]
 | 
			
		||||
    pub max: i64,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
lazy_static::lazy_static! {
 | 
			
		||||
    static ref ARGS: AppConfig = {
 | 
			
		||||
        AppConfig::parse()
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl AppConfig {
 | 
			
		||||
    /// Get parsed command line arguments
 | 
			
		||||
    pub fn get() -> &'static AppConfig {
 | 
			
		||||
        &ARGS
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Path to the file to update the value
 | 
			
		||||
    pub fn file_path(&self) -> PathBuf {
 | 
			
		||||
        Path::new(&self.file_path).to_path_buf()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod test {
 | 
			
		||||
    use crate::app_config::AppConfig;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn verify_cli() {
 | 
			
		||||
        use clap::CommandFactory;
 | 
			
		||||
        AppConfig::command().debug_assert()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1
									
								
								custom_consumption/src/lib.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								custom_consumption/src/lib.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
pub mod app_config;
 | 
			
		||||
							
								
								
									
										52
									
								
								custom_consumption/src/main.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								custom_consumption/src/main.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,52 @@
 | 
			
		||||
use custom_consumption::app_config::AppConfig;
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    env_logger::init();
 | 
			
		||||
 | 
			
		||||
    let options = eframe::NativeOptions {
 | 
			
		||||
        viewport: egui::ViewportBuilder::default().with_inner_size([220.0, 60.0]),
 | 
			
		||||
        ..Default::default()
 | 
			
		||||
    };
 | 
			
		||||
    eframe::run_native(
 | 
			
		||||
        "Custom consumption",
 | 
			
		||||
        options,
 | 
			
		||||
        Box::new(|_cc| Box::<MyApp>::default()),
 | 
			
		||||
    )
 | 
			
		||||
    .unwrap()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct MyApp {
 | 
			
		||||
    consumption: i64,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Default for MyApp {
 | 
			
		||||
    fn default() -> Self {
 | 
			
		||||
        let mut prev_consumption = 42;
 | 
			
		||||
 | 
			
		||||
        if AppConfig::get().file_path().is_file() {
 | 
			
		||||
            prev_consumption = std::fs::read_to_string(AppConfig::get().file_path())
 | 
			
		||||
                .unwrap_or("43".to_string())
 | 
			
		||||
                .parse()
 | 
			
		||||
                .unwrap_or(44)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Self {
 | 
			
		||||
            consumption: prev_consumption,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl eframe::App for MyApp {
 | 
			
		||||
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
 | 
			
		||||
        egui::CentralPanel::default().show(ctx, |ui| {
 | 
			
		||||
            ui.heading("Custom consumption");
 | 
			
		||||
            ui.add(
 | 
			
		||||
                egui::Slider::new(
 | 
			
		||||
                    &mut self.consumption,
 | 
			
		||||
                    AppConfig::get().min..=AppConfig::get().max,
 | 
			
		||||
                )
 | 
			
		||||
                .text("W"),
 | 
			
		||||
            );
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user