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

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(())
}