use crate::state::StateStore;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SortMode {
#[default]
Priority,
Project,
Recency,
Natural,
}
impl SortMode {
pub fn next(self) -> Self {
match self {
SortMode::Priority => SortMode::Project,
SortMode::Project => SortMode::Recency,
SortMode::Recency => SortMode::Natural,
SortMode::Natural => SortMode::Priority,
}
}
pub fn label(&self) -> &'static str {
match self {
SortMode::Priority => "priority",
SortMode::Project => "project",
SortMode::Recency => "recency",
SortMode::Natural => "natural",
}
}
fn as_str(&self) -> &'static str {
match self {
SortMode::Priority => "priority",
SortMode::Project => "project",
SortMode::Recency => "recency",
SortMode::Natural => "natural",
}
}
fn from_str(s: &str) -> Self {
match s.trim().to_lowercase().as_str() {
"project" => SortMode::Project,
"recency" => SortMode::Recency,
"natural" => SortMode::Natural,
_ => SortMode::Priority, }
}
pub fn load() -> Self {
StateStore::new()
.ok()
.and_then(|store| store.load_settings().ok())
.map(|s| Self::from_str(&s.sort_mode))
.unwrap_or_default()
}
pub fn save(&self) {
if let Ok(store) = StateStore::new()
&& let Ok(mut settings) = store.load_settings()
{
settings.sort_mode = self.as_str().to_string();
let _ = store.save_settings(&settings);
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WorktreeSortMode {
#[default]
Natural,
Age,
}
impl WorktreeSortMode {
pub fn next(self) -> Self {
match self {
WorktreeSortMode::Natural => WorktreeSortMode::Age,
WorktreeSortMode::Age => WorktreeSortMode::Natural,
}
}
pub fn label(&self) -> &'static str {
match self {
WorktreeSortMode::Natural => "natural",
WorktreeSortMode::Age => "age",
}
}
fn as_str(&self) -> &'static str {
self.label()
}
fn from_str(s: &str) -> Self {
match s.trim().to_lowercase().as_str() {
"age" => WorktreeSortMode::Age,
_ => WorktreeSortMode::Natural,
}
}
pub fn load() -> Self {
StateStore::new()
.ok()
.and_then(|store| store.load_settings().ok())
.and_then(|s| s.worktree_sort_mode)
.map(|s| Self::from_str(&s))
.unwrap_or_default()
}
pub fn save(&self) {
if let Ok(store) = StateStore::new()
&& let Ok(mut settings) = store.load_settings()
{
settings.worktree_sort_mode = Some(self.as_str().to_string());
let _ = store.save_settings(&settings);
}
}
}