Rename "status" to "state" in most places

This follows the name change of euphoxide, which renamed its connection
Status to State.
This commit is contained in:
Joscha 2023-01-20 21:42:40 +01:00
parent f72da10171
commit 23352e7027
4 changed files with 89 additions and 91 deletions

View file

@ -44,7 +44,7 @@ enum Event {
Disconnected, Disconnected,
Packet(Box<ParsedPacket>), Packet(Box<ParsedPacket>),
// Commands // Commands
Status(oneshot::Sender<Option<ConnState>>), // TODO Rename to State State(oneshot::Sender<Option<ConnState>>),
RequestLogs, RequestLogs,
Auth(String), Auth(String),
Nick(String), Nick(String),
@ -236,7 +236,7 @@ impl State {
self.on_packet(&packet).await?; self.on_packet(&packet).await?;
let _ = euph_room_event_tx.send(EuphRoomEvent::Packet(packet)); let _ = euph_room_event_tx.send(EuphRoomEvent::Packet(packet));
} }
Event::Status(reply_tx) => self.on_status(reply_tx).await, Event::State(reply_tx) => self.on_state(reply_tx).await,
Event::RequestLogs => self.on_request_logs(), Event::RequestLogs => self.on_request_logs(),
Event::Auth(password) => self.on_auth(password), Event::Auth(password) => self.on_auth(password),
Event::Nick(name) => self.on_nick(name), Event::Nick(name) => self.on_nick(name),
@ -339,15 +339,14 @@ impl State {
Ok(()) Ok(())
} }
// TODO Rename to on_state async fn on_state(&self, reply_tx: oneshot::Sender<Option<ConnState>>) {
async fn on_status(&self, reply_tx: oneshot::Sender<Option<ConnState>>) { let state = if let Some(conn_tx) = &self.conn_tx {
let status = if let Some(conn_tx) = &self.conn_tx {
conn_tx.state().await.ok() conn_tx.state().await.ok()
} else { } else {
None None
}; };
let _ = reply_tx.send(status); let _ = reply_tx.send(state);
} }
fn on_request_logs(&self) { fn on_request_logs(&self) {
@ -505,11 +504,10 @@ impl Room {
self.event_tx.is_closed() self.event_tx.is_closed()
} }
// TODO Rename to state pub async fn state(&self) -> Result<Option<ConnState>, Error> {
pub async fn status(&self) -> Result<Option<ConnState>, Error> {
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
self.event_tx self.event_tx
.send(Event::Status(tx)) .send(Event::State(tx))
.map_err(|_| Error::Stopped)?; .map_err(|_| Error::Stopped)?;
rx.await.map_err(|_| Error::Stopped) rx.await.map_err(|_| Error::Stopped)
} }

View file

