Removed TUI due to instability

This commit is contained in:
Neshura 2023-07-29 02:32:58 +02:00
parent 054433053b
commit 649d575324
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
3 changed files with 18 additions and 188 deletions

95
Cargo.lock generated
View file

@ -293,7 +293,6 @@ name = "ascendance-of-a-bookworm-bot"
version = "1.1.0"
dependencies = [
"chrono",
"crossterm 0.26.1",
"lemmy_api_common",
"lemmy_db_schema",
"once_cell",
@ -302,7 +301,6 @@ dependencies = [
"serde_derive",
"serde_json",
"strum_macros 0.25.0",
"tui",
"url",
]
@ -407,12 +405,6 @@ dependencies = [
"bytes",
]
[[package]]
name = "cassowary"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53"
[[package]]
name = "cc"
version = "1.0.79"
@ -484,47 +476,6 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "crossterm"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e64e6c0fbe2c17357405f7c758c1ef960fce08bdfb2c03d88d2a18d7e09c4b67"
dependencies = [
"bitflags",
"crossterm_winapi",
"libc",
"mio",
"parking_lot",
"signal-hook",
"signal-hook-mio",
"winapi",
]
[[package]]
name = "crossterm"
version = "0.26.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a84cda67535339806297f1b331d6dd6320470d2a0fe65381e79ee9e156dd3d13"
dependencies = [
"bitflags",
"crossterm_winapi",
"libc",
"mio",
"parking_lot",
"signal-hook",
"signal-hook-mio",
"winapi",
]
[[package]]
name = "crossterm_winapi"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b"
dependencies = [
"winapi",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
@ -1744,27 +1695,6 @@ dependencies = [
"digest",
]
[[package]]
name = "signal-hook"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "732768f1176d21d09e076c23a93123d40bba92d50c4058da34d45c8de8e682b9"
dependencies = [
"libc",
"signal-hook-registry",
]
[[package]]
name = "signal-hook-mio"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af"
dependencies = [
"libc",
"mio",
"signal-hook",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.1"
@ -2122,19 +2052,6 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed"
[[package]]
name = "tui"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccdd26cbd674007e649a272da4475fb666d3aa0ad0531da7136db6fab0e5bad1"
dependencies = [
"bitflags",
"cassowary",
"crossterm 0.25.0",
"unicode-segmentation",
"unicode-width",
]
[[package]]
name = "typed-builder"
version = "0.10.0"
@ -2173,18 +2090,6 @@ dependencies = [
"tinyvec",
]
[[package]]
name = "unicode-segmentation"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
[[package]]
name = "unicode-width"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]]
name = "untrusted"
version = "0.7.1"

View file

@ -7,7 +7,6 @@ edition = "2021"
[dependencies]
chrono = "0.4.26"
crossterm = "0.26.1"
lemmy_api_common = "0.18.1"
lemmy_db_schema = "0.18.1"
once_cell = "1.18.0"
@ -16,5 +15,4 @@ serde = "1.0.164"
serde_derive = "1.0.164"
serde_json = "1.0.97"
strum_macros = "0.25.0"
tui = "0.19.0"
url = "2.4.0"

View file

@ -1,4 +1,4 @@
use chrono::{Utc, DateTime, NaiveTime};
use chrono::{Utc, DateTime, NaiveTime, FixedOffset, TimeZone, NaiveDate, NaiveDateTime};
use config::{Config, PrevPost, Secrets, CommunitiesVector};
use lemmy_api_common::{
person::{Login, LoginResponse},
@ -10,9 +10,7 @@ use lemmy_db_schema::{
};
use once_cell::sync::Lazy;
use reqwest::{blocking::Client, StatusCode};
use tui::{backend::{CrosstermBackend, Backend}, Terminal, widgets::{Block, Borders, Cell, Row, Table, Paragraph, Wrap}, layout::{Layout, Constraint, Direction, Alignment}, Frame, style::{Style, Modifier, Color}, text::{Span, Spans}};
use crossterm::{event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, execute, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},};
use std::{thread::{sleep, self}, time::{self, Duration}, io, vec};
use std::{thread::{sleep, self}, time::{self, Duration}, io, vec, str::FromStr};
mod config;
@ -102,6 +100,19 @@ impl Bot {
sleep(time::Duration::from_secs(10));
}
}
pub(crate) fn print_info(&self) {
print!("\x1B[2J\x1B[1;1H");
println!("##[Ascendance of a Bookworm Bot]##");
println!("Instance: {}", &self.config.instance);
println!("Ran Last: {}", &self.start_time.format("%d/%m/%Y %H:%M:%S"));
println!("{:#<1$}", "", 30);
self.post_history.iter().for_each(|post| {
print!("{} ", post.title);
print!("{:<1$}: ", "", 60 - post.title.len());
println!("{}", post.last_post_url);
})
}
}
fn list_posts(auth: &Sensitive<String>, base: String) -> GetPostsResponse {
@ -123,7 +134,7 @@ fn list_posts(auth: &Sensitive<String>, base: String) -> GetPostsResponse {
return serde_json::from_str(&res).unwrap();
}
fn run_bot(mut terminal: Terminal<CrosstermBackend<&io::Stdout>>) {
fn run_bot() {
// Get all needed auth tokens at the start
let mut old = Utc::now().time();
let mut this = Bot::new();
@ -132,96 +143,12 @@ fn run_bot(mut terminal: Terminal<CrosstermBackend<&io::Stdout>>) {
// Enter a loop (not for debugging)
loop {
let _ = enable_raw_mode();
this.run_once(old);
// Update UI
terminal.draw(|f| {
ui(f, &this);
}).unwrap();
disable_raw_mode().unwrap();
terminal.show_cursor().unwrap();
this.print_info();
this.idle();
}
}
fn ui<B: Backend>(f: &mut Frame<B>, state: &Bot) {
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(1)
.constraints(
[
Constraint::Percentage(15),
Constraint::Percentage(90),
].as_ref()
)
.split(f.size());
// Account Infos
let account_info = vec![
Row::new(vec![
Cell::from(format!("Lemmy Username: {} |", state.secrets.lemmy.username)),
Cell::from(format!("Lemmy Instance: {} |", state.config.instance))
])
];
let block = Table::new(account_info)
.block(Block::default().title("Account info").borders(Borders::ALL))
.widths(&[
Constraint::Percentage(15),
Constraint::Percentage(20),
Constraint::Percentage(20),
Constraint::Percentage(20),
Constraint::Percentage(20),
]);
f.render_widget(block, chunks[0]);
// Post Table
let selected_style = Style::default().add_modifier(Modifier::REVERSED);
let normal_style = Style::default().bg(Color::Blue);
let header_cells = ["Series", "Community", "Reddit", "Discord", "Last Post"]
.iter()
.map(|h| Cell::from(*h).style(Style::default().fg(Color::Red)));
let header = Row::new(header_cells)
.style(normal_style)
.height(1)
.bottom_margin(1);
let rows = state.post_history.iter().map(|post| {
let config = &state.config.feeds[post.id];
Row::new([Cell::from(post.title.clone()), Cell::from(format!("{}", config.communities.chapter)), Cell::from(format!("{}", config.reddit.enabled as u8)), Cell::from("0"), Cell::from(post.last_post_url.clone())]).height(1 as u16).bottom_margin(1)
});
let t = Table::new(rows)
.header(header)
.block(Block::default().borders(Borders::ALL).title("Table"))
.highlight_style(selected_style)
.highlight_symbol(">> ")
.widths(&[
Constraint::Percentage(30),
Constraint::Percentage(10),
Constraint::Percentage(5),
Constraint::Percentage(5),
Constraint::Percentage(50),
]);
f.render_widget(t, chunks[1]);
}
fn main() -> Result<(), io::Error> {
let stdout = io::stdout();
let backend = CrosstermBackend::new(&stdout);
let terminal = Terminal::new(backend)?;
run_bot(terminal);
run_bot();
Ok(())
}