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
use crate::errors::*;
use std::io::{stderr, stdin, Write};

#[cfg(windows)]
pub fn print_addr_qr(port: u16) -> Result<()> {
    panic!("Not supported for Windows yet")
}

#[cfg(unix)]
pub fn print_addr_qr(port: u16) -> Result<()> {
    eprintln!("Please select a network interface manually:");
    use pnet::datalink::NetworkInterface;
    let interfaces = pnet::datalink::interfaces();
    let options = interfaces.iter().map(|x| {
        (
            &x.name,
            x.ips
                .iter()
                .map(|x| x.to_string())
                .collect::<Vec<_>>()
                .join(", "),
        )
    });
    for x in options.enumerate() {
        println!("{}. {:?}", x.0, x.1);
    }

    eprint!("Input: ");
    stderr().flush()?;

    let input = stdin().lines().next().unwrap().unwrap();
    let input = input.parse::<u8>().map_err(|_| Error::InvalidSelect)?;

    let interface: &NetworkInterface = interfaces
        .get(input as usize)
        .map_or_else(|| Err(Error::InvalidSelect), Ok)?;

    let text = format!("{}:{}", interface.ips[0].ip(), port);
    qr2term::print_qr(text).unwrap();
    Ok(())
}