kuizer-gui 0.3.0

Desktop app for kuizer, a keyboard-driven PC cleaner that also deeply cleans developer artifacts, caches, toolchains, Docker and duplicates, built on GPUI
use kuizer::keymap::Action;

use crate::activity::{Activity, State, Step};
use crate::glossary::Term;
use crate::icon::Icon;
use crate::model::Part;
use crate::start::ScanOptions;

/// One section of the sidebar.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Tab {
    Overview,
    DiskUsage,
    Artifacts,
    Caches,
    Toolchains,
    Docker,
    Repos,
    AppCaches,
    Trash,
    System,
    LargeFiles,
    Duplicates,
    History,
    Issues,
}

impl Tab {
    pub const ALL: [Tab; 14] = [
        Self::Overview,
        Self::DiskUsage,
        Self::AppCaches,
        Self::Trash,
        Self::System,
        Self::LargeFiles,
        Self::Artifacts,
        Self::Caches,
        Self::Toolchains,
        Self::Docker,
        Self::Repos,
        Self::Duplicates,
        Self::History,
        Self::Issues,
    ];

    /// Whether this section is only shown when developer items are enabled.
    pub fn is_developer(self) -> bool {
        matches!(
            self,
            Self::Artifacts | Self::Caches | Self::Toolchains | Self::Docker | Self::Repos
        )
    }

    /// Sidebar sections visible for the current developer-mode setting.
    pub fn visible(developer: bool) -> impl Iterator<Item = Self> {
        Self::ALL
            .into_iter()
            .filter(move |tab| developer || !tab.is_developer())
    }

    /// The next visible section in the sidebar, wrapping around.
    pub fn next_for(self, developer: bool) -> Self {
        let tabs: Vec<Self> = Self::visible(developer).collect();
        let index = tabs.iter().position(|&tab| tab == self).unwrap_or(0);
        tabs[(index + 1) % tabs.len()]
    }

    /// The previous visible section in the sidebar, wrapping around.
    pub fn previous_for(self, developer: bool) -> Self {
        let tabs: Vec<Self> = Self::visible(developer).collect();
        let index = tabs.iter().position(|&tab| tab == self).unwrap_or(0);
        tabs[(index + tabs.len() - 1) % tabs.len()]
    }

    /// Whether rows in this section can be selected for removal. The disk explorer removes
    /// one focused entry at a time instead of a selection.
    pub fn selects(self) -> bool {
        self.grades() || matches!(self, Self::Trash | Self::LargeFiles)
    }

    /// Whether rows in this section carry a mixed safety grade, so the safety segments and
    /// Select safe apply to it. The trash is always safe and large files always need review.
    pub fn grades(self) -> bool {
        matches!(
            self,
            Self::Artifacts
                | Self::Caches
                | Self::Toolchains
                | Self::Docker
                | Self::AppCaches
                | Self::System
        )
    }

    /// Whether Select all and range selections may pick rows of this section. Large files are
    /// personal files and are only ever selected one at a time.
    pub fn bulk(self) -> bool {
        self.selects() && self != Self::LargeFiles
    }

    /// What one row of this section is called, singular and plural.
    pub fn noun(self) -> (&'static str, &'static str) {
        match self {
            Self::Overview | Self::Artifacts => ("item", "items"),
            Self::DiskUsage => ("entry", "entries"),
            Self::Caches => ("cache", "caches"),
            Self::Toolchains => ("version", "versions"),
            Self::Docker => ("object", "objects"),
            Self::Repos => ("repository", "repositories"),
            Self::AppCaches => ("cache", "caches"),
            Self::Trash => ("folder", "folders"),
            Self::System => ("cache", "caches"),
            Self::LargeFiles => ("file", "files"),
            Self::Duplicates => ("file", "files"),
            Self::History => ("cleanup", "cleanups"),
            Self::Issues => ("issue", "issues"),
        }
    }

    pub fn term(self) -> Term {
        match self {
            Self::Overview => Term::Overview,
            Self::DiskUsage => Term::DiskUsage,
            Self::Artifacts => Term::ProjectArtifacts,
            Self::Caches => Term::DeveloperCaches,
            Self::Toolchains => Term::Toolchains,
            Self::Docker => Term::Docker,
            Self::Repos => Term::GitRepositories,
            Self::AppCaches => Term::AppCaches,
            Self::Trash => Term::TrashContents,
            Self::System => Term::SystemCaches,
            Self::LargeFiles => Term::PersonalFiles,
            Self::Duplicates => Term::DuplicateFiles,
            Self::History => Term::History,
            Self::Issues => Term::Issues,
        }
    }

