Skip to content

Commit

Permalink
add: save client config
Browse files Browse the repository at this point in the history
  • Loading branch information
luftaquila committed May 1, 2024
1 parent f74660c commit 41cb67b
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,5 +262,59 @@ fn prompt_display_position(displays: &mut Vec<Display>, server_conf: Vec<Display
}
}

// TODO: ask write config to file
loop {
print!("[INF] save current config? [y/n]: ");
stdout().flush().unwrap();

let ch = match stdin_char() {
Ok(ch) => ch,
Err(_) => {
continue;
}
};

match ch {
'y' => {
let path = config_dir!("client").join("client_config.json");

let mut file = match fs::File::create(&path) {
Ok(file) => file,
Err(e) => {
eprintln!(
"[ERR] failed to create {:?}: {:?}",
path.as_os_str().to_str().unwrap(),
e
);
break;
}
};

match serde_json::to_string_pretty(&displays) {
Ok(json) => {
if let Err(e) = file.write_all(json.as_bytes()) {
eprintln!(
"[ERR] failed to write to {:?}: {:?}",
path.as_os_str().to_str().unwrap(),
e
);
}

println!(
"[INF] config saved at {:?}",
path.as_os_str().to_str().unwrap()
);
}
Err(e) => eprintln!("[ERR] failed to save config into file: {:?}", e),
}

break;
}
'n' => {
break; // current display again
}
_ => {
continue;
}
}
}
}

0 comments on commit 41cb67b

Please sign in to comment.