Skip to content

Commit

Permalink
load config macos
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Jan 21, 2023
1 parent 43fb2c9 commit 0183a4c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 50 deletions.
35 changes: 30 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,47 @@ The configuration should be the following paths otherwise Rio will use the defau
#### config.toml

```toml
# Rio configuration file
performance = "High"
height = 400
width = 600
```

### List

#### Perfomance

- High: Adapter that has the highest performance. This is often a discrete GPU.
- Low: Adapter that uses the least possible power. This is often an integrated GPU.

See more in https://docs.rs/wgpu/latest/wgpu/enum.PowerPreference.html

```toml
# <performance> Set WGPU rendering perfomance
# default: High
# options: High, Average, Low
# options: High, Low
# High: Adapter that has the highest performance. This is often a discrete GPU.
# Low: Adapter that uses the least possible power. This is often an integrated GPU.
performance = "High"
```

### Height

Sets terminal window height

```toml
# <height> Set default height
# default: 400
height = 400
```

### Width

Sets terminal window width

```toml
# <width> Set default width
# default: 600
# default: 400
width = 600

## TODO: Add more configs
```

## TODO
Expand Down
86 changes: 45 additions & 41 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
use serde::Deserialize;
use std::default::Default;
#[allow(unused_imports)]
use std::io::Write;

#[derive(Default, Debug, Deserialize, PartialEq, Clone, Copy)]
enum Performance {
pub enum Performance {
#[default]
High,
Average,
Low,
}

#[derive(Debug, Deserialize, PartialEq)]
struct Config {
performance: Performance,
width: u16,
height: u16,
pub struct Config {
pub performance: Performance,
pub width: u16,
pub height: u16,
}

impl Config {
#[allow(dead_code)]
fn load_from_path(path: &str) -> Self {
if std::path::Path::new(path).exists() {
let content = std::fs::read_to_string(path).unwrap();
Expand All @@ -29,8 +30,7 @@ impl Config {
}
}

#[allow(dead_code)]
fn load() -> Self {
pub fn load_macos() -> Self {
let base_dir_buffer = dirs::home_dir().unwrap();
let base_dir = base_dir_buffer.to_str().unwrap();

Expand All @@ -46,38 +46,6 @@ impl Config {
}
}

#[allow(dead_code)]
fn create_temporary_config(performance: Performance, width: u16, height: u16) -> Config {
let toml_str = format!(
r#"
# Rio configuration file
# <performance> Set WGPU rendering performance
# default: High
# options: High, Average, Low
performance = "{:?}"
# <height> Set default height
# default: 400
height = {}
# <width> Set default width
# default: 600
width = {}
## TODO: Add more configs
"#,
performance, height, width
);
let binding = format!("/tmp/{:?}-config.toml", performance);
let file_name = binding.as_str();

let mut file = std::fs::File::create(file_name).unwrap();
writeln!(file, "{}", toml_str).unwrap(); // writing using the macro 'writeln!'``

Config::load_from_path(file_name) // load_from_path should just call load() with a custom path
}

impl Default for Config {
fn default() -> Self {
Config {
Expand All @@ -92,6 +60,42 @@ impl Default for Config {
mod tests {
use super::*;

#[allow(dead_code)]
fn create_temporary_config(
performance: Performance,
width: u16,
height: u16,
) -> Config {
let toml_str = format!(
r#"
# Rio configuration file
# <performance> Set WGPU rendering performance
# default: High
# options: High, Low
performance = "{:?}"
# <height> Set default height
# default: 400
height = {}
# <width> Set default width
# default: 600
width = {}
## TODO: Add more configs
"#,
performance, height, width
);
let binding = format!("/tmp/{:?}-config.toml", performance);
let file_name = binding.as_str();

let mut file = std::fs::File::create(file_name).unwrap();
writeln!(file, "{}", toml_str).unwrap(); // writing using the macro 'writeln!'``

Config::load_from_path(file_name) // load_from_path should just call load() with a custom path
}

#[test]
fn load_default_config() {
let expected = Config {
Expand All @@ -111,7 +115,7 @@ mod tests {
#[test]
fn load_default_performance_config() {
let expected = Config {
performance: Performance::Average,
performance: Performance::Low,
width: 400,
height: 400,
};
Expand Down
1 change: 1 addition & 0 deletions rio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ image = { version = "0.24.0", default-features = false, features = ["ico"] }
bytemuck = { version = "1.9", features = [ "derive" ] }
vte = "0.11.0"
tty = { path = "../tty" }
config = { path = "../config" }

[package.metadata.bundle]
name = "Rio"
Expand Down
7 changes: 4 additions & 3 deletions rio/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod text;
mod window;

use crate::term::Term;
use config::Config;
use std::borrow::Cow;
use std::error::Error;
use std::sync::Arc;
Expand All @@ -20,17 +21,17 @@ async fn main() -> Result<(), Box<dyn Error>> {
let window_builder = window::create_window_builder("Rio");
let winit_window = window_builder.build(&event_loop).unwrap();

std::env::set_var("TERM", "xterm-256color");
let config = Config::load_macos();

// todo: read from config
std::env::set_var("TERM", "xterm-256color");
let shell: String = match std::env::var("SHELL") {
Ok(val) => val,
Err(..) => String::from("bash"),
};
let (process, mut w_process, _pid) =
pty(&Cow::Borrowed(&shell), COLS as u16, ROWS as u16);

let mut rio: Term = match Term::new(&winit_window).await {
let mut rio: Term = match Term::new(&winit_window, &config).await {
Ok(term_instance) => term_instance,
Err(e) => {
panic!("couldn't create Rio terminal {}", e);
Expand Down
8 changes: 7 additions & 1 deletion rio/src/term/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@ const IDENTITY_MATRIX: [f32; 16] = [
impl Term {
pub async fn new(
winit_window: &winit::window::Window,
config: &config::Config,
) -> Result<Term, Box<dyn Error>> {
let instance = wgpu::Instance::new(wgpu::Backends::all());
let surface = unsafe { instance.create_surface(&winit_window) };

let power_preference: wgpu::PowerPreference = match config.performance {
config::Performance::High => wgpu::PowerPreference::HighPerformance,
config::Performance::Low => wgpu::PowerPreference::LowPower,
};

let (device, queue) = (async {
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
power_preference,
compatible_surface: Some(&surface),
force_fallback_adapter: false,
})
Expand Down

0 comments on commit 0183a4c

Please sign in to comment.