1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;

use anyhow::Result;
use log::{info, warn};

use crate::platform::directories::get_config_directories;

/// Return the contents of the alias file, if it exists and can be parsed.
/// The file should be located in `pueue_directory` and named `pueue_aliases`.
pub fn get_aliases() -> Result<HashMap<String, String>> {
    // Go through all config directories and check for a alias file.
    let mut alias_file_path = None;
    for directory in get_config_directories()? {
        let path = directory.join("pueue_aliases.yml");
        if path.exists() {
            alias_file_path = Some(path);
        }
    }

    // Return early if we cannot find the file
    let alias_file_path = match alias_file_path {
        None => {
            info!("Didn't find pueue alias file.");
            return Ok(HashMap::new());
        }
        Some(alias_file_path) => alias_file_path,
    };

    // Read the file content
    let mut alias_file = File::open(alias_file_path)?;
    let mut content = String::new();
    alias_file.read_to_string(&mut content)?;

    Ok(serde_yaml::from_str(&content)?)
}

/// Check if there exists an alias for a given command.
/// Only the first word will be replaced. The separator is a space.
pub fn insert_alias(command: String) -> String {
    let first = match command.split_whitespace().next() {
        Some(first) => first,
        None => return command,
    };

    let aliases = match get_aliases() {
        Err(err) => {
            warn!("Failed to open aliases file: {}", err);
            return command;
        }
        Ok(aliases) => aliases,
    };

    for (original, alias) in aliases.iter() {
        if original == first {
            return command.replacen(original, alias, 1);
        }
    }

    command
}