ssh_utils_lib/
helper.rs

1use std::path::PathBuf;
2use anyhow::{Context, Result};
3use ratatui::layout::{Constraint, Direction, Layout, Rect};
4
5use crate::config::app_vault::EncryptionKey;
6
7pub static CONFIG_FILE: &str = "config.toml";
8pub static ENCRYPTED_FILE: &str = "encrypted_data.bin";
9
10pub fn get_file_path(file_name: &str) -> Result<String> {
11    let mut config_dir: PathBuf = if cfg!(debug_assertions) {
12        ".".into() // current running dir
13    } else {
14        dirs::home_dir().context("Unable to reach user's home directory.")?
15    };
16
17    config_dir.push(".config/ssh-utils");
18    config_dir.push(file_name);
19    let file_path = config_dir.to_str().context("Failed to convert path to string")?.to_string();
20    Ok(file_path)
21}
22
23pub fn convert_to_array(vec: &EncryptionKey) -> Result<[u8; 32]> {
24    let slice = vec.as_slice();
25    let array: &[u8; 32] = slice.try_into().context("Failed to convert Vec<u8> to [u8; 32]")?;
26    Ok(*array)
27}
28
29/// helper function to create a centered rect using up certain percentage of the available rect `r`
30pub fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
31    let popup_layout = Layout::default()
32        .direction(Direction::Vertical)
33        .constraints(
34            [
35                Constraint::Percentage((100 - percent_y) / 2),
36                Constraint::Percentage(percent_y),
37                Constraint::Percentage((100 - percent_y) / 2),
38            ]
39            .as_ref(),
40        )
41        .split(r);
42
43    Layout::default()
44        .direction(Direction::Horizontal)
45        .constraints(
46            [
47                Constraint::Percentage((100 - percent_x) / 2),
48                Constraint::Percentage(percent_x),
49                Constraint::Percentage((100 - percent_x) / 2),
50            ]
51            .as_ref(),
52        )
53        .split(popup_layout[1])[1]
54}