@ -14,7 +14,7 @@ use crate::ui::widgets::resize::Resize;
use crate::ui::widgets::text::Text; use crate::ui::widgets::text::Text;
use crate::ui::widgets::BoxedWidget; use crate::ui::widgets::BoxedWidget;
use super::room::RoomStatus; use super::room::RoomState;
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Focus { enum Focus {
@ -97,9 +97,9 @@ impl AccountUiState {
} }
/// Returns `false` if the account UI should not be displayed any longer. /// Returns `false` if the account UI should not be displayed any longer.
pub fn stabilize(&mut self, status: &RoomStatus) -> bool { pub fn stabilize(&mut self, state: &RoomState) -> bool {
if let RoomStatus::Connected(ConnState::Joined(status)) = status { if let RoomState::Connected(ConnState::Joined(state)) = state {
match (&self, &status.account) { match (&self, &state.account) {
(Self::LoggedOut(_), Some(view)) => *self = Self::LoggedIn(LoggedIn(view.clone())), (Self::LoggedOut(_), Some(view)) => *self = Self::LoggedIn(LoggedIn(view.clone())),
(Self::LoggedIn(_), None) => *self = Self::LoggedOut(LoggedOut::new()), (Self::LoggedIn(_), None) => *self = Self::LoggedOut(LoggedOut::new()),
_ => {} _ => {}

View file

@ -49,14 +49,14 @@ enum State {
} }
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
pub enum RoomStatus { pub enum RoomState {
NoRoom, NoRoom,
Stopped, Stopped,
Connecting, Connecting,
Connected(ConnState), Connected(ConnState),
} }
impl RoomStatus { impl RoomState {
pub fn connecting_or_connected(&self) -> bool { pub fn connecting_or_connected(&self) -> bool {
match self { match self {
Self::NoRoom | Self::Stopped => false, Self::NoRoom | Self::Stopped => false,
@ -147,14 +147,14 @@ impl EuphRoom {
self.room = None; self.room = None;
} }
pub async fn status(&self) -> RoomStatus { pub async fn state(&self) -> RoomState {
match &self.room { match &self.room {
Some(room) => match room.status().await { Some(room) => match room.state().await {
Ok(Some(status)) => RoomStatus::Connected(status), Ok(Some(state)) => RoomState::Connected(state),
Ok(None) => RoomStatus::Connecting, Ok(None) => RoomState::Connecting,
Err(_) => RoomStatus::Stopped, Err(_) => RoomState::Stopped,
}, },
None => RoomStatus::NoRoom, None => RoomState::NoRoom,
} }
} }
@ -190,19 +190,19 @@ impl EuphRoom {
} }
} }
fn stabilize_focus(&mut self, status: &RoomStatus) { fn stabilize_focus(&mut self, state: &RoomState) {
match status { match state {
RoomStatus::Connected(ConnState::Joined(_)) => {} RoomState::Connected(ConnState::Joined(_)) => {}
_ => self.focus = Focus::Chat, // There is no nick list to focus on _ => self.focus = Focus::Chat, // There is no nick list to focus on
} }
} }
fn stabilize_state(&mut self, status: &RoomStatus) { fn stabilize_state(&mut self, state: &RoomState) {
match &mut self.state { match &mut self.state {
State::Auth(_) State::Auth(_)
if !matches!( if !matches!(
status, state,
RoomStatus::Connected(ConnState::Joining(Joining { RoomState::Connected(ConnState::Joining(Joining {
bounce: Some(_), bounce: Some(_),
.. ..
})) }))
@ -210,11 +210,11 @@ impl EuphRoom {
{ {
self.state = State::Normal self.state = State::Normal
} }
State::Nick(_) if !matches!(status, RoomStatus::Connected(ConnState::Joined(_))) => { State::Nick(_) if !matches!(state, RoomState::Connected(ConnState::Joined(_))) => {
self.state = State::Normal self.state = State::Normal
} }
State::Account(account) => { State::Account(account) => {
if !account.stabilize(status) { if !account.stabilize(state) {
self.state = State::Normal self.state = State::Normal
} }
} }
@ -222,20 +222,20 @@ impl EuphRoom {
} }
} }
async fn stabilize(&mut self, status: &RoomStatus) { async fn stabilize(&mut self, state: &RoomState) {
self.stabilize_pseudo_msg().await; self.stabilize_pseudo_msg().await;
self.stabilize_focus(status); self.stabilize_focus(state);
self.stabilize_state(status); self.stabilize_state(state);
} }
pub async fn widget(&mut self) -> BoxedWidget { pub async fn widget(&mut self) -> BoxedWidget {
let status = self.status().await; let state = self.state().await;
self.stabilize(&status).await; self.stabilize(&state).await;
let chat = if let RoomStatus::Connected(ConnState::Joined(joined)) = &status { let chat = if let RoomState::Connected(ConnState::Joined(joined)) = &state {
self.widget_with_nick_list(&status, joined).await self.widget_with_nick_list(&state, joined).await
} else { } else {
self.widget_without_nick_list(&status).await self.widget_without_nick_list(&state).await
}; };
let mut layers = vec![chat]; let mut layers = vec![chat];
@ -257,10 +257,10 @@ impl EuphRoom {
Layer::new(layers).into() Layer::new(layers).into()
} }
async fn widget_without_nick_list(&self, status: &RoomStatus) -> BoxedWidget { async fn widget_without_nick_list(&self, state: &RoomState) -> BoxedWidget {
VJoin::new(vec![ VJoin::new(vec![
Segment::new(Border::new( Segment::new(Border::new(
Padding::new(self.status_widget(status).await).horizontal(1), Padding::new(self.status_widget(state).await).horizontal(1),
)), )),
// TODO Use last known nick? // TODO Use last known nick?
Segment::new(self.chat.widget(String::new(), true)).expanding(true), Segment::new(self.chat.widget(String::new(), true)).expanding(true),
@ -268,11 +268,11 @@ impl EuphRoom {
.into() .into()
} }
async fn widget_with_nick_list(&self, status: &RoomStatus, joined: &Joined) -> BoxedWidget { async fn widget_with_nick_list(&self, state: &RoomState, joined: &Joined) -> BoxedWidget {
HJoin::new(vec![ HJoin::new(vec![
Segment::new(VJoin::new(vec![ Segment::new(VJoin::new(vec![
Segment::new(Border::new( Segment::new(Border::new(
Padding::new(self.status_widget(status).await).horizontal(1), Padding::new(self.status_widget(state).await).horizontal(1),
)), )),
Segment::new( Segment::new(
self.chat self.chat
@ -293,18 +293,18 @@ impl EuphRoom {
.into() .into()
} }
async fn status_widget(&self, status: &RoomStatus) -> BoxedWidget { async fn status_widget(&self, state: &RoomState) -> BoxedWidget {
let room_style = ContentStyle::default().bold().blue(); let room_style = ContentStyle::default().bold().blue();
let mut info = Styled::new(format!("&{}", self.name()), room_style); let mut info = Styled::new(format!("&{}", self.name()), room_style);
info = match status { info = match state {
RoomStatus::NoRoom | RoomStatus::Stopped => info.then_plain(", archive"), RoomState::NoRoom | RoomState::Stopped => info.then_plain(", archive"),
RoomStatus::Connecting => info.then_plain(", connecting..."), RoomState::Connecting => info.then_plain(", connecting..."),
RoomStatus::Connected(ConnState::Joining(j)) if j.bounce.is_some() => { RoomState::Connected(ConnState::Joining(j)) if j.bounce.is_some() => {
info.then_plain(", auth required") info.then_plain(", auth required")
} }
RoomStatus::Connected(ConnState::Joining(_)) => info.then_plain(", joining..."), RoomState::Connected(ConnState::Joining(_)) => info.then_plain(", joining..."),
RoomStatus::Connected(ConnState::Joined(j)) => { RoomState::Connected(ConnState::Joined(j)) => {
let nick = &j.session.name; let nick = &j.session.name;
if nick.is_empty() { if nick.is_empty() {
info.then_plain(", present without nick") info.then_plain(", present without nick")
@ -326,8 +326,8 @@ impl EuphRoom {
Text::new(info).into() Text::new(info).into()
} }
async fn list_chat_key_bindings(&self, bindings: &mut KeyBindingsList, status: &RoomStatus) { async fn list_chat_key_bindings(&self, bindings: &mut KeyBindingsList, state: &RoomState) {
let can_compose = matches!(status, RoomStatus::Connected(ConnState::Joined(_))); let can_compose = matches!(state, RoomState::Connected(ConnState::Joined(_)));
self.chat.list_key_bindings(bindings, can_compose).await; self.chat.list_key_bindings(bindings, can_compose).await;
} }
@ -336,9 +336,9 @@ impl EuphRoom {
terminal: &mut Terminal, terminal: &mut Terminal,
crossterm_lock: &Arc<FairMutex<()>>, crossterm_lock: &Arc<FairMutex<()>>,
event: &InputEvent, event: &InputEvent,
status: &RoomStatus, state: &RoomState,
) -> bool { ) -> bool {
let can_compose = matches!(status, RoomStatus::Connected(ConnState::Joined(_))); let can_compose = matches!(state, RoomState::Connected(ConnState::Joined(_)));
match self match self
.chat .chat
@ -368,17 +368,17 @@ impl EuphRoom {
false false
} }
fn list_room_key_bindings(&self, bindings: &mut KeyBindingsList, status: &RoomStatus) { fn list_room_key_bindings(&self, bindings: &mut KeyBindingsList, state: &RoomState) {
match status { match state {
// Authenticating // Authenticating
RoomStatus::Connected(ConnState::Joining(Joining { RoomState::Connected(ConnState::Joining(Joining {
bounce: Some(_), .. bounce: Some(_), ..
})) => { })) => {
bindings.binding("a", "authenticate"); bindings.binding("a", "authenticate");
} }
// Connected // Connected
RoomStatus::Connected(ConnState::Joined(_)) => { RoomState::Connected(ConnState::Joined(_)) => {
bindings.binding("n", "change nick"); bindings.binding("n", "change nick");
bindings.binding("m", "download more messages"); bindings.binding("m", "download more messages");
bindings.binding("A", "show account ui"); bindings.binding("A", "show account ui");
@ -394,10 +394,10 @@ impl EuphRoom {
bindings.binding("ctrl+p", "open room's plugh.de/present page"); bindings.binding("ctrl+p", "open room's plugh.de/present page");
} }
async fn handle_room_input_event(&mut self, event: &InputEvent, status: &RoomStatus) -> bool { async fn handle_room_input_event(&mut self, event: &InputEvent, state: &RoomState) -> bool {
match status { match state {
// Authenticating // Authenticating
RoomStatus::Connected(ConnState::Joining(Joining { RoomState::Connected(ConnState::Joining(Joining {
bounce: Some(_), .. bounce: Some(_), ..
})) => { })) => {
if let key!('a') = event { if let key!('a') = event {
@ -407,7 +407,7 @@ impl EuphRoom {
} }
// Joined // Joined
RoomStatus::Connected(ConnState::Joined(joined)) => match event { RoomState::Connected(ConnState::Joined(joined)) => match event {
key!('n') | key!('N') => { key!('n') | key!('N') => {
self.state = State::Nick(nick::new(joined.clone())); self.state = State::Nick(nick::new(joined.clone()));
return true; return true;
@ -466,11 +466,11 @@ impl EuphRoom {
async fn list_chat_focus_key_bindings( async fn list_chat_focus_key_bindings(
&self, &self,
bindings: &mut KeyBindingsList, bindings: &mut KeyBindingsList,
status: &RoomStatus, state: &RoomState,
) { ) {
self.list_room_key_bindings(bindings, status); self.list_room_key_bindings(bindings, state);
bindings.empty(); bindings.empty();
self.list_chat_key_bindings(bindings, status).await; self.list_chat_key_bindings(bindings, state).await;
} }
async fn handle_chat_focus_input_event( async fn handle_chat_focus_input_event(
@ -478,18 +478,18 @@ impl EuphRoom {
terminal: &mut Terminal, terminal: &mut Terminal,
crossterm_lock: &Arc<FairMutex<()>>, crossterm_lock: &Arc<FairMutex<()>>,
event: &InputEvent, event: &InputEvent,
status: &RoomStatus, state: &RoomState,
) -> bool { ) -> bool {
// We need to handle chat input first, otherwise the other // We need to handle chat input first, otherwise the other
// key bindings will shadow characters in the editor. // key bindings will shadow characters in the editor.
if self if self
.handle_chat_input_event(terminal, crossterm_lock, event, status) .handle_chat_input_event(terminal, crossterm_lock, event, state)
.await .await
{ {
return true; return true;
} }
if self.handle_room_input_event(event, status).await { if self.handle_room_input_event(event, state).await {
return true; return true;
} }
@ -505,14 +505,14 @@ impl EuphRoom {
fn handle_nick_list_focus_input_event( fn handle_nick_list_focus_input_event(
&mut self, &mut self,
event: &InputEvent, event: &InputEvent,
status: &RoomStatus, state: &RoomState,
) -> bool { ) -> bool {
if util::handle_list_input_event(&mut self.nick_list, event) { if util::handle_list_input_event(&mut self.nick_list, event) {
return true; return true;
} }
if let key!('i') = event { if let key!('i') = event {
if let RoomStatus::Connected(ConnState::Joined(joined)) = status { if let RoomState::Connected(ConnState::Joined(joined)) = state {
if let Some(id) = self.nick_list.cursor() { if let Some(id) = self.nick_list.cursor() {
if id == joined.session.session_id { if id == joined.session.session_id {
self.state = self.state =
@ -532,15 +532,15 @@ impl EuphRoom {
// Handled in rooms list, not here // Handled in rooms list, not here
bindings.binding("esc", "leave room"); bindings.binding("esc", "leave room");
let status = self.status().await; let state = self.state().await;
match self.focus { match self.focus {
Focus::Chat => { Focus::Chat => {
if let RoomStatus::Connected(ConnState::Joined(_)) = status { if let RoomState::Connected(ConnState::Joined(_)) = state {
bindings.binding("tab", "focus on nick list"); bindings.binding("tab", "focus on nick list");
} }
self.list_chat_focus_key_bindings(bindings, &status).await; self.list_chat_focus_key_bindings(bindings, &state).await;
} }
Focus::NickList => { Focus::NickList => {
bindings.binding("tab, esc", "focus on chat"); bindings.binding("tab, esc", "focus on chat");
@ -557,20 +557,20 @@ impl EuphRoom {
crossterm_lock: &Arc<FairMutex<()>>, crossterm_lock: &Arc<FairMutex<()>>,
event: &InputEvent, event: &InputEvent,
) -> bool { ) -> bool {
let status = self.status().await; let state = self.state().await;
match self.focus { match self.focus {
Focus::Chat => { Focus::Chat => {
// Needs to be handled first or the tab key may be shadowed // Needs to be handled first or the tab key may be shadowed
// during editing. // during editing.
if self if self
.handle_chat_focus_input_event(terminal, crossterm_lock, event, &status) .handle_chat_focus_input_event(terminal, crossterm_lock, event, &state)
.await .await
{ {
return true; return true;
} }
if let RoomStatus::Connected(ConnState::Joined(_)) = status { if let RoomState::Connected(ConnState::Joined(_)) = state {
if let key!(Tab) = event { if let key!(Tab) = event {
self.focus = Focus::NickList; self.focus = Focus::NickList;
return true; return true;
@ -583,7 +583,7 @@ impl EuphRoom {
return true; return true;
} }
if self.handle_nick_list_focus_input_event(event, &status) { if self.handle_nick_list_focus_input_event(event, &state) {
return true; return true;
} }
} }

View file

@ -14,7 +14,7 @@ use crate::config::{Config, RoomsSortOrder};
use crate::euph::EuphRoomEvent; use crate::euph::EuphRoomEvent;
use crate::vault::Vault; use crate::vault::Vault;
use super::euph::room::{EuphRoom, RoomStatus}; use super::euph::room::{EuphRoom, RoomState};
use super::input::{key, InputEvent, KeyBindingsList}; use super::input::{key, InputEvent, KeyBindingsList};
use super::widgets::editor::EditorState; use super::widgets::editor::EditorState;
use super::widgets::join::{HJoin, Segment, VJoin}; use super::widgets::join::{HJoin, Segment, VJoin};
@ -236,15 +236,15 @@ impl Rooms {
result.join(" ") result.join(" ")
} }
fn format_status(status: RoomStatus) -> Option<String> { fn format_room_state(state: RoomState) -> Option<String> {
match status { match state {
RoomStatus::NoRoom | RoomStatus::Stopped => None, RoomState::NoRoom | RoomState::Stopped => None,
RoomStatus::Connecting => Some("connecting".to_string()), RoomState::Connecting => Some("connecting".to_string()),
RoomStatus::Connected(ConnState::Joining(j)) if j.bounce.is_some() => { RoomState::Connected(ConnState::Joining(j)) if j.bounce.is_some() => {
Some("auth required".to_string()) Some("auth required".to_string())
} }
RoomStatus::Connected(ConnState::Joining(_)) => Some("joining".to_string()), RoomState::Connected(ConnState::Joining(_)) => Some("joining".to_string()),
RoomStatus::Connected(ConnState::Joined(joined)) => Some(Self::format_pbln(&joined)), RoomState::Connected(ConnState::Joined(joined)) => Some(Self::format_pbln(&joined)),
} }
} }
@ -256,13 +256,13 @@ impl Rooms {
} }
} }
fn format_room_info(status: RoomStatus, unseen: usize) -> Styled { fn format_room_info(state: RoomState, unseen: usize) -> Styled {
let unseen_style = ContentStyle::default().bold().green(); let unseen_style = ContentStyle::default().bold().green();
let status = Self::format_status(status); let state = Self::format_room_state(state);
let unseen = Self::format_unseen_msgs(unseen); let unseen = Self::format_unseen_msgs(unseen);
match (status, unseen) { match (state, unseen) {
(None, None) => Styled::default(), (None, None) => Styled::default(),
(None, Some(u)) => Styled::new_plain(" (") (None, Some(u)) => Styled::new_plain(" (")
.then(u, unseen_style) .then(u, unseen_style)
@ -276,7 +276,7 @@ impl Rooms {
} }
} }
fn sort_rooms(&self, rooms: &mut [(&String, RoomStatus, usize)]) { fn sort_rooms(&self, rooms: &mut [(&String, RoomState, usize)]) {
match self.order { match self.order {
Order::Alphabet => rooms.sort_unstable_by_key(|(n, _, _)| *n), Order::Alphabet => rooms.sort_unstable_by_key(|(n, _, _)| *n),
Order::Importance => { Order::Importance => {
@ -295,19 +295,19 @@ impl Rooms {
let mut rooms = vec![]; let mut rooms = vec![];
for (name, room) in &self.euph_rooms { for (name, room) in &self.euph_rooms {
let status = room.status().await; let state = room.state().await;
let unseen = room.unseen_msgs_count().await; let unseen = room.unseen_msgs_count().await;
rooms.push((name, status, unseen)); rooms.push((name, state, unseen));
} }
self.sort_rooms(&mut rooms); self.sort_rooms(&mut rooms);
for (name, status, unseen) in rooms { for (name, state, unseen) in rooms {
let room_style = ContentStyle::default().bold().blue(); let room_style = ContentStyle::default().bold().blue();
let room_sel_style = ContentStyle::default().bold().black().on_white(); let room_sel_style = ContentStyle::default().bold().black().on_white();
let mut normal = Styled::new(format!("&{name}"), room_style); let mut normal = Styled::new(format!("&{name}"), room_style);
let mut selected = Styled::new(format!("&{name}"), room_sel_style); let mut selected = Styled::new(format!("&{name}"), room_sel_style);
let info = Self::format_room_info(status, unseen); let info = Self::format_room_info(state, unseen);
normal = normal.and_then(info.clone()); normal = normal.and_then(info.clone());
selected = selected.and_then(info); selected = selected.and_then(info);