1use crate::auth::{AuthHandle, User};
2use crate::theme::Theme;
3use crossterm::event::KeyEvent;
4use ratatui::layout::Rect;
5use ratatui::Frame;
6use std::path::{Path, PathBuf};
7use std::sync::Arc;
8
9pub type PluginFactory = Arc<dyn Fn(&str, &str, &Path) -> Box<dyn Plugin + Send> + Send + Sync>;
12
13#[derive(Debug, Clone)]
15pub struct PluginCmdItem {
16 pub category: String,
17 pub label: String,
18}
19
20pub trait Plugin: Send {
21 fn id(&self) -> &str;
22 fn name(&self) -> &str;
23 fn init(&mut self, ctx: &mut PluginContext) -> Result<(), Box<dyn std::error::Error>>;
24 fn handle_key(&mut self, _key: KeyEvent) -> bool {
25 false
26 }
27 fn render(&self, _f: &mut Frame, _area: Rect) {}
28 fn tick(&mut self) {}
29 fn on_focus(&mut self) {}
30 fn on_blur(&mut self) {}
31 fn on_theme_change(&mut self, _theme: &Theme) {}
32 fn on_user_update(&mut self, _user: Option<&User>) {}
33 fn status_hints(&self) -> Vec<(String, String)> {
34 vec![]
35 }
36 fn commands(&self) -> Vec<PluginCmdItem> {
38 vec![]
39 }
40 fn handle_palette_command(&mut self, _index: usize) {}
43
44 fn on_plugin_message(&mut self, _from: &str, _action: &str, _data: &str) {}
46
47 fn shutdown(&mut self) {}
52
53 fn binary_path(&self) -> Option<&Path> {
57 None
58 }
59
60 fn is_alive(&self) -> bool {
64 true
65 }
66}
67
68pub struct PluginContext {
69 pub theme: Theme,
70 pub auth: Option<Arc<dyn AuthHandle>>,
71 pub data_dir: PathBuf,
75}
76
77impl PluginContext {
78 pub fn new() -> Self {
79 PluginContext {
80 theme: Theme::default(),
81 auth: None,
82 data_dir: PathBuf::new(),
83 }
84 }
85}
86
87impl Default for PluginContext {
88 fn default() -> Self {
89 Self::new()
90 }
91}