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 {
homeserver: String,
id: u32,
name: String,
session: Session,
sync_token: Option<String>,
@ -31,6 +32,16 @@ pub struct AccountsManager {
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 {
pub fn new(config:Option<String>) -> 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<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> {
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();
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(())

View File

@ -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<Stdout> {
return Ok(stdout);
}
fn draw_main_status_block(frame: &mut Frame<CrosstermBackend<Stdout>>, area: Rect) {
let panel = Block::default()
.title("Status")
.borders(Borders::ALL);
fn draw_main_status_block(frame: &mut Frame<CrosstermBackend<Stdout>>, 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]);