use std::sync::{Arc, Mutex};
use crate::error::Error;
use crate::event::{Event, EventSender};
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum Action {
Quit,
Refresh,
Help,
Cancel,
PreviousFile,
NextFile,
ToggleRuler,
ScrollUpLines(usize),
ScrollDownLines(usize),
ScrollUpScreenFraction(usize),
ScrollDownScreenFraction(usize),
ScrollToTop,
ScrollToBottom,
ScrollLeftColumns(usize),
ScrollRightColumns(usize),
ScrollLeftScreenFraction(usize),
ScrollRightScreenFraction(usize),
ToggleLineNumbers,
ToggleLineWrapping,
PromptGoToLine,
PromptSearchFromStart,
PromptSearchForwards,
PromptSearchBackwards,
PreviousMatch,
NextMatch,
PreviousMatchLine,
NextMatchLine,
PreviousMatchScreen,
NextMatchScreen,
FirstMatch,
LastMatch,
AppendDigitToRepeatCount(usize),
}
impl std::fmt::Display for Action {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Action::*;
match *self {
Quit => write!(f, "Quit"),
Refresh => write!(f, "Refresh the screen"),
Help => write!(f, "Show this help"),
Cancel => write!(f, "Close help or any open prompt"),
PreviousFile => write!(f, "Switch to the previous file"),
NextFile => write!(f, "Switch to the next file"),
ToggleRuler => write!(f, "Toggle the ruler"),
ScrollUpLines(1) => write!(f, "Scroll up"),
ScrollUpLines(n) => write!(f, "Scroll up {} lines", n),
ScrollDownLines(1) => write!(f, "Scroll down"),
ScrollDownLines(n) => write!(f, "Scroll down {} lines", n),
ScrollUpScreenFraction(1) => write!(f, "Scroll up one screen"),
ScrollUpScreenFraction(n) => write!(f, "Scroll up 1/{} screen", n),
ScrollDownScreenFraction(1) => write!(f, "Scroll down one screen"),
ScrollDownScreenFraction(n) => write!(f, "Scroll down 1/{} screen", n),
ScrollToTop => write!(f, "Move to the start of the file"),
ScrollToBottom => write!(f, "Move to and follow the end of the file"),
ScrollLeftColumns(1) => write!(f, "Scroll left"),
ScrollLeftColumns(n) => write!(f, "Scroll left {} columns", n),
ScrollRightColumns(1) => write!(f, "Scroll right"),
ScrollRightColumns(n) => write!(f, "Scroll right {} columns", n),
ScrollLeftScreenFraction(1) => write!(f, "Scroll left one screen"),
ScrollLeftScreenFraction(n) => write!(f, "Scroll left 1/{} screen", n),
ScrollRightScreenFraction(1) => write!(f, "Scroll right one screen"),
ScrollRightScreenFraction(n) => write!(f, "Scroll right 1/{} screen", n),
ToggleLineNumbers => write!(f, "Toggle line numbers"),
ToggleLineWrapping => write!(f, "Cycle through line wrapping modes"),
PromptGoToLine => write!(f, "Go to position in file"),
PromptSearchFromStart => write!(f, "Search from the start of the file"),
PromptSearchForwards => write!(f, "Search forwards"),
PromptSearchBackwards => write!(f, "Search backwards"),
PreviousMatch => write!(f, "Move to the previous match"),
NextMatch => write!(f, "Move to the next match"),
PreviousMatchLine => write!(f, "Move to the previous matching line"),
NextMatchLine => write!(f, "Move the the next matching line"),
PreviousMatchScreen => write!(f, "Move to the previous match following the screen"),
NextMatchScreen => write!(f, "Move to the next match following the screen"),
FirstMatch => write!(f, "Move to the first match"),
LastMatch => write!(f, "Move to the last match"),
AppendDigitToRepeatCount(n) => write!(f, "Append digit {} to repeat count", n),
}
}
}
#[derive(Clone)]
pub struct ActionSender(Arc<Mutex<EventSender>>);
impl ActionSender {
pub(crate) fn new(event_sender: EventSender) -> ActionSender {
ActionSender(Arc::new(Mutex::new(event_sender)))
}
pub fn send(&self, action: Action) -> Result<(), Error> {
let sender = self.0.lock().unwrap();
sender.send(Event::Action(action))?;
Ok(())
}
}