Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions llmfit-tui/src/filter_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

/// Persisted filter state, saved to `~/.config/llmfit/filters.json`.
///
/// Every field is optional so the file degrades gracefully when new filters are
/// added or the model database changes between runs. Multi-select filters are
/// stored as `name -> selected` maps so additions/removals in the model list
/// don't corrupt saved state.
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct FilterConfig {
pub fit_filter: Option<String>,
pub availability_filter: Option<String>,
pub tp_filter: Option<String>,
pub sort_column: Option<String>,
pub sort_ascending: Option<bool>,
pub installed_first: Option<bool>,
pub search_query: Option<String>,

// Multi-select popup filters: name → selected
pub providers: Option<HashMap<String, bool>>,
pub use_cases: Option<HashMap<String, bool>>,
pub capabilities: Option<HashMap<String, bool>>,
pub quants: Option<HashMap<String, bool>>,
pub run_modes: Option<HashMap<String, bool>>,
pub params_buckets: Option<HashMap<String, bool>>,
pub licenses: Option<HashMap<String, bool>>,
pub runtimes: Option<HashMap<String, bool>>,
}

impl FilterConfig {
/// Path to the config file: `~/.config/llmfit/filters.json`
fn config_path() -> Option<PathBuf> {
let home = std::env::var("HOME")
.or_else(|_| std::env::var("USERPROFILE"))
.ok()?;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like Theme uses the same HOME/USERPROFILE approach so this is consistent. If you ever want to align with platform conventions (~/Library/Application Support on macOS, XDG on Linux), the dirs crate handles that nicely. No blocker though.

Some(
PathBuf::from(home)
.join(".config")
.join("llmfit")
.join("filters.json"),
)
}

/// Load the saved filter config from disk, falling back to defaults.
pub fn load() -> Self {
Self::config_path()
.and_then(|path| fs::read_to_string(path).ok())
.and_then(|s| serde_json::from_str(&s).ok())
.unwrap_or_default()
}

/// Save the current filter config to disk.
pub fn save(&self) {
if let Some(path) = Self::config_path() {
if let Some(parent) = path.parent() {
let _ = fs::create_dir_all(parent);
}
if let Ok(json) = serde_json::to_string_pretty(self) {
let _ = fs::write(&path, json);
}
}
}

/// Apply a saved name→selected map onto a positional `Vec<bool>`,
/// matching by the corresponding names vector. Entries not present
/// in the saved map keep their current (default) value.
pub fn apply_map(names: &[String], selected: &mut [bool], saved: &HashMap<String, bool>) {
for (i, name) in names.iter().enumerate() {
if let Some(&val) = saved.get(name) {
selected[i] = val;
}
}
}

/// Build a name→selected map from parallel name and selected slices.
pub fn build_map(names: &[String], selected: &[bool]) -> HashMap<String, bool> {
names
.iter()
.zip(selected.iter())
.map(|(name, &sel)| (name.clone(), sel))
.collect()
}
}
1 change: 1 addition & 0 deletions llmfit-tui/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod display;
mod filter_config;
mod serve_api;
mod theme;
mod tui_app;
Expand Down
Loading
Loading