diff --git a/src/accounts.rs b/src/accounts.rs index 7e6f97f..ef94741 100644 --- a/src/accounts.rs +++ b/src/accounts.rs @@ -13,6 +13,7 @@ use serde::{Deserialize, Serialize}; pub struct Account { homeserver: String, id: u32, + name: String, session: Session, sync_token: Option, @@ -31,6 +32,16 @@ pub struct AccountsManager { clients: Vec>, } +impl Account { + pub fn name (&self) -> &String { + &self.name + } + + pub fn user_id(&self) -> String { + self.session.user_id.to_string() + } +} + impl AccountsManager { pub fn new(config:Option) -> Self { return match config { @@ -78,6 +89,7 @@ impl AccountsManager { let account = Account { homeserver: homeserver.to_string(), id, + name: client.account().get_display_name().await?.expect("failed to fetch display name"), session, sync_token: None }; @@ -98,7 +110,7 @@ impl AccountsManager { let account = if account_id >= self.num_accounts { return Err(Error::msg("Invalid account ID")); } else { - self.accounts.get(account_id as usize).expect("Account lookup failed") + self.get(account_id).expect("Account lookup failed") }; @@ -147,4 +159,19 @@ impl AccountsManager { Ok(()) } + + pub fn get(&self, id: u32) -> Option<&Account> { + self.accounts.get(id as usize) + } + + pub fn current(&self) -> Option<&Account> { + self.get(self.current_account) + } + + pub fn client(&self) -> &Option { + match self.clients.get(self.current_account as usize) { + None => &None, + Some(c) => c, + } + } } \ No newline at end of file diff --git a/src/app/mod.rs b/src/app/mod.rs index 83feec2..39cec00 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -52,4 +52,12 @@ impl App { pub fn room(&self) -> Option<&Room> { self.rooms.get(self.current_room_id as usize) } + + pub fn account(&self) -> Result<&Account, ()> { + let account = self.accounts_manager.current(); + match account { + None => Err(()), + Some(a) => Ok(a) + } + } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 00a48c9..88bec91 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,14 +14,14 @@ async fn main() -> anyhow::Result<()> { let mut app = app::App::new(); app.fill_test_data(); + let client = app.accounts_manager.add("https://nerdcult.net", "test", "abcd1234").await?; + let mut ui = UI::new(); ui.draw_main(&app)?; - // let client = app.account_manager.add("https://nerdcult.net", "test", "abcd1234").await?; - // app.client = Some(client); - // let client = app.account_manager.login(0).await?; + // let client = app.accounts_manager.login(0).await?; // app.client = Some(client); Ok(()) diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 4124d01..2762857 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -8,10 +8,11 @@ use crossterm::{ use anyhow::{Error, Result}; use std::io::Stdout; use std::{io, thread, time::Duration}; +use crossterm::style::style; use tui::{backend::CrosstermBackend, layout::{Constraint, Direction, Layout, Rect}, widgets::{Block, Borders, Widget}, Terminal, Frame}; use tui::layout::Alignment; -use tui::style::{Color, Style}; -use tui::text::{Spans, Span}; +use tui::style::{Color, Modifier, Style}; +use tui::text::{Spans, Span, Text}; use tui::widgets::{Paragraph, Wrap}; @@ -26,10 +27,19 @@ fn terminal_prepare() -> Result { return Ok(stdout); } -fn draw_main_status_block(frame: &mut Frame>, area: Rect) { - let panel = Block::default() - .title("Status") - .borders(Borders::ALL); +fn draw_main_status_block(frame: &mut Frame>, area: Rect, app: &App) { + let account = app.accounts_manager.current().expect("failed to resolve current account"); + + let mut content = Text::styled(account.name(), Style::default().add_modifier(Modifier::BOLD)); + content.extend(Text::styled(account.user_id(), Style::default())); + content.extend(Text::styled("settings", Style::default().fg(Color::LightMagenta).add_modifier(Modifier::ITALIC | Modifier::UNDERLINED))); + + let panel = Paragraph::new(content) + .block( + Block::default() + .title("Status") + .borders(Borders::ALL)) + .alignment(Alignment::Left); frame.render_widget(panel, area); } @@ -126,7 +136,7 @@ impl UI { self.terminal.draw(|frame| { - draw_main_status_block(frame, left_chunks[0]); + draw_main_status_block(frame, left_chunks[0], app); draw_main_rooms_block(frame, left_chunks[1]); draw_main_messages_block(frame, middle_chunks[0], app); draw_main_message_compose_block(frame, middle_chunks[1]);