use super::Model;
use crate::reply::help::HelpRow;
use crate::reply::search::Found;
use crate::ui::Channel;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Lookup {
Commands,
Finding,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Pages {
pub channel: Channel,
pub rows: Vec<HelpRow>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Hits {
pub channel: Channel,
pub found: Found,
}
impl Model {
pub fn begin_commands(&mut self) {
self.lookup = Some(Lookup::Commands);
self.outbox
.push(super::Posted::read(crate::verbs::window::help()));
}
pub fn commanding(&self) -> bool {
self.lookup == Some(Lookup::Commands)
}
pub(super) fn paged(&mut self, channel: &Channel, rows: Vec<HelpRow>) {
let answered = Pages {
channel: channel.clone(),
rows,
};
match self
.pages
.iter_mut()
.find(|held| held.channel.name == channel.name)
{
Some(held) => *held = answered,
None => self.pages.push(answered),
}
}
pub fn begin_finding(&mut self) {
self.lookup = Some(Lookup::Finding);
}
pub fn finding(&self) -> bool {
self.lookup == Some(Lookup::Finding)
}
pub fn close_lookup(&mut self) {
self.lookup = None;
}
pub fn needled(&self) -> bool {
!self.needle.trim().is_empty()
}
pub fn post_search(&mut self) {
if self.needled() {
self.outbox.push(super::Posted::read(crate::verbs::search(
self.needle.trim().to_owned(),
)));
}
}
pub(super) fn hit(&mut self, channel: &Channel, found: Found) {
let answered = Hits {
channel: channel.clone(),
found,
};
match self
.found
.iter_mut()
.find(|held| held.channel.name == channel.name)
{
Some(held) => *held = answered,
None => self.found.push(answered),
}
}
pub fn refresh_roster(&mut self) {
self.outbox
.push(super::Posted::read(crate::verbs::workspaces()));
}
}
#[cfg(test)]
mod tests;