aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: b4c7781b92532d4294aeb88f2bc14245629dc8d7 (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
use std::io::{self, Write};
use std::io::prelude::*;
use std::net::TcpListener;
use std::process::Command;

fn open_stream() -> io::Result<()> {
    let listener = TcpListener::bind("127.0.0.1:34254")?;

    for stream in listener.incoming() {
        match stream {
            Ok(mut stream) => {
                let mut url = String::new();
                stream.read_to_string(&mut url)?;

                Command::new("open")
                    .arg("-a")
                    .arg("Opera")

                    // Trim the trailing newline, otherwise this doesn't
                    // work
                    .arg(&url.trim_right())
                    .spawn()?;
            }
            Err(e) => {
                write!(io::stderr(), "{}", e)?;
            }
        }
    }

    Ok(())
}

fn main() {
    open_stream().unwrap_or_else(|e| {
        writeln!(io::stderr(), "{}", e)
            .expect("Failed printing to stderr");
    });
}