Skip to main content

mimick/
watch_path_display.rs

1//! Provides user-friendly display names for watch paths.
2//!
3//! Flatpak document-portal paths (`/run/user/.../doc/...`) are mapped
4//! back to their original human-readable folder names. Regular paths
5//! are shortened to their final component for compact UI display.
6
7use std::path::Path;
8
9/// Converts a stored watch path into a user-friendly label for display in the UI and logs.
10pub fn display_watch_path(path: &str) -> String {
11    if is_document_portal_path(path) {
12        Path::new(path)
13            .file_name()
14            .and_then(|name| name.to_str())
15            .filter(|name| !name.is_empty())
16            .map(|name| name.to_string())
17            .unwrap_or_else(|| "Selected Folder".to_string())
18    } else {
19        path.to_string()
20    }
21}
22
23/// Returns an explanatory subtitle for special watch paths, if applicable.
24pub fn watch_path_subtitle(_path: &str) -> Option<&'static str> {
25    None
26}
27
28/// Returns the full path for regular folders, or the folder name for Flatpak portal paths.
29/// Useful for contexts where the path is displayed inline (e.g., diagnostics, album linked folder) without a separate subtitle.
30pub fn display_watch_path_inline(path: &str) -> String {
31    display_watch_path(path)
32}
33
34/// Detects document-portal paths returned by the Flatpak file chooser portal.
35pub fn is_document_portal_path(path: &str) -> bool {
36    path.starts_with("/run/user/") && path.contains("/doc/")
37}
38
39#[cfg(test)]
40mod tests {
41    use super::{display_watch_path, is_document_portal_path};
42
43    #[test]
44    fn test_display_watch_path_for_portal_folder() {
45        assert_eq!(
46            display_watch_path("/run/user/1000/doc/abcd1234/Screenshots"),
47            "Screenshots"
48        );
49    }
50
51    #[test]
52    fn test_display_watch_path_for_regular_folder() {
53        assert_eq!(
54            display_watch_path("/home/user/Pictures"),
55            "/home/user/Pictures"
56        );
57        assert!(!is_document_portal_path("/home/user/Pictures"));
58    }
59}