use crate::prelude::*;
use std::fmt::Display;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Mode {
Normal,
Visual,
Select,
OperatorPending,
Insert,
CommandLineEx,
CommandLineSearchForward,
CommandLineSearchBackward,
Terminal,
}
impl Display for Mode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Mode::Normal => write!(f, "Normal"),
Mode::Visual => write!(f, "Visual"),
Mode::Select => write!(f, "Select"),
Mode::OperatorPending => write!(f, "Operator-pending"),
Mode::Insert => write!(f, "Insert"),
Mode::CommandLineEx => write!(f, "Command-line (ex)"),
Mode::CommandLineSearchForward => {
write!(f, "Command-line (search forward)")
}
Mode::CommandLineSearchBackward => {
write!(f, "Command-line (search backward)")
}
Mode::Terminal => write!(f, "Terminal"),
}
}
}
impl Mode {
pub fn all() -> Vec<Mode> {
vec![
Mode::Normal,
Mode::Visual,
Mode::Select,
Mode::OperatorPending,
Mode::Insert,
Mode::CommandLineEx,
Mode::Terminal,
]
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Modes {
values: HashSet<Mode>,
}
impl Modes {
pub fn new() -> Self {
Modes {
values: HashSet::new(),
}
}
pub fn with(&self, mode: Mode) -> Self {
let mut values = self.values.clone();
values.insert(mode);
Modes { values }
}
pub fn without(&self, mode: Mode) -> Self {
let mut values = self.values.clone();
values.remove(&mode);
Modes { values }
}
pub fn set(&mut self, mode: Mode) -> bool {
self.values.insert(mode)
}
pub fn unset(&mut self, mode: Mode) -> bool {
self.values.remove(&mode)
}
pub fn extend(&mut self, modes: Modes) {
self.values.extend(modes.values.iter())
}
pub fn is_empty(&self) -> bool {
self.values.is_empty()
}
pub fn len(&self) -> usize {
self.values.len()
}
pub fn contains(&self, mode: &Mode) -> bool {
self.values.contains(mode)
}
pub fn iter(&self) -> std::collections::hash_set::Iter<'_, Mode> {
self.values.iter()
}
}
impl From<Mode> for Modes {
fn from(mode: Mode) -> Self {
let mut values = HashSet::new();
values.insert(mode);
Modes { values }
}
}
impl From<Vec<Mode>> for Modes {
fn from(modes: Vec<Mode>) -> Self {
let mut values = HashSet::new();
for m in modes.iter() {
values.insert(*m);
}
Modes { values }
}
}