gravityfile-plugin 0.4.0

Lua plugin system for gravityfile
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Hook definitions for plugin event system.
//!
//! The explorer fires these events. Every hook but [`Hook::OnDeleteStart`]
//! is an observer: it runs in the background and cannot hold up the
//! explorer. `on_delete_start` is awaited, and a plugin can veto the delete
//! by returning `{ prevent_default = true }`.

use std::collections::HashMap;
use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use crate::types::Value;

/// Events that plugins can hook into.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Hook {
    // ==================== Navigation Events ====================
    /// The explorer moved to another directory.
    OnNavigate {
        /// Previous directory path.
        from: PathBuf,
        /// New directory path.
        to: PathBuf,
    },

    // ==================== Scan Events ====================
    /// A full scan started.
    OnScanStart {
        /// Root path being scanned.
        path: PathBuf,
    },

    /// A full scan finished.
    OnScanComplete {
        /// Root path that was scanned.
        path: PathBuf,
        /// Total files found.
        total_files: u64,
        /// Total directories found.
        total_dirs: u64,
        /// Total size in bytes.
        total_size: u64,
    },

    /// A full scan failed.
    OnScanError {
        /// Root path that failed.
        path: PathBuf,
        /// Error message.
        error: String,
    },

    // ==================== File Operation Events ====================
    /// A delete is about to start. Awaited; a plugin can veto it.
    OnDeleteStart {
        /// Items to be deleted.
        items: Vec<PathBuf>,
        /// Whether using trash (recoverable).
        use_trash: bool,
    },

    /// A delete finished.
    OnDeleteComplete {
        /// Number of items successfully deleted.
        deleted: usize,
        /// Number of items that failed to delete.
        failed: usize,
        /// Total bytes freed.
        bytes_freed: u64,
    },

    /// A copy finished.
    OnCopyComplete {
        /// Number of items successfully copied.
        succeeded: usize,
        /// Number of items that failed.
        failed: usize,
        /// Total bytes copied.
        bytes_copied: u64,
    },

    /// A move finished.
    OnMoveComplete {
        /// Number of items successfully moved.
        succeeded: usize,
        /// Number of items that failed.
        failed: usize,
    },

    /// A rename finished.
    OnRenameComplete {
        /// Original path.
        source: PathBuf,
        /// New path after rename.
        new_path: PathBuf,
    },

    // ==================== Analysis Events ====================
    /// Duplicate analysis finished.
    OnDuplicatesFound {
        /// Number of duplicate groups found.
        group_count: usize,
        /// Total wasted space in bytes.
        wasted_bytes: u64,
    },

    /// Age analysis finished.
    OnAgeAnalysisComplete {
        /// Number of stale directories found.
        stale_dirs: usize,
        /// Files whose ages were counted.
        files: u64,
        /// Their average age, in seconds.
        average_age_secs: u64,
    },

    // ==================== Lifecycle Events ====================
    /// The explorer started.
    OnStartup,

    /// The explorer is about to quit; awaited briefly.
    OnShutdown,
}

impl Hook {
    /// The function a plugin defines to receive this hook.
    pub fn name(&self) -> &'static str {
        match self {
            Self::OnNavigate { .. } => "on_navigate",
            Self::OnScanStart { .. } => "on_scan_start",
            Self::OnScanComplete { .. } => "on_scan_complete",
            Self::OnScanError { .. } => "on_scan_error",
            Self::OnDeleteStart { .. } => "on_delete_start",
            Self::OnDeleteComplete { .. } => "on_delete_complete",
            Self::OnCopyComplete { .. } => "on_copy_complete",
            Self::OnMoveComplete { .. } => "on_move_complete",
            Self::OnRenameComplete { .. } => "on_rename_complete",
            Self::OnDuplicatesFound { .. } => "on_duplicates_found",
            Self::OnAgeAnalysisComplete { .. } => "on_age_analysis_complete",
            Self::OnStartup => "on_startup",
            Self::OnShutdown => "on_shutdown",
        }
    }
}

/// How prominent a plugin's notice is.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum NoticeLevel {
    #[default]
    Info,
    Warn,
    Error,
}

impl NoticeLevel {
    /// The level named `name`; anything unknown is `Info`.
    pub fn parse(name: &str) -> Self {
        match name {
            "warn" | "warning" => Self::Warn,
            "error" => Self::Error,
            _ => Self::Info,
        }
    }
}

/// A message a plugin asked the explorer to show, with `gf.notify`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Notice {
    pub level: NoticeLevel,
    pub message: String,
}

impl Notice {
    /// The most notices one hook call can raise.
    pub const MAX_PER_CALL: usize = 8;
    /// The longest notice, in bytes; longer ones are cut at a character.
    pub const MAX_BYTES: usize = 500;

    /// A notice, its message cut to [`Self::MAX_BYTES`].
    pub fn new(level: NoticeLevel, message: &str) -> Self {
        let mut end = message.len().min(Self::MAX_BYTES);
        while !message.is_char_boundary(end) {
            end -= 1;
        }
        Self {
            level,
            message: message[..end].to_string(),
        }
    }
}

/// Context provided to plugins when a hook is invoked.
#[derive(Debug, Clone, Default)]
pub struct HookContext {
    /// Additional data passed to the hook.
    pub data: HashMap<String, Value>,

    /// Current working directory.
    pub cwd: Option<PathBuf>,

    /// Current view root (drill-down location).
    pub view_root: Option<PathBuf>,

