use super::*;
pub(crate) fn encode_view(app: &App) -> String {
let mut parts: Vec<String> = Vec::new();
if !app.view.filter().is_empty() {
parts.push(format!("filter={}", app.view.filter().text()));
}
parts.push(format!(
"sort={}:{}",
app.view.sort_key().label(),
if app.view.sort_desc() { "desc" } else { "asc" }
));
parts.push(format!("grouped={}", app.view.grouped()));
let scope = match app.scope {
Scope::Envs => "envs",
Scope::Apps => "apps",
};
parts.push(format!("scope={scope}"));
parts.join(";")
}
pub fn encode_filter_only_view(filter: &str) -> String {
format!("filter={filter}")
}
pub fn view_filter_value(encoded: &str) -> &str {
for part in encoded.split(';') {
if let Some(rest) = part.trim().strip_prefix("filter=") {
return rest;
}
}
""
}
pub(crate) fn apply_view(app: &mut App, snap: &str) {
let mut new_filter = String::new();
for part in snap.split(';') {
let Some((k, v)) = part.split_once('=') else {
continue;
};
match k.trim() {
"filter" => new_filter = v.trim().to_string(),
"sort" => {
let (key, desc) = parse_sort(Some(v.trim()));
app.set_sort(key, desc);
}
"grouped" => app.view.set_grouped(v.trim().eq_ignore_ascii_case("true")),
"scope" => {
app.scope = match v.trim() {
"apps" => Scope::Apps,
_ => Scope::Envs,
};
}
_ => {}
}
}
app.view.set_filter(new_filter);
app.resort_envs(); }