1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
use super::App;
impl App {
pub(super) fn update_config_file<F>(&mut self, error_context: &str, update: F) -> bool
where
F: FnOnce(&str) -> String,
{
#[cfg(test)]
if std::env::var_os(crate::config::CONFIG_PATH_ENV_VAR).is_none() {
return false;
}
let path = crate::config::config_path();
if let Some(parent) = path.parent() {
if let Err(err) = std::fs::create_dir_all(parent) {
crate::logging::config_write_failed(&path, error_context, &err.to_string());
self.state.config_diagnostic =
Some(format!("failed to save {error_context}: {err}"));
self.config_diagnostic_deadline =
Some(std::time::Instant::now() + std::time::Duration::from_secs(5));
return false;
}
}
let content = std::fs::read_to_string(&path).unwrap_or_default();
let new_content = update(&content);
if let Err(err) = std::fs::write(&path, new_content) {
crate::logging::config_write_failed(&path, error_context, &err.to_string());
self.state.config_diagnostic = Some(format!("failed to save {error_context}: {err}"));
self.config_diagnostic_deadline =
Some(std::time::Instant::now() + std::time::Duration::from_secs(5));
return false;
}
true
}
pub(super) fn mark_onboarding_complete(&mut self) {
self.update_config_file("onboarding setting", |content| {
crate::config::upsert_top_level_bool(content, "onboarding", false)
});
}
pub(super) fn save_theme(&mut self, name: &str) {
if self.update_config_file("theme", |content| {
let content = crate::config::upsert_section_value(
content,
"theme",
"name",
&format!("\"{name}\""),
);
crate::config::upsert_section_bool(&content, "theme", "auto_switch", false)
}) {
self.apply_config_from_disk(false);
}
}
pub(super) fn save_sound(&mut self, enabled: bool) {
if self.update_config_file("sound setting", |content| {
crate::config::upsert_section_bool(content, "ui.sound", "enabled", enabled)
}) {
self.apply_config_from_disk(false);
}
}
pub(super) fn save_toast_delivery(&mut self, delivery: crate::config::ToastDelivery) {
let value = match delivery {
crate::config::ToastDelivery::Off => "\"off\"",
crate::config::ToastDelivery::Herdr => "\"herdr\"",
crate::config::ToastDelivery::Terminal => "\"terminal\"",
crate::config::ToastDelivery::System => "\"system\"",
};
if self.update_config_file("toast setting", |content| {
let content =
crate::config::upsert_section_value(content, "ui.toast", "delivery", value);
crate::config::remove_section_key(&content, "ui.toast", "enabled")
}) {
self.apply_config_from_disk(false);
}
}
pub(super) fn save_agent_border_labels(&mut self, enabled: bool) {
if self.update_config_file("agent border labels", |content| {
crate::config::upsert_section_bool(
content,
"ui",
"show_agent_labels_on_pane_borders",
enabled,
)
}) {
self.apply_config_from_disk(false);
}
}
pub(super) fn save_pane_history_persistence(&mut self, enabled: bool) {
if self.update_config_file("pane screen history", |content| {
crate::config::upsert_section_bool(content, "experimental", "pane_history", enabled)
}) {
self.apply_config_from_disk(false);
}
}
pub(super) fn save_switch_ascii_input_source_in_prefix(&mut self, enabled: bool) {
if self.update_config_file("prefix ascii input source", |content| {
crate::config::upsert_section_bool(
content,
"experimental",
"switch_ascii_input_source_in_prefix",
enabled,
)
}) {
self.apply_config_from_disk(false);
}
}
pub(super) fn save_agent_panel_sort(&mut self, sort: crate::app::state::AgentPanelSort) {
let value = match sort {
crate::app::state::AgentPanelSort::Spaces => {
crate::config::AgentPanelSortConfig::Spaces.as_str()
}
crate::app::state::AgentPanelSort::Priority => {
crate::config::AgentPanelSortConfig::Priority.as_str()
}
};
if self.update_config_file("agent panel sort", |content| {
crate::config::upsert_section_value(
content,
"ui",
"agent_panel_sort",
&format!("\"{value}\""),
)
}) {
self.apply_config_from_disk(false);
}
}
}
|