|
|
|
@ -10,22 +10,14 @@ use termion::input::TermRead;
|
|
|
|
|
use termion::raw::IntoRawMode;
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
phetch("phkt.io", 70, "/");
|
|
|
|
|
let response = phetch("phkt.io", 70, "/links");
|
|
|
|
|
render(&response);
|
|
|
|
|
user_input();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn phetch(host: &str, port: i8, selector: &str) {
|
|
|
|
|
fn user_input() {
|
|
|
|
|
let stdin = stdin();
|
|
|
|
|
let mut stdout = stdout().into_raw_mode().unwrap();
|
|
|
|
|
TcpStream::connect(format!("{}:{}", host, port))
|
|
|
|
|
.and_then(|mut stream| {
|
|
|
|
|
stream.write(format!("{}\r\n", selector).as_ref());
|
|
|
|
|
Ok(stream)
|
|
|
|
|
})
|
|
|
|
|
.and_then(|mut stream| {
|
|
|
|
|
let mut buf = String::new();
|
|
|
|
|
stream.read_to_string(&mut buf);
|
|
|
|
|
render(&buf);
|
|
|
|
|
|
|
|
|
|
let mut y = 1;
|
|
|
|
|
if let Ok((_col, row)) = termion::terminal_size() {
|
|
|
|
|
y = row + 1;
|
|
|
|
@ -58,12 +50,23 @@ fn phetch(host: &str, port: i8, selector: &str) {
|
|
|
|
|
// Flush again.
|
|
|
|
|
stdout.flush().unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn phetch(host: &str, port: i8, selector: &str) -> String {
|
|
|
|
|
let mut out = String::new();
|
|
|
|
|
TcpStream::connect(format!("{}:{}", host, port))
|
|
|
|
|
.and_then(|mut stream| {
|
|
|
|
|
stream.write(format!("{}\r\n", selector).as_ref());
|
|
|
|
|
Ok(stream)
|
|
|
|
|
})
|
|
|
|
|
.and_then(|mut stream| {
|
|
|
|
|
stream.read_to_string(&mut out);
|
|
|
|
|
Ok(())
|
|
|
|
|
})
|
|
|
|
|
.map_err(|err| {
|
|
|
|
|
eprintln!("err: {}", err);
|
|
|
|
|
});
|
|
|
|
|
out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render(buf: &str) {
|
|
|
|
@ -75,9 +78,9 @@ fn draw(buf: &str) -> String {
|
|
|
|
|
let mut skip_to_end = false;
|
|
|
|
|
let mut links = 0;
|
|
|
|
|
let mut out = String::with_capacity(buf.len() * 2);
|
|
|
|
|
let mut prefix: &str;
|
|
|
|
|
let mut prefix = "";
|
|
|
|
|
for (i, c) in buf.chars().enumerate() {
|
|
|
|
|
let mut is_link = false;
|
|
|
|
|
for c in buf.chars() {
|
|
|
|
|
if start {
|
|
|
|
|
match c {
|
|
|
|
|
'i' => {
|
|
|
|
@ -85,26 +88,40 @@ fn draw(buf: &str) -> String {
|
|
|
|
|
is_link = false;
|
|
|
|
|
}
|
|
|
|
|
'h' => {
|
|
|
|
|
prefix = "\x1B[94m";
|
|
|
|
|
prefix = "\x1B[96m";
|
|
|
|
|
links += 1;
|
|
|
|
|
is_link = true;
|
|
|
|
|
}
|
|
|
|
|
'0' => {
|
|
|
|
|
prefix = "\x1B[95m";
|
|
|
|
|
prefix = "\x1B[94m";
|
|
|
|
|
links += 1;
|
|
|
|
|
is_link = true;
|
|
|
|
|
}
|
|
|
|
|
'1' => {
|
|
|
|
|
prefix = "\x1B[96m";
|
|
|
|
|
prefix = "\x1B[94m";
|
|
|
|
|
links += 1;
|
|
|
|
|
is_link = true;
|
|
|
|
|
}
|
|
|
|
|
'.' => {
|
|
|
|
|
if buf.len() > i + 2
|
|
|
|
|
&& buf[i..].chars().next().unwrap() == '\r'
|
|
|
|
|
&& buf[i + 1..].chars().next().unwrap() == '\n'
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
'\r' => continue,
|
|
|
|
|
'\n' => continue,
|
|
|
|
|
_ => prefix = "",
|
|
|
|
|
}
|
|
|
|
|
out.push_str(" ");
|
|
|
|
|
if is_link {
|
|
|
|
|
out.push_str("\x1B[95m");
|
|
|
|
|
if links < 10 {
|
|
|
|
|
out.push(' ');
|
|
|
|
|
}
|
|
|
|
|
out.push_str(&links.to_string());
|
|
|
|
|
out.push_str(". ");
|
|
|
|
|
out.push_str(". \x1B[0m");
|
|
|
|
|
} else {
|
|
|
|
|
out.push(' ');
|
|
|
|
|
out.push_str("\x1B[0m");
|
|
|
|
|