    pub fn icon(self) -> Icon {
        match self {
            Self::Overview => Icon::Overview,
            Self::DiskUsage => Icon::DiskUsage,
            Self::Artifacts => Icon::Artifacts,
            Self::Caches => Icon::Caches,
            Self::Toolchains => Icon::Toolchains,
            Self::Docker => Icon::Docker,
            Self::Repos => Icon::Repos,
            Self::AppCaches => Icon::AppCaches,
            Self::Trash => Icon::Trash,
            Self::System => Icon::System,
            Self::LargeFiles => Icon::LargeFiles,
            Self::Duplicates => Icon::Duplicates,
            Self::History => Icon::History,
            Self::Issues => Icon::Issues,
        }
    }

    /// The group heading the sidebar shows above this section, on the first section of a group.
    pub fn heading(self) -> Option<&'static str> {
        match self {
            Self::DiskUsage => Some("Clean up"),
            Self::Artifacts => Some("Developer"),
            Self::Duplicates => Some("Files"),
            Self::History => Some("Activity"),
            _ => None,
        }
    }

    pub fn action(self) -> Action {
        match self {
            Self::Overview => Action::Overview,
            Self::DiskUsage => Action::DiskUsage,
            Self::Artifacts => Action::Artifacts,
            Self::Caches => Action::Caches,
            Self::Toolchains => Action::Toolchains,
            Self::Docker => Action::Docker,
            Self::Repos => Action::Repos,
            Self::AppCaches => Action::AppCaches,
            Self::Trash => Action::Trash,
            Self::System => Action::System,
            Self::LargeFiles => Action::LargeFiles,
            Self::Duplicates => Action::Duplicates,
            Self::History => Action::History,
            Self::Issues => Action::Issues,
        }
    }

    pub fn label(self) -> &'static str {
        match self {
            Self::Overview => "Overview",
            Self::DiskUsage => "Disk explorer",
            Self::Artifacts => "Project artifacts",
            Self::Caches => "Developer caches",
            Self::Toolchains => "Toolchains",
            Self::Docker => "Docker",
            Self::Repos => "Git repositories",
            Self::AppCaches => "App caches",
            Self::Trash => "Trash",
            Self::System => "System",
            Self::LargeFiles => "Large files",
            Self::Duplicates => "Duplicates",
            Self::History => "History",
            Self::Issues => "Issues",
        }
    }

    /// The part of a scan that fills this section, for the ones a scan delivers.
    pub fn part(self) -> Option<Part> {
        match self {
            Self::Overview | Self::Duplicates => None,
            Self::Artifacts | Self::Issues => Some(Part::Artifacts),
            Self::Caches | Self::AppCaches => Some(Part::Caches),
            Self::Toolchains => Some(Part::Toolchains),
            Self::Docker => Some(Part::Docker),
            Self::Repos => Some(Part::Repos),
            Self::Trash => Some(Part::Trash),
            Self::System => Some(Part::System),
            Self::LargeFiles => Some(Part::Big),
            Self::DiskUsage => Some(Part::Usage),
            Self::History => Some(Part::History),
        }
    }

    pub fn status(self, activity: &Activity, options: &ScanOptions) -> &'static str {
        let step = match self {
            Self::Overview => return "Shown once every part of the scan is done",
            Self::Duplicates if options.duplicates => {
                return "Searched once the rest of the scan is on screen";
            }
            Self::Duplicates => return "Searched on request once the scan is done",
            Self::Artifacts | Self::Issues => Step::Folders,
            Self::Caches | Self::AppCaches => Step::Caches,
            Self::Toolchains => Step::Toolchains,
            Self::Docker => Step::Docker,
            Self::Repos => Step::Repos,
            Self::Trash => Step::Trash,
            Self::System => Step::System,
            Self::LargeFiles => Step::Big,
            Self::DiskUsage => Step::Usage,
            Self::History => Step::History,
        };
        let state = activity.state(step, options);
        match self {
            _ if matches!(state, State::Done(_)) => "Done, shown with the first project artifacts",
            _ if state == State::Off => match self {
                Self::Caches | Self::AppCaches => "Caches are off for this scan",
                Self::Toolchains => "Toolchains are off for this scan",
                Self::Docker => "Docker and Podman are off for this scan",
                Self::Trash => "The trash is off for this scan",
                Self::System => "System caches are off for this scan",
                Self::LargeFiles => "Large files are off for this scan",
                Self::DiskUsage => "The disk explorer is off for this scan",
                _ => "Git repositories are off for this scan",
            },
            Self::Artifacts => "Looking for project artifacts",
            Self::Issues => "Collecting issues while scanning folders",
            Self::Caches => "Measuring developer caches",
            Self::AppCaches => "Measuring app and browser caches",
            Self::Trash => "Measuring the trash",
            Self::System => "Listing system package caches",
            Self::LargeFiles => "Looking for large files and old downloads",
            Self::DiskUsage => "Measuring every folder for the disk explorer",
            Self::Toolchains => "Measuring installed toolchains",
            Self::Docker => "Asking Docker and Podman",
            Self::Repos if state == State::Pending => {
                "Noting git repositories while scanning folders"
            }
            Self::Repos => "Checking git repositories",
            _ => "Reading the cleanup history",
        }
    }
}