Compare commits

..

No commits in common. "e17a96d34dcc8c1a4f0aa368bc5a26fef9a7761f" and "c5742e66f4bd0ea50f4406c50dbe3628d988418c" have entirely different histories.

3 changed files with 35 additions and 174 deletions

View File

@ -5,21 +5,9 @@ use matrix_sdk::{Client};
use accounts::Account;
use accounts::AccountsManager;
pub struct Message {
pub author: String,
pub message: String,
}
pub struct Room {
pub messages: Vec<Message>,
}
pub struct App {
pub accounts_manager: accounts::AccountsManager,
pub client: Option<Client>,
current_room_id: u32,
rooms: Vec<Room>,
}
impl App {
@ -32,24 +20,8 @@ impl App {
};
Self {
accounts_manager: AccountManager::new(),
client: None,
current_room_id: 0,
rooms: Vec::new(),
accounts_manager: AccountsManager::new(config),
client: None
}
}
pub fn fill_test_data(&mut self) {
let mut room = Room {
messages: Vec::new()
};
room.messages.push(Message {author: "someone".to_string(), message: "test".to_string()});
self.rooms.push(room);
}
pub fn room(&self) -> Option<&Room> {
self.rooms.get(self.current_room_id as usize)
}
}

View File

@ -4,25 +4,16 @@ mod app;
use matrix_sdk::ruma::exports::serde_json;
use tokio::time::{sleep, Duration};
use crate::app::Message;
use crate::ui::UI;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let mut app = app::App::new();
app.fill_test_data();
let mut ui = UI::new();
ui.draw_main(&app)?;
let account = app.accounts_manager.add("https://nerdcult.net", "test", "abcd1234").await?;
// let client = app.account_manager.add("https://nerdcult.net", "test", "abcd1234").await?;
// app.client = Some(client);
// let client = app.account_manager.login(0).await?;
// app.client = Some(client);
app.accounts_manager.login(0).await?;
Ok(())
}

View File

@ -1,150 +1,48 @@
use crate::app::{App};
use crate::ui;
use crossterm::event::KeyCode::Null;
use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use anyhow::{Error, Result};
use std::io::Stdout;
use std::{io, thread, time::Duration};
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::widgets::{Paragraph, Wrap};
use tui::{
backend::CrosstermBackend,
layout::{Constraint, Direction, Layout},
widgets::{Block, Borders, Widget},
Terminal,
};
pub struct UI {
backend: CrosstermBackend<Stdout>,
terminal: Terminal<CrosstermBackend<Stdout>>,
}
fn terminal_prepare() -> Result<Stdout> {
pub fn ui_create() -> Result<(), io::Error> {
// setup terminal
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
return Ok(stdout);
}
fn draw_main_status_block(frame: &mut Frame<CrosstermBackend<Stdout>>, area: Rect) {
let panel = Block::default()
.title("Status")
.borders(Borders::ALL);
frame.render_widget(panel, area);
}
fn draw_main_rooms_block(frame: &mut Frame<CrosstermBackend<Stdout>>, area: Rect) {
let panel = Block::default()
.title("Rooms")
.borders(Borders::ALL);
frame.render_widget(panel, area);
}
fn draw_main_messages_block(frame: &mut Frame<CrosstermBackend<Stdout>>, area: Rect, app: &App) {
let messages = match app.room() {
None => return,
Some(r) => {
r.messages
.iter()
.rev()
.map(|msg| {
Spans::from(vec![
Span::styled(&msg.author, Style::default().fg(Color::Cyan)),
Span::styled(": ", Style::default().fg(Color::Cyan)),
Span::styled(&msg.message, Style::default().fg(Color::White)),
])
})
.collect::<Vec<_>>()
},
};
let panel = Paragraph::new(messages)
.block(
Block::default()
.title("Messages")
.borders(Borders::ALL))
.alignment(Alignment::Left)
.wrap(Wrap {trim: false});
frame.render_widget(panel, area);
}
fn draw_main_message_compose_block(frame: &mut Frame<CrosstermBackend<Stdout>>, area: Rect) {
let panel = Block::default()
.title("Message Compose")
.borders(Borders::ALL);
frame.render_widget(panel, area);
}
fn draw_main_room_info_block(frame: &mut Frame<CrosstermBackend<Stdout>>, area: Rect) {
let panel = Block::default()
.title("Room Info")
.borders(Borders::ALL);
frame.render_widget(panel, area);
}
impl UI {
pub fn new() -> Self {
let stdout = terminal_prepare().expect("failed to prepare terminal");
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend).expect("failed to initialize terminal");
terminal.clear().expect("failed to clear screen");
Self {
terminal
}
}
pub fn draw_main(&mut self, app: &App) -> Result<()> {
let chunk = self.terminal.size()?;
let main_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Length(32), Constraint::Min(16), Constraint::Length(32)].as_ref())
.split(chunk);
let left_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(5), Constraint::Min(4)].as_ref())
.split(main_chunks[0]);
let middle_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(4), Constraint::Length(3)].as_ref())
.split(main_chunks[1]);
let right_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(4)].as_ref())
.split(main_chunks[2]);
self.terminal.draw(|frame| {
draw_main_status_block(frame, left_chunks[0]);
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]);
draw_main_room_info_block(frame, right_chunks[0]);
})?;
thread::sleep(Duration::from_millis(10000));
Ok(())
}
pub fn close(&mut self) -> Result<()>{
disable_raw_mode()?;
execute!(
self.terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
self.terminal.show_cursor()?;
Ok(())
}
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
terminal.draw(|f| {
let size = f.size();
let block = Block::default().title("Layout design in work...").borders(Borders::ALL);
f.render_widget(block, size);
})?;
thread::sleep(Duration::from_millis(5000));
// restore terminal
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
terminal.show_cursor()?;
Ok(())
}