aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs46
1 files changed, 3 insertions, 43 deletions
diff --git a/src/main.rs b/src/main.rs
index fcaca7a..937ec95 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,54 +1,14 @@
extern crate getopts;
-extern crate url;
+
+extern crate hearurl;
use getopts::Options;
-use url::Url;
use std::env;
use std::io::{self, Write};
-use std::io::prelude::*;
-use std::net::TcpListener;
-use std::process::Command;
const DEFAULT_PORT: u16 = 37705;
-fn open_stream(browser: String, port: u16) -> io::Result<()> {
- let listener = TcpListener::bind(
- format!("127.0.0.1:{}", port)
- )?;
-
- for stream in listener.incoming() {
- match stream {
- Ok(mut stream) => {
- let mut url = String::new();
- match stream.read_to_string(&mut url) {
- Ok(_) => {},
- Err(e) => writeln!(io::stderr(), "{}", e)?,
- };
-
- match Url::parse(url.as_str()) {
- Ok(url) => {
- match Command::new("open")
- .arg("-a")
- .arg(&browser)
- .arg(&url.as_str())
- .spawn() {
- Ok(_) => {},
- Err(e) => writeln!(io::stderr(), "{}", e)?,
- };
- },
- Err(e) => writeln!(io::stderr(), "{}", e)?,
- };
- }
- Err(e) => {
- writeln!(io::stderr(), "{}", e)?;
- }
- }
- }
-
- Ok(())
-}
-
fn print_usage(opts: Options) {
let brief = "Usage: hearurl -b BROWSER";
print!("{}", opts.usage(&brief));
@@ -85,7 +45,7 @@ fn main() {
None => DEFAULT_PORT,
};
- open_stream(browser, port).unwrap_or_else(|e| {
+ hearurl::open_stream(browser, port).unwrap_or_else(|e| {
writeln!(io::stderr(), "{}", e)
.expect("Failed printing to stderr");
});