agent-ssh 0.3.0

Arrange agent terminal layouts and reconnect through provider attachment plans
Documentation
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Host {
    pub name: String,
    pub group: String,
    pub color: Option<String>,
}

pub fn parse_ssh_config(text: &str) -> Vec<Host> {
    let mut group = "other".to_owned();
    let mut color = None;
    let mut hosts = Vec::new();

    for line in text.lines() {
        let trimmed = line.trim();
        if let Some(rest) = trimmed.strip_prefix('#') {
            let mut fields = rest.split_whitespace();
            if fields.next() == Some("@group")
                && let Some(name) = fields.next()
            {
                group = name.to_owned();
                color = fields
                    .next()
                    .filter(|value| valid_color(value))
                    .map(str::to_owned);
            }
            continue;
        }
        let mut fields = trimmed.split_whitespace();
        if fields
            .next()
            .is_some_and(|word| word.eq_ignore_ascii_case("host"))
        {
            for name in fields.filter(|name| !name.chars().any(|c| "*?!".contains(c))) {
                hosts.push(Host {
                    name: name.to_owned(),
                    group: group.clone(),
                    color: color.clone(),
                });
            }
        }
    }
    hosts
}

fn valid_color(value: &str) -> bool {
    value.len() == 7 && value.starts_with('#') && value[1..].chars().all(|c| c.is_ascii_hexdigit())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_groups_colors_and_skips_wildcards() {
        let hosts = parse_ssh_config(
            "# @group pve #E57000\nHost one two\nHost *.internal\n# @group lucid\nHost three\n",
        );
        assert_eq!(
            hosts,
            vec![
                Host {
                    name: "one".into(),
                    group: "pve".into(),
                    color: Some("#E57000".into())
                },
                Host {
                    name: "two".into(),
                    group: "pve".into(),
                    color: Some("#E57000".into())
                },
                Host {
                    name: "three".into(),
                    group: "lucid".into(),
                    color: None
                },
            ]
        );
    }
}