forked from trinitrix/core
1
0
Fork 0

feature (UI): implemented status block

This commit is contained in:
antifallobst 2023-06-23 16:07:18 +02:00
parent 5afb82a7b4
commit 3a88ca38a0
4 changed files with 56 additions and 11 deletions

View File

@ -13,6 +13,7 @@ use serde::{Deserialize, Serialize};
pub struct Account { pub struct Account {
homeserver: String, homeserver: String,
id: u32, id: u32,
name: String,
session: Session, session: Session,
sync_token: Option<String>, sync_token: Option<String>,
@ -31,6 +32,16 @@ pub struct AccountsManager {
clients: Vec<Option<Client>>, clients: Vec<Option<Client>>,
} }
impl Account {
pub fn name (&self) -> &String {
&self.name
}
pub fn user_id(&self) -> String {
self.session.user_id.to_string()
}
}
impl AccountsManager { impl AccountsManager {
pub fn new(config:Option<String>) -> Self { pub fn new(config:Option<String>) -> Self {
return match config { return match config {
@ -78,6 +89,7 @@ impl AccountsManager {
let account = Account { let account = Account {
homeserver: homeserver.to_string(), homeserver: homeserver.to_string(),
id, id,
name: client.account().get_display_name().await?.expect("failed to fetch display name"),
session, session,
sync_token: None sync_token: None
}; };
@ -98,7 +110,7 @@ impl AccountsManager {
let account = if account_id >= self.num_accounts { let account = if account_id >= self.num_accounts {
return Err(Error::msg("Invalid account ID")); return Err(Error::msg("Invalid account ID"));
} else { } 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(()) 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<Client> {
match self.clients.get(self.current_account as usize) {
None => &None,
Some(c) => c,
}
}
} }

View File

@ -52,4 +52,12 @@ impl App {
pub fn room(&self) -> Option<&Room> { pub fn room(&self) -> Option<&Room> {
self.rooms.get(self.current_room_id as usize) 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)
}
}
} }

View File

@ -14,14 +14,14 @@ async fn main() -> anyhow::Result<()> {
let mut app = app::App::new(); let mut app = app::App::new();
app.fill_test_data(); app.fill_test_data();
let client = app.accounts_manager.add("https://nerdcult.net", "test", "abcd1234").await?;
let mut ui = UI::new(); let mut ui = UI::new();
ui.draw_main(&app)?; 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); // app.client = Some(client);
Ok(()) Ok(())

View File

@ -8,10 +8,11 @@ use crossterm::{
use anyhow::{Error, Result}; use anyhow::{Error, Result};
use std::io::Stdout; use std::io::Stdout;
use std::{io, thread, time::Duration}; 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::{backend::CrosstermBackend, layout::{Constraint, Direction, Layout, Rect}, widgets::{Block, Borders, Widget}, Terminal, Frame};
use tui::layout::Alignment; use tui::layout::Alignment;
use tui::style::{Color, Style}; use tui::style::{Color, Modifier, Style};
use tui::text::{Spans, Span}; use tui::text::{Spans, Span, Text};
use tui::widgets::{Paragraph, Wrap}; use tui::widgets::{Paragraph, Wrap};
@ -26,10 +27,19 @@ fn terminal_prepare() -> Result<Stdout> {
return Ok(stdout); return Ok(stdout);
} }
fn draw_main_status_block(frame: &mut Frame<CrosstermBackend<Stdout>>, area: Rect) { fn draw_main_status_block(frame: &mut Frame<CrosstermBackend<Stdout>>, area: Rect, app: &App) {
let panel = Block::default() 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") .title("Status")
.borders(Borders::ALL); .borders(Borders::ALL))
.alignment(Alignment::Left);
frame.render_widget(panel, area); frame.render_widget(panel, area);
} }
@ -126,7 +136,7 @@ impl UI {
self.terminal.draw(|frame| { 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_rooms_block(frame, left_chunks[1]);
draw_main_messages_block(frame, middle_chunks[0], app); draw_main_messages_block(frame, middle_chunks[0], app);
draw_main_message_compose_block(frame, middle_chunks[1]); draw_main_message_compose_block(frame, middle_chunks[1]);