diff --git a/src/config/mod.rs b/src/config/mod.rs index 6fb1519..841640e 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -144,6 +144,7 @@ impl Config { post_history[idx].last_post_url = item.url.clone(); } None => post_history.push(PrevPost { + id: feed.id, title: data.title, last_post_url: item.url.clone(), }), @@ -163,6 +164,7 @@ pub_struct!(RedditConfig { }); pub_struct!(FeedSetting { + id: usize, feed_url: String, communities: FeedCommunities, reddit: FeedRedditSettings, @@ -190,6 +192,7 @@ pub_struct!(FeedRedditSettings { // Posts structs pub_struct!(PrevPost { + id: usize, title: String, last_post_url: String, }); diff --git a/src/main.rs b/src/main.rs index 0b243d3..f8d0297 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,9 +10,9 @@ 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}, layout::{Layout, Constraint, Direction}, Frame, style::{Style, Modifier, Color}}; +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}; +use std::{thread::{sleep, self}, time::{self, Duration}, io, vec}; mod config; @@ -94,12 +94,13 @@ impl Bot { println!("Posting: {}", post.name); self.post(post.clone()); }); + } + pub(crate) fn idle(&self) { while Utc::now().time() - self.start_time.time() < chrono::Duration::seconds(60) { sleep(time::Duration::from_secs(10)); } - - } + } } fn list_posts(auth: &Sensitive, base: String) -> GetPostsResponse { @@ -121,55 +122,85 @@ fn list_posts(auth: &Sensitive, base: String) -> GetPostsResponse { return serde_json::from_str(&res).unwrap(); } -fn run_bot() { +fn run_bot(mut terminal: Terminal>) { // Get all needed auth tokens at the start let mut old = Utc::now().time(); let mut this = Bot::new(); - println!("{}", this.secrets.lemmy.username); this.login(); this.community_ids.load(&this.auth, &this.config.instance); - // Create empty eTag list - println!("TODO: Etag list"); - // Enter a loop (not for debugging) loop { this.run_once(old); + + // Update UI + terminal.draw(|f| { + ui(f, &this); + }).unwrap(); + + thread::sleep(Duration::from_secs(5)); + + disable_raw_mode().unwrap(); + execute!( + terminal.backend_mut(), + LeaveAlternateScreen, + DisableMouseCapture + ).unwrap(); + terminal.show_cursor().unwrap(); + + this.idle(); } } -fn ui(f: &mut Frame) { +fn ui(f: &mut Frame, state: &Bot) { let chunks = Layout::default() .direction(Direction::Vertical) .margin(1) .constraints( [ - Constraint::Percentage(10), + Constraint::Percentage(15), Constraint::Percentage(90), ].as_ref() ) .split(f.size()); + // Account Infos - let block = Block::default() - .title("Block") - .borders(Borders::ALL); + 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", "Last Post"] + 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 = [ - Row::new([Cell::from("a1"), Cell::from("a2"), Cell::from("a3")]).height(1 as u16).bottom_margin(1), - Row::new([Cell::from("b1"), Cell::from("b2"), Cell::from("b3")]).height(1 as u16).bottom_margin(1), - Row::new([Cell::from("unicorn"), Cell::from("c2"), Cell::from("c3")]).height(2 as u16).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) @@ -177,9 +208,11 @@ fn ui(f: &mut Frame) { .highlight_style(selected_style) .highlight_symbol(">> ") .widths(&[ + Constraint::Percentage(30), + Constraint::Percentage(10), + Constraint::Percentage(5), + Constraint::Percentage(5), Constraint::Percentage(50), - Constraint::Length(30), - Constraint::Min(10), ]); f.render_widget(t, chunks[1]); } @@ -187,23 +220,8 @@ fn ui(f: &mut Frame) { fn main() -> Result<(), io::Error> { let stdout = io::stdout(); let backend = CrosstermBackend::new(&stdout); - let mut terminal = Terminal::new(backend)?; + let terminal = Terminal::new(backend)?; - terminal.draw(|f| { - ui(f); - })?; - - thread::sleep(Duration::from_secs(5)); - - disable_raw_mode()?; - execute!( - terminal.backend_mut(), - LeaveAlternateScreen, - DisableMouseCapture - )?; - terminal.show_cursor()?; - - - run_bot(); + run_bot(terminal); Ok(()) }