    /// The colour theme's name.
    pub theme: Option<String>,
}

impl HookContext {
    /// Create a new empty context.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set a value in the context.
    pub fn set(&mut self, key: impl Into<String>, value: impl Into<Value>) -> &mut Self {
        self.data.insert(key.into(), value.into());
        self
    }

    /// Get a value from the context.
    pub fn get(&self, key: &str) -> Option<&Value> {
        self.data.get(key)
    }

    /// Set the current working directory.
    pub fn with_cwd(mut self, cwd: PathBuf) -> Self {
        self.cwd = Some(cwd);
        self
    }

    /// Set the view root.
    pub fn with_view_root(mut self, view_root: PathBuf) -> Self {
        self.view_root = Some(view_root);
        self
    }

    /// Set the theme.
    pub fn with_theme(mut self, theme: impl Into<String>) -> Self {
        self.theme = Some(theme.into());
        self
    }
}

/// Result returned by a plugin hook handler.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct HookResult {
    /// Whether the hook was handled.
    pub handled: bool,

    /// Whether to prevent default behavior.
    pub prevent_default: bool,

    /// Whether to stop propagation to other plugins.
    pub stop_propagation: bool,

    /// Return value from the hook (if any).
    pub value: Option<Value>,

    /// Error message (if hook failed).
    pub error: Option<String>,

    /// Messages the plugin asked the explorer to show.
    #[serde(default)]
    pub notices: Vec<Notice>,

    /// The plugin that produced this result, filled in by the manager.
    #[serde(default)]
    pub plugin: Option<String>,
}

impl HookResult {
    /// Create a successful result.
    pub fn ok() -> Self {
        Self {
            handled: true,
            ..Default::default()
        }
    }

    /// Create a result with a return value.
    pub fn with_value(value: impl Into<Value>) -> Self {
        Self {
            handled: true,
            value: Some(value.into()),
            ..Default::default()
        }
    }

    /// Create an error result.
    pub fn error(message: impl Into<String>) -> Self {
        Self {
            handled: true,
            error: Some(message.into()),
            ..Default::default()
        }
    }

    /// Mark this result as preventing default behavior.
    pub fn prevent_default(mut self) -> Self {
        self.prevent_default = true;
        self
    }

    /// Mark this result as stopping propagation.
    pub fn stop_propagation(mut self) -> Self {
        self.stop_propagation = true;
        self
    }

    /// Check if the hook execution was successful.
    pub fn is_ok(&self) -> bool {
        self.error.is_none()
    }

    /// Check if the hook had an error.
    pub fn is_err(&self) -> bool {
        self.error.is_some()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// One of every hook. The match makes a new variant fail to compile here
    /// until it is listed, and the test then checks plugin discovery knows it.
    fn every_hook() -> Vec<Hook> {
        let all = vec![
            Hook::OnNavigate {
                from: PathBuf::new(),
                to: PathBuf::new(),
            },
            Hook::OnScanStart {
                path: PathBuf::new(),
            },
            Hook::OnScanComplete {
                path: PathBuf::new(),
                total_files: 0,
                total_dirs: 0,
                total_size: 0,
            },
            Hook::OnScanError {
                path: PathBuf::new(),
                error: String::new(),
            },
            Hook::OnDeleteStart {
                items: Vec::new(),
                use_trash: true,
            },
            Hook::OnDeleteComplete {
                deleted: 0,
                failed: 0,
                bytes_freed: 0,
            },
            Hook::OnCopyComplete {
                succeeded: 0,
                failed: 0,
                bytes_copied: 0,
            },
            Hook::OnMoveComplete {
                succeeded: 0,
                failed: 0,
            },
            Hook::OnRenameComplete {
                source: PathBuf::new(),
                new_path: PathBuf::new(),
            },
            Hook::OnDuplicatesFound {
                group_count: 0,
                wasted_bytes: 0,
            },
            Hook::OnAgeAnalysisComplete {
                stale_dirs: 0,
                files: 0,
                average_age_secs: 0,
            },
            Hook::OnStartup,
            Hook::OnShutdown,
        ];
        for hook in &all {
            match hook {
                Hook::OnNavigate { .. }
                | Hook::OnScanStart { .. }
                | Hook::OnScanComplete { .. }
                | Hook::OnScanError { .. }
                | Hook::OnDeleteStart { .. }
                | Hook::OnDeleteComplete { .. }
                | Hook::OnCopyComplete { .. }
                | Hook::OnMoveComplete { .. }
                | Hook::OnRenameComplete { .. }
                | Hook::OnDuplicatesFound { .. }
                | Hook::OnAgeAnalysisComplete { .. }
                | Hook::OnStartup
                | Hook::OnShutdown => {}
            }
        }
        all
    }

    #[test]
    fn plugin_discovery_knows_every_hook_and_nothing_else() {
        let names: Vec<&str> = every_hook().iter().map(Hook::name).collect();
        assert_eq!(names, crate::runtime::HOOK_NAMES);
    }

    #[test]
    fn long_notices_are_cut_at_a_character_boundary() {
        let notice = Notice::new(NoticeLevel::Info, &"é".repeat(Notice::MAX_BYTES));
        assert!(notice.message.len() <= Notice::MAX_BYTES);
        assert!(notice.message.chars().all(|c| c == 'é'));
        assert_eq!(NoticeLevel::parse("warning"), NoticeLevel::Warn);
        assert_eq!(NoticeLevel::parse("loud"), NoticeLevel::Info);
    }
}