Feat(ui): Add status panel, which shows command statuses and errors

This commit is contained in:
Benedikt Peetz 2023-07-24 23:53:21 +02:00
parent d46a0c3a24
commit fe3849a7b7
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
8 changed files with 279 additions and 142 deletions

View File

@ -2,6 +2,7 @@ use cli_log::debug;
#[derive(Debug)] #[derive(Debug)]
pub enum Command { pub enum Command {
RaiseError(String),
Greet(String), Greet(String),
Exit, Exit,
CommandLineShow, CommandLineShow,

View File

@ -1,66 +1,105 @@
use crate::app::{command_interface::Command, events::event_types::EventStatus, App}; use crate::app::{command_interface::Command, events::event_types::EventStatus, App};
use anyhow::Result; use anyhow::{Context, Result};
use cli_log::info; use cli_log::{info, warn, trace};
use tokio::sync::mpsc;
pub async fn handle( pub async fn handle(
app: &mut App<'_>, app: &mut App<'_>,
command: &Command, command: &Command,
send_output: bool, output_callback: &Option<mpsc::Sender<String>>,
) -> Result<(EventStatus, String)> { ) -> Result<EventStatus> {
macro_rules! set_status_output { // A command returns both _status output_ (what you would normally print to stderr)
// and _main output_ (the output which is normally printed to stdout).
// We simulate these by returning the main output to the lua function, and printing the
// status output to a status ui field.
//
// Every function should return some status output to show the user, that something is
// happening, while only some functions return some value to the main output, as this
// is reserved for functions called only for their output (for example `greet()`).
macro_rules! send_status_output {
($str:expr) => { ($str:expr) => {
if send_output { app.status.add_status_message($str.to_owned());
app.ui.set_command_output($str); };
($str:expr, $($args:ident),+) => {
app.status.add_status_message(format!($str, $($args),+));
};
}
macro_rules! send_error_output {
($str:expr) => {
app.status.add_error_message($str.to_owned());
};
($str:expr, $($args:ident),+) => {
app.status.add_error_message(format!($str, $($args),+));
};
}
macro_rules! send_main_output {
($str:expr) => {
if let Some(sender) = output_callback {
sender
.send($str.to_owned())
.await
.context("Failed to send command main output")?;
} }
}; };
($str:expr, $($args:ident),+) => { ($str:expr, $($args:ident),+) => {
if send_output { if let Some(sender) = output_callback {
app.ui.set_command_output(&format!($str, $($args),+)); sender
.send(format!($str, $($args),+))
.await
.context("Failed to send command main output")?;
} }
}; };
} }
info!("Handling command: {:#?}", command);
trace!("Handling command: {:#?}", command);
Ok(match command { Ok(match command {
Command::Exit => ( Command::Exit => {
EventStatus::Terminate, send_status_output!("Terminating the application..");
"Terminated the application".to_owned(), EventStatus::Terminate
), }
Command::CommandLineShow => { Command::CommandLineShow => {
app.ui.cli_enable(); app.ui.cli_enable();
set_status_output!("CLI online"); send_status_output!("CLI online");
(EventStatus::Ok, "".to_owned()) EventStatus::Ok
} }
Command::CommandLineHide => { Command::CommandLineHide => {
app.ui.cli_disable(); app.ui.cli_disable();
set_status_output!("CLI offline"); send_status_output!("CLI offline");
(EventStatus::Ok, "".to_owned()) EventStatus::Ok
} }
Command::CyclePlanes => { Command::CyclePlanes => {
app.ui.cycle_main_input_position(); app.ui.cycle_main_input_position();
set_status_output!("Switched main input position"); send_status_output!("Switched main input position");
(EventStatus::Ok, "".to_owned()) EventStatus::Ok
} }
Command::CyclePlanesRev => { Command::CyclePlanesRev => {
app.ui.cycle_main_input_position_rev(); app.ui.cycle_main_input_position_rev();
set_status_output!("Switched main input position; reversed"); send_status_output!("Switched main input position; reversed");
(EventStatus::Ok, "".to_owned()) EventStatus::Ok
} }
Command::RoomMessageSend(msg) => { Command::RoomMessageSend(msg) => {
if let Some(room) = app.status.room_mut() { if let Some(room) = app.status.room_mut() {
room.send(msg.clone()).await?; room.send(msg.clone()).await?;
} else {
// TODO(@Soispha): Should this raise a lua error? It could be very confusing,
// when a user doesn't read the log.
warn!("Can't send message: `{}`, as there is no open room!", &msg);
} }
set_status_output!("Send message: `{}`", msg); send_status_output!("Send message: `{}`", msg);
(EventStatus::Ok, "".to_owned()) EventStatus::Ok
} }
Command::Greet(name) => { Command::Greet(name) => {
info!("Greated {}", name); send_main_output!("Hi, {}!", name);
set_status_output!("Hi, {}!", name); EventStatus::Ok
(EventStatus::Ok, "".to_owned())
} }
Command::Help(_) => todo!(), Command::Help(_) => todo!(),
Command::RaiseError(err) => {
send_error_output!(err);
EventStatus::Ok
},
}) })
} }

View File

@ -1,7 +1,7 @@
mod handlers; mod handlers;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use cli_log::{info, trace}; use cli_log::trace;
use crossterm::event::Event as CrosstermEvent; use crossterm::event::Event as CrosstermEvent;
use tokio::sync::mpsc::Sender; use tokio::sync::mpsc::Sender;
@ -27,22 +27,12 @@ impl Event {
.await .await
.with_context(|| format!("Failed to handle matrix event: `{:#?}`", event)), .with_context(|| format!("Failed to handle matrix event: `{:#?}`", event)),
Event::CommandEvent(event, callback_tx) => { Event::CommandEvent(event, callback_tx) => command::handle(app, event, callback_tx)
let (result, output) = command::handle(app, event, callback_tx.is_some()) .await
.await .with_context(|| format!("Failed to handle command event: `{:#?}`", event)),
.with_context(|| format!("Failed to handle command event: `{:#?}`", event))?;
if let Some(callback_tx) = callback_tx {
callback_tx
.send(output.clone())
.await
.with_context(|| format!("Failed to send command output: {}", output))?;
}
Ok(result)
}
Event::LuaCommand(lua_code) => lua_command::handle(app, lua_code.to_owned()) Event::LuaCommand(lua_code) => lua_command::handle(app, lua_code.to_owned())
.await .await
.with_context(|| format!("Failed to handle lua code: `{:#?}`", lua_code)), .with_context(|| format!("Failed to handle lua code: `{}`", lua_code)),
Event::InputEvent(event) => match app.status.state() { Event::InputEvent(event) => match app.status.state() {
State::None => unreachable!( State::None => unreachable!(

View File

@ -25,6 +25,19 @@ pub struct Room {
view_scroll: Option<usize>, view_scroll: Option<usize>,
} }
pub struct StatusMessage {
content: String,
is_error: bool,
}
impl StatusMessage {
pub fn content(&self) -> String {
self.content.clone()
}
pub fn is_error(&self) -> bool {
self.is_error
}
}
pub struct Status { pub struct Status {
state: State, state: State,
account_name: String, account_name: String,
@ -33,6 +46,7 @@ pub struct Status {
client: Option<Client>, client: Option<Client>,
rooms: IndexMap<String, Room>, rooms: IndexMap<String, Room>,
current_room_id: String, current_room_id: String,
status_messages: Vec<StatusMessage>,
} }
impl Room { impl Room {
@ -125,14 +139,40 @@ impl Status {
Self { Self {
state: State::None, state: State::None,
account_name: "".to_string(), account_name: "".to_owned(),
account_user_id: "".to_string(), account_user_id: "".to_owned(),
client, client,
rooms, rooms,
current_room_id: "".to_string(), current_room_id: "".to_owned(),
status_messages: vec![StatusMessage {
content: "Initialized!".to_owned(),
is_error: false,
}],
} }
} }
pub fn add_status_message(&mut self, msg: String) {
// TODO(@Soispha): This could allocate a lot of ram, when we don't
// add a limit to the messages.
// This needs to be proven.
self.status_messages.push(StatusMessage {
content: msg,
is_error: false,
})
}
pub fn add_error_message(&mut self, msg: String) {
// TODO(@Soispha): This could allocate a lot of ram, when we don't
// add a limit to the messages.
// This needs to be proven.
self.status_messages.push(StatusMessage {
content: msg,
is_error: true,
})
}
pub fn status_messages(&self) -> &Vec<StatusMessage> {
&self.status_messages
}
pub fn account_name(&self) -> &String { pub fn account_name(&self) -> &String {
&self.account_name &self.account_name
} }

View File

@ -11,12 +11,13 @@ use crossterm::{
}; };
use tui::{ use tui::{
backend::CrosstermBackend, backend::CrosstermBackend,
style::Color,
widgets::{Block, Borders, ListState}, widgets::{Block, Borders, ListState},
Terminal, Terminal,
}; };
use tui_textarea::TextArea; use tui_textarea::TextArea;
use crate::ui::terminal_prepare; use crate::ui::{terminal_prepare, textarea_inactivate, textarea_activate};
use super::setup; use super::setup;
@ -28,10 +29,121 @@ pub enum InputPosition {
Rooms, Rooms,
Messages, Messages,
MessageCompose, MessageCompose,
CommandMonitor,
RoomInfo, RoomInfo,
CLI, CLI,
} }
impl InputPosition {
// calculate to widgets colors, based of which widget is currently selected
pub fn colors(
&self,
mut cli: &mut Option<TextArea<'_>>,
mut message_compose: &mut TextArea<'_>,
) -> Vec<Color> {
match self {
InputPosition::Status => {
textarea_inactivate(&mut message_compose);
if let Some(cli) = &mut cli {
textarea_inactivate(cli);
}
vec![
Color::White,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
]
}
InputPosition::Rooms => {
textarea_inactivate(&mut message_compose);
if let Some(cli) = &mut cli {
textarea_inactivate(cli);
}
vec![
Color::DarkGray,
Color::White,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
]
}
InputPosition::Messages => {
textarea_inactivate(&mut message_compose);
if let Some(cli) = &mut cli {
textarea_inactivate(cli);
}
vec![
Color::DarkGray,
Color::DarkGray,
Color::White,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
]
}
InputPosition::MessageCompose => {
textarea_activate(&mut message_compose);
if let Some(cli) = &mut cli {
textarea_inactivate(cli);
}
vec![
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
]
}
InputPosition::RoomInfo => {
textarea_inactivate(&mut message_compose);
if let Some(cli) = &mut cli {
textarea_inactivate(cli);
}
vec![
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::White,
Color::DarkGray,
]
}
InputPosition::CLI => {
textarea_inactivate(&mut message_compose);
if let Some(cli) = &mut cli {
textarea_activate(cli);
}
vec![
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
]
}
InputPosition::CommandMonitor => {
textarea_inactivate(&mut message_compose);
if let Some(cli) = &mut cli {
textarea_inactivate(cli);
}
vec![
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::White,
]
}
}
}
}
pub struct UI<'a> { pub struct UI<'a> {
terminal: Terminal<CrosstermBackend<Stdout>>, terminal: Terminal<CrosstermBackend<Stdout>>,
input_position: InputPosition, input_position: InputPosition,
@ -89,7 +201,8 @@ impl UI<'_> {
InputPosition::Status => InputPosition::Rooms, InputPosition::Status => InputPosition::Rooms,
InputPosition::Rooms => InputPosition::Messages, InputPosition::Rooms => InputPosition::Messages,
InputPosition::Messages => InputPosition::MessageCompose, InputPosition::Messages => InputPosition::MessageCompose,
InputPosition::MessageCompose => InputPosition::RoomInfo, InputPosition::MessageCompose => InputPosition::CommandMonitor,
InputPosition::CommandMonitor => InputPosition::RoomInfo,
InputPosition::RoomInfo => match self.cli { InputPosition::RoomInfo => match self.cli {
Some(_) => InputPosition::CLI, Some(_) => InputPosition::CLI,
None => InputPosition::Status, None => InputPosition::Status,
@ -107,7 +220,8 @@ impl UI<'_> {
InputPosition::Rooms => InputPosition::Status, InputPosition::Rooms => InputPosition::Status,
InputPosition::Messages => InputPosition::Rooms, InputPosition::Messages => InputPosition::Rooms,
InputPosition::MessageCompose => InputPosition::Messages, InputPosition::MessageCompose => InputPosition::Messages,
InputPosition::RoomInfo => InputPosition::MessageCompose, InputPosition::RoomInfo => InputPosition::CommandMonitor,
InputPosition::CommandMonitor => InputPosition::MessageCompose,
InputPosition::CLI => InputPosition::RoomInfo, InputPosition::CLI => InputPosition::RoomInfo,
}; };
} }
@ -153,12 +267,12 @@ impl UI<'_> {
} }
pub async fn update_setup(&mut self) -> Result<()> { pub async fn update_setup(&mut self) -> Result<()> {
let ui = match &mut self.setup_ui { let setup_ui = match &mut self.setup_ui {
Some(c) => c, Some(c) => c,
None => bail!("SetupUI instance not found"), None => bail!("SetupUI instance not found"),
}; };
ui.update(&mut self.terminal).await?; setup_ui.update(&mut self.terminal).await?;
Ok(()) Ok(())
} }

View File

@ -1,19 +1,13 @@
use std::cmp; use std::cmp;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use tui::{ use tui::layout::{Constraint, Direction, Layout};
layout::{Constraint, Direction, Layout},
style::Color,
};
use crate::{ use crate::app::status::Status;
app::status::Status,
ui::{textarea_activate, textarea_inactivate},
};
use self::widgets::{messages, room_info, rooms, status}; use self::widgets::{command_monitor, messages, room_info, rooms, status};
use super::{InputPosition, UI}; use super::UI;
pub mod widgets; pub mod widgets;
@ -57,90 +51,12 @@ impl UI<'_> {
let right_chunks = Layout::default() let right_chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.constraints([Constraint::Min(4)].as_ref()) .constraints([Constraint::Length(5), Constraint::Min(4)].as_ref())
.split(main_chunks[2]); .split(main_chunks[2]);
// calculate to widgets colors, based of which widget is currently selected let colors = self
let colors = match self.input_position { .input_position
InputPosition::Status => { .colors(&mut self.cli, &mut self.message_compose);
textarea_inactivate(&mut self.message_compose);
if let Some(cli) = &mut self.cli {
textarea_inactivate(cli);
}
vec![
Color::White,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
]
}
InputPosition::Rooms => {
textarea_inactivate(&mut self.message_compose);
if let Some(cli) = &mut self.cli {
textarea_inactivate(cli);
}
vec![
Color::DarkGray,
Color::White,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
]
}
InputPosition::Messages => {
textarea_inactivate(&mut self.message_compose);
if let Some(cli) = &mut self.cli {
textarea_inactivate(cli);
}
vec![
Color::DarkGray,
Color::DarkGray,
Color::White,
Color::DarkGray,
Color::DarkGray,
]
}
InputPosition::MessageCompose => {
textarea_activate(&mut self.message_compose);
if let Some(cli) = &mut self.cli {
textarea_inactivate(cli);
}
vec![
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
]
}
InputPosition::RoomInfo => {
textarea_inactivate(&mut self.message_compose);
if let Some(cli) = &mut self.cli {
textarea_inactivate(cli);
}
vec![
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::White,
]
}
InputPosition::CLI => {
textarea_inactivate(&mut self.message_compose);
if let Some(cli) = &mut self.cli {
textarea_activate(cli);
}
vec![
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
Color::DarkGray,
]
}
};
// initiate the widgets // initiate the widgets
let status_panel = status::init(status, &colors); let status_panel = status::init(status, &colors);
@ -148,6 +64,7 @@ impl UI<'_> {
let (messages_panel, mut messages_state) = messages::init(status.room(), &colors) let (messages_panel, mut messages_state) = messages::init(status.room(), &colors)
.context("Failed to initiate the messages widget")?; .context("Failed to initiate the messages widget")?;
let room_info_panel = room_info::init(status.room(), &colors); let room_info_panel = room_info::init(status.room(), &colors);
let command_monitor = command_monitor::init(status.status_messages(), &colors);
// render the widgets // render the widgets
self.terminal.draw(|frame| { self.terminal.draw(|frame| {
@ -160,6 +77,7 @@ impl UI<'_> {
None => (), None => (),
}; };
frame.render_widget(room_info_panel, right_chunks[0]); frame.render_widget(room_info_panel, right_chunks[0]);
frame.render_widget(command_monitor, right_chunks[1]);
})?; })?;
Ok(()) Ok(())

View File

@ -0,0 +1,34 @@
use tui::{
layout::Alignment,
style::{Color, Style},
text::Text,
widgets::{Block, Borders, Paragraph},
};
use crate::{app::status::StatusMessage, ui::central::InputPosition};
pub fn init<'a>(status_events: &Vec<StatusMessage>, colors: &Vec<Color>) -> Paragraph<'a> {
let mut command_monitor = Text::default();
status_events.iter().for_each(|event| {
// TODO(@Soispha): The added text (`event.content()`) doesn't wrap nicely,
// it would be nice if it did.
command_monitor.extend(Text::styled(
event.content(),
Style::default().fg(if event.is_error() {
Color::Red
} else {
Color::Cyan
}),
));
});
Paragraph::new(command_monitor)
.block(
Block::default()
.title("Command Montior")
.borders(Borders::ALL)
.style(Style::default().fg(colors[InputPosition::CommandMonitor as usize])),
)
.alignment(Alignment::Center)
}

View File

@ -2,3 +2,4 @@ pub mod messages;
pub mod room_info; pub mod room_info;
pub mod rooms; pub mod rooms;
pub mod status; pub mod status;
pub mod command_monitor;