Files
ratatrix/src/plugins/core.rs
2023-11-06 20:37:24 +01:00

98 lines
2.4 KiB
Rust

/*
* Copyright (C) 2023 Valentin Lorentz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use color_eyre::eyre::{Result, WrapErr};
use tokio::sync::mpsc;
use crate::action::Action;
use crate::commands::{CommandHandler, RataCommand};
use crate::plugins::{Plugin, PluginGetter, PrePlugin};
inventory::submit! {
PluginGetter(get_plugin)
}
fn get_plugin() -> Result<Box<dyn PrePlugin>> {
Ok(Box::new(Core {}))
}
struct Core {}
impl PrePlugin for Core {
fn name(&self) -> String {
"Core".to_owned()
}
fn help(&self) -> String {
"core utilities".to_owned()
}
fn load(self: Box<Self>, app: &mut crate::App) -> Result<Box<dyn Plugin>> {
app.commands.register(ActionCommand::new(
"quit",
"Exits the process",
Action::Quit,
));
app.commands.register(ActionCommand::new(
"suspend",
"Puts the process in the background",
Action::Suspend,
));
app.commands.register(ActionCommand::new(
"previous",
"Makes the previous buffer active",
Action::PreviousBuffer,
));
app.commands.register(ActionCommand::new(
"next",
"Makes the next buffer active",
Action::NextBuffer,
));
Ok(self)
}
}
impl Plugin for Core {}
struct ActionCommand {
pub name: &'static str,
pub help: &'static str,
pub action: Action,
}
impl ActionCommand {
pub fn new(name: &'static str, help: &'static str, action: Action) -> Box<dyn RataCommand> {
Box::new(ActionCommand { name, help, action })
}
}
impl RataCommand for ActionCommand {
fn name(&self) -> String {
self.name.to_owned()
}
fn help(&self) -> String {
self.help.to_owned()
}
fn handler(&self) -> CommandHandler {
let action = self.action.clone();
Box::new(move |_name, _args, _app, action_tx| {
action_tx
.send(action.clone())
.context("Could not queue action")
})
}
}