rustfmt
This commit is contained in:
parent
0e9aac214c
commit
4e9ab9d760
246
src/main.rs
246
src/main.rs
@ -1,11 +1,11 @@
|
|||||||
use doorlib::{self as door, esc, Conn};
|
use doorlib::{self as door, Conn, esc};
|
||||||
use std::error::Error;
|
|
||||||
use rss::Channel;
|
|
||||||
use std::fmt::Write;
|
|
||||||
use textwrap::wrap;
|
|
||||||
use iconv_native::convert;
|
use iconv_native::convert;
|
||||||
|
use rss::Channel;
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fmt::Write;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
|
use textwrap::wrap;
|
||||||
fn report(mut err: &(dyn std::error::Error + 'static)) -> String {
|
fn report(mut err: &(dyn std::error::Error + 'static)) -> String {
|
||||||
let mut s = format!("{}", err);
|
let mut s = format!("{}", err);
|
||||||
while let Some(src) = err.source() {
|
while let Some(src) = err.source() {
|
||||||
@ -15,23 +15,20 @@ fn report(mut err: &(dyn std::error::Error + 'static)) -> String {
|
|||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn xbit_news_feed() -> Result<Channel, Box<dyn Error>> {
|
fn xbit_news_feed() -> Result<Channel, Box<dyn Error>> {
|
||||||
let resp = reqwest::blocking::get("https://x-bit.org/rss/rss.xml");
|
let resp = reqwest::blocking::get("https://x-bit.org/rss/rss.xml");
|
||||||
|
|
||||||
match resp {
|
match resp {
|
||||||
Ok(resp) => {
|
Ok(resp) => {
|
||||||
|
if resp.status().is_success() {
|
||||||
if resp.status().is_success() {
|
|
||||||
|
|
||||||
let content = resp.bytes()?;
|
let content = resp.bytes()?;
|
||||||
let channel = Channel::read_from(&content[..])?;
|
let channel = Channel::read_from(&content[..])?;
|
||||||
return Ok(channel);
|
return Ok(channel);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
report(e.source().expect("REASON"));
|
report(e.source().expect("REASON"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err("Error loading feed".into())
|
Err("Error loading feed".into())
|
||||||
}
|
}
|
||||||
@ -44,21 +41,77 @@ fn door_main(mut u: door::User) -> Result<(), Box<dyn Error>> {
|
|||||||
let mut index = 0;
|
let mut index = 0;
|
||||||
let mut start = 0;
|
let mut start = 0;
|
||||||
|
|
||||||
u.write_str(format!("{}{}{} {}{}{}", esc!("[2J"), esc!("[1;1H"), esc!("[0;30;46m"), channel.title(), esc!("[K"), esc!("[0m")).as_str())?;
|
u.write_str(
|
||||||
u.write_str(format!("{}{} Press Q to quit, Up and Down to Scroll {}{}", esc!("[24;1H"), esc!("[0;30;46m"), esc!("[K"), esc!("[0m")).as_str())?;
|
format!(
|
||||||
|
"{}{}{} {}{}{}",
|
||||||
|
esc!("[2J"),
|
||||||
|
esc!("[1;1H"),
|
||||||
|
esc!("[0;30;46m"),
|
||||||
|
channel.title(),
|
||||||
|
esc!("[K"),
|
||||||
|
esc!("[0m")
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
)?;
|
||||||
|
u.write_str(
|
||||||
|
format!(
|
||||||
|
"{}{} Press Q to quit, Up and Down to Scroll {}{}",
|
||||||
|
esc!("[24;1H"),
|
||||||
|
esc!("[0;30;46m"),
|
||||||
|
esc!("[K"),
|
||||||
|
esc!("[0m")
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
)?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
for i in start..start + 22 {
|
for i in start..start + 22 {
|
||||||
let p = format!("\x1b[{};1H", (i - start) + 2);
|
let p = format!("\x1b[{};1H", (i - start) + 2);
|
||||||
|
|
||||||
if i < channel.items().len() {
|
if i < channel.items().len() {
|
||||||
if i == index {
|
if i == index {
|
||||||
u.write_ln(format!("{}{}{:62.62} {:16.16}{}{}", p, esc!("[1;37;45m"), String::from_utf8(convert(channel.items[i].title().unwrap_or("No Title"), "UTF-8", "ASCII//TRANSLIT").unwrap_or(channel.items[i].title().unwrap_or("No Title").into()))?, channel.items[i].pub_date().unwrap_or("??/??/????"), esc!("[K"), esc!("[0m")).as_str())?;
|
u.write_ln(
|
||||||
} else {
|
format!(
|
||||||
u.write_ln(format!("{}{}{:62.62} {:16.16}{}{}", p, esc!("[1;37m"), String::from_utf8(convert(channel.items[i].title().unwrap_or("No Title"), "UTF-8", "ASCII//TRANSLIT").unwrap_or(channel.items[i].title().unwrap_or("No Title").into()))?, channel.items[i].pub_date().unwrap_or("??/??/????"), esc!("[K"), esc!("[0m")).as_str())?;
|
"{}{}{:62.62} {:16.16}{}{}",
|
||||||
}
|
p,
|
||||||
|
esc!("[1;37;45m"),
|
||||||
|
String::from_utf8(
|
||||||
|
convert(
|
||||||
|
channel.items[i].title().unwrap_or("No Title"),
|
||||||
|
"UTF-8",
|
||||||
|
"ASCII//TRANSLIT"
|
||||||
|
)
|
||||||
|
.unwrap_or(channel.items[i].title().unwrap_or("No Title").into())
|
||||||
|
)?,
|
||||||
|
channel.items[i].pub_date().unwrap_or("??/??/????"),
|
||||||
|
esc!("[K"),
|
||||||
|
esc!("[0m")
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
)?;
|
||||||
|
} else {
|
||||||
|
u.write_ln(
|
||||||
|
format!(
|
||||||
|
"{}{}{:62.62} {:16.16}{}{}",
|
||||||
|
p,
|
||||||
|
esc!("[1;37m"),
|
||||||
|
String::from_utf8(
|
||||||
|
convert(
|
||||||
|
channel.items[i].title().unwrap_or("No Title"),
|
||||||
|
"UTF-8",
|
||||||
|
"ASCII//TRANSLIT"
|
||||||
|
)
|
||||||
|
.unwrap_or(channel.items[i].title().unwrap_or("No Title").into())
|
||||||
|
)?,
|
||||||
|
channel.items[i].pub_date().unwrap_or("??/??/????"),
|
||||||
|
esc!("[K"),
|
||||||
|
esc!("[0m")
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
u.write_ln(format!("{}{}", p, esc!("[K")).as_str())?;
|
u.write_ln(format!("{}{}", p, esc!("[K")).as_str())?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut ch = u.read_byte()?;
|
let mut ch = u.read_byte()?;
|
||||||
@ -74,65 +127,128 @@ fn door_main(mut u: door::User) -> Result<(), Box<dyn Error>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if ch == b'B' {
|
} else if ch == b'B' {
|
||||||
if !channel.items().is_empty() && index < channel.items().len() - 1 {
|
if !channel.items().is_empty() && index < channel.items().len() - 1 {
|
||||||
index += 1;
|
index += 1;
|
||||||
if index >= start + 22 {
|
if index >= start + 22 {
|
||||||
start += 22;
|
start += 22;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if ch == b'q' || ch == b'Q' {
|
} else if ch == b'q' || ch == b'Q' {
|
||||||
break;
|
break;
|
||||||
} else if ch == b'\n' || ch == b'\r' {
|
} else if ch == b'\n' || ch == b'\r' {
|
||||||
let desc = String::from_utf8(convert(channel.items[index].description().unwrap_or("No Content"), "UTF-8", "ASCII//TRANSLIT").unwrap_or(channel.items[index].description().unwrap_or("No Content").into()))?;
|
let desc = String::from_utf8(
|
||||||
let content = wrap(&desc, 79);
|
convert(
|
||||||
|
channel.items[index].description().unwrap_or("No Content"),
|
||||||
|
"UTF-8",
|
||||||
|
"ASCII//TRANSLIT",
|
||||||
|
)
|
||||||
|
.unwrap_or(
|
||||||
|
channel.items[index]
|
||||||
|
.description()
|
||||||
|
.unwrap_or("No Content")
|
||||||
|
.into(),
|
||||||
|
),
|
||||||
|
)?;
|
||||||
|
let content = wrap(&desc, 79);
|
||||||
|
|
||||||
let mut cstart = 0;
|
let mut cstart = 0;
|
||||||
let mut cindex = 0;
|
let mut cindex = 0;
|
||||||
loop {
|
loop {
|
||||||
u.write_ln(format!("{}{}{}{}{}{}", esc!("[2J"), esc!("[1;1H"), esc!("[0;30;46m"), channel.items[index].title().unwrap_or("No Title"), esc!("[K"), esc!("[0m")).as_str())?;
|
u.write_ln(
|
||||||
u.write_ln(format!("{}{}{}{}{}", esc!("[2;1H"), esc!("[0;30;46m"), channel.items[index].link().unwrap_or("No Link"), esc!("[K"), esc!("[0m")).as_str())?;
|
format!(
|
||||||
u.write_str(format!("{}{} Press ENTER to Return to menu, Up and Down to Scroll {}{}", esc!("[24;1H"), esc!("[0;30;46m"), esc!("[K"), esc!("[0m")).as_str())?;
|
"{}{}{}{}{}{}",
|
||||||
for i in cstart..cstart+20 {
|
esc!("[2J"),
|
||||||
let p = format!("\x1b[{};1H", (i - cstart) + 3);
|
esc!("[1;1H"),
|
||||||
if i < content.len() {
|
esc!("[0;30;46m"),
|
||||||
u.write_ln(format!("{}{}{}", p, &content[i], esc!("[K")).as_str())?;
|
channel.items[index].title().unwrap_or("No Title"),
|
||||||
} else {
|
esc!("[K"),
|
||||||
u.write_ln(format!("{}{}", p, esc!("[K")).as_str())?;
|
esc!("[0m")
|
||||||
}
|
)
|
||||||
}
|
.as_str(),
|
||||||
ch = u.read_byte()?;
|
)?;
|
||||||
|
u.write_ln(
|
||||||
|
format!(
|
||||||
|
"{}{}{}{}{}",
|
||||||
|
esc!("[2;1H"),
|
||||||
|
esc!("[0;30;46m"),
|
||||||
|
channel.items[index].link().unwrap_or("No Link"),
|
||||||
|
esc!("[K"),
|
||||||
|
esc!("[0m")
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
)?;
|
||||||
|
u.write_str(
|
||||||
|
format!(
|
||||||
|
"{}{} Press ENTER to Return to menu, Up and Down to Scroll {}{}",
|
||||||
|
esc!("[24;1H"),
|
||||||
|
esc!("[0;30;46m"),
|
||||||
|
esc!("[K"),
|
||||||
|
esc!("[0m")
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
)?;
|
||||||
|
for i in cstart..cstart + 20 {
|
||||||
|
let p = format!("\x1b[{};1H", (i - cstart) + 3);
|
||||||
|
if i < content.len() {
|
||||||
|
u.write_ln(format!("{}{}{}", p, &content[i], esc!("[K")).as_str())?;
|
||||||
|
} else {
|
||||||
|
u.write_ln(format!("{}{}", p, esc!("[K")).as_str())?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ch = u.read_byte()?;
|
||||||
|
|
||||||
if ch == b'\n' || ch == b'\r' || ch == b'q' || ch == b'Q' {
|
if ch == b'\n' || ch == b'\r' || ch == b'q' || ch == b'Q' {
|
||||||
break;
|
break;
|
||||||
} else if ch == b'\x1b' {
|
} else if ch == b'\x1b' {
|
||||||
ch = u.read_byte()?;
|
ch = u.read_byte()?;
|
||||||
if ch == b'[' {
|
if ch == b'[' {
|
||||||
ch = u.read_byte()?;
|
ch = u.read_byte()?;
|
||||||
if ch == b'A' {
|
if ch == b'A' {
|
||||||
if cindex > 0 {
|
if cindex > 0 {
|
||||||
cindex -= 20;
|
cindex -= 20;
|
||||||
if cindex < cstart {
|
if cindex < cstart {
|
||||||
cstart -= 20;
|
cstart -= 20;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if ch == b'B' {
|
} else if ch == b'B' {
|
||||||
if !content.is_empty() && cindex + 20 < content.len() {
|
if !content.is_empty() && cindex + 20 < content.len() {
|
||||||
cindex += 20;
|
cindex += 20;
|
||||||
if cindex >= cstart + 20 {
|
if cindex >= cstart + 20 {
|
||||||
cstart += 20;
|
cstart += 20;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
u.write_str(format!("{}{}{} {}{}{}", esc!("[2J"), esc!("[1;1H"), esc!("[0;30;46m"), String::from_utf8(convert(channel.title(), "UTF-8", "ASCII//TRANSLIT").unwrap_or(channel.title().into()))?, esc!("[K"), esc!("[0m")).as_str())?;
|
|
||||||
u.write_str(format!("{}{} Press Q to quit, Up and Down to Scroll {}{}", esc!("[24;1H"), esc!("[0;30;46m"), esc!("[K"), esc!("[0m")).as_str())?;
|
|
||||||
|
|
||||||
|
u.write_str(
|
||||||
|
format!(
|
||||||
|
"{}{}{} {}{}{}",
|
||||||
|
esc!("[2J"),
|
||||||
|
esc!("[1;1H"),
|
||||||
|
esc!("[0;30;46m"),
|
||||||
|
String::from_utf8(
|
||||||
|
convert(channel.title(), "UTF-8", "ASCII//TRANSLIT")
|
||||||
|
.unwrap_or(channel.title().into())
|
||||||
|
)?,
|
||||||
|
esc!("[K"),
|
||||||
|
esc!("[0m")
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
)?;
|
||||||
|
u.write_str(
|
||||||
|
format!(
|
||||||
|
"{}{} Press Q to quit, Up and Down to Scroll {}{}",
|
||||||
|
esc!("[24;1H"),
|
||||||
|
esc!("[0;30;46m"),
|
||||||
|
esc!("[K"),
|
||||||
|
esc!("[0m")
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user