gitpoulpi-config 1.4.0

GitPoulpi config schema, TOML load/save, defaults, and OS-correct path resolution.
Documentation
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//! Writers and the resolved view of `desktop.toml` (see `crate::split` for the
//! three-file design and `schema::desktop_ui` for the schema).
//!
//! Every writer is a **sparse** read-modify-write of the document, like the
//! ones for `config.toml`: it validates the edited document as a
//! [`DesktopConfig`] before anything is written, writes atomically (private
//! temporary file, then rename), keeps keys it does not know, and never
//! touches `config.toml` or `tui.toml`. A first write creates the file without
//! clobbering one a racing process just created. Writes of this process are
//! serialized by the same lock as the other config writers.
//!
//! Comments are not preserved (the document is re-serialized from
//! `toml::Value`), exactly as for `config.toml`.
//!
//! The desktop calls these through [`DesktopConfig`]'s associated functions:
//! [`DesktopConfig::set_in_file`], [`DesktopConfig::set_window`],
//! [`DesktopConfig::set_panes`], [`DesktopConfig::set_column_widths`], ...

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use crate::schema::{coerce_desktop, DesktopColumnWidths, DesktopConfig, DesktopUiConfig};
use crate::{insert_dotted, json_to_toml, remove_dotted};
use crate::{ConfigPaths, Error, Result};

/// The `desktop.ui.*` keys some desktop component reads and an environment
/// variable (`GITPOULPI_DESKTOP_UI_<KEY>`, the same mechanical rule as
/// `GITPOULPI_UI_*`) may therefore override: the per-interface keys of
/// [`crate::LIVE_KEYS`]. A test keeps the two lists in step.
pub const DESKTOP_LIVE_KEYS: &[&str] = &[
    "desktop.ui.language",
    "desktop.ui.theme",
    "desktop.ui.translate_git_terms",
    "desktop.ui.git_terms_english",
    "desktop.ui.date_format",
    "desktop.ui.date_locale",
    "desktop.ui.date_time_format",
    "desktop.ui.date_verbose_format",
    "desktop.ui.graph_lane_background",
    "desktop.ui.graph_row_tint_strength",
    "desktop.ui.toolbar_position",
    "desktop.ui.toolbar_align",
    "desktop.ui.dialog_geometry",
    "desktop.ui.toolbar_pr_pinned",
    "desktop.ui.statusbar_position",
    "desktop.ui.tabs_position",
    "desktop.ui.graph_color_theme",
    "desktop.ui.ref_tree",
    "desktop.ui.graph_show_stashes",
    "desktop.ui.ref_connector",
    "desktop.ui.log_wip_counts_in_message",
    "desktop.ui.reveal_on_ref_click",
    "desktop.ui.tree_folders_first",
    "desktop.ui.graph_ghost_ref",
    "desktop.ui.allow_agent_view_control",
    "desktop.ui.tab_dir_follow",
    "desktop.ui.reuse_window",
    "desktop.ui.image_view",
    "desktop.ui.image_view_max_mib",
    "desktop.ui.image_view_max_unit",
    "desktop.ui.image_view_pixelated_from",
    "desktop.ui.image_view_sync",
    "desktop.ui.show_avatars",
    "desktop.ui.avatars",
    "desktop.ui.avatar_fallback",
    "desktop.ui.composer_files",
    "desktop.ui.sidebar_sections_scope",
    "desktop.ui.blame_side",
    "desktop.ui.column_resize",
    "desktop.ui.log_columns_layout",
    "desktop.ui.diff_layout",
    "desktop.ui.pr_terminology",
    "desktop.ui.hide_finished_pr_chip",
    "desktop.ui.diff_hunks_only",
    "desktop.ui.diff_show_whitespace",
    "desktop.ui.diff_word_wrap",
    "desktop.ui.diff_intraline",
    "desktop.ui.minimap",
];

/// The prefix every `desktop.toml` key carries in `config get|set`.
pub const KEY_PREFIX: &str = "desktop.";

/// `key` without [`KEY_PREFIX`] when it names a `desktop.toml` key.
pub fn strip_prefix(key: &str) -> Option<&str> {
    key.strip_prefix(KEY_PREFIX)
}

/// Whether some desktop component reads `key` (`desktop.ui.theme`): the
/// desktop reads its own window, pane and column keys as well.
pub fn key_is_implemented(key: &str) -> bool {
    DESKTOP_LIVE_KEYS.contains(&key)
        || key.starts_with("desktop.keybindings.")
        || strip_prefix(key).is_some_and(|k| {
            k.strip_prefix("ui.").is_some_and(|k| {
                crate::DESKTOP_ONLY_KEYS
                    .iter()
                    .any(|o| k == *o || k.strip_prefix(o).is_some_and(|r| r.starts_with('.')))
            })
        })
}

fn bad(key: &str) -> impl FnOnce(toml::de::Error) -> Error {
    let key = key.to_string();
    move |err| Error::BadValue {
        key,
        value: "<value>".to_string(),
        expected: err.to_string(),
    }
}

/// The one sparse read-modify-write of `desktop.toml`. `edit` may run twice
/// when a racing process created the file first. `clamp`: out-of-range pixel
/// values are pulled into range instead of refused.
fn edit_document(
    path: &Path,
    label: &str,
    clamp: bool,
    edit: impl Fn(&mut toml::Value) -> Result<()>,
) -> Result<DesktopConfig> {
    let _guard = crate::split::lock_writes();
    for _ in 0..2 {
        let existed = path.exists();
        let mut doc = crate::split::read_doc(path)?;
        edit(&mut doc)?;
        let mut config: DesktopConfig = doc.clone().try_into().map_err(bad(label))?;
        if !clamp {
            config.validate()?;
        }
        config.sanitize();
        let body = toml::to_string_pretty(&doc)?;
        if let Some(parent) = path.parent() {
            crate::secure_fs::create_private_dir_all(parent)?;
        }
        if existed {
            crate::secure_fs::write_private(path, body.as_bytes())?;
            return Ok(config);
        }
        // No clobber: a process that created the file meanwhile wins, and the
        // edit is replayed on top of what it wrote.
        if crate::split::create_new_file(path, body.as_bytes())? {
            return Ok(config);
        }
    }
    Err(Error::Io(std::io::Error::other(
        "desktop.toml kept changing while it was written",
    )))
}

fn int(v: u32) -> toml::Value {
    toml::Value::Integer(i64::from(v))
}

fn table(entries: Vec<(&str, toml::Value)>) -> toml::Value {
    toml::Value::Table(
        entries
            .into_iter()
            .map(|(k, v)| (k.to_string(), v))
            .collect(),
    )
}

fn widths_value(w: DesktopColumnWidths) -> toml::Value {
    table(vec![
        ("ref_w", int(w.ref_w)),
        ("graph_w", int(w.graph_w)),
        ("author_w", int(w.author_w)),
        ("date_w", int(w.date_w)),
        ("sha_w", int(w.sha_w)),
    ])
}

/// Whether the document at `path` says `key` (dotted).
fn file_says(path: &Path, key: &str) -> Result<bool> {
    let doc = crate::split::read_doc(path)?;
    let mut cursor = &doc;
    for segment in key.split('.') {
        match cursor.get(segment) {
            Some(next) => cursor = next,
            None => return Ok(false),
        }
    }
    Ok(true)
}

impl DesktopConfig {
    /// Sets one dotted key relative to `desktop.toml` (`ui.theme`,
    /// `ui.column_widths_px.ref_w`, `keybindings.<ctx>.<action>`) from text,
    /// as `gitpoulpi config set desktop.ui.theme ...` does. A value outside a
    /// pixel range is refused. Returns the value as stored.
    pub fn set_in_file(paths: &ConfigPaths, key: &str, value: &str) -> Result<serde_json::Value> {
        if let Some(rest) = key.strip_prefix("keybindings.") {
            let (ctx, action) = rest
                .split_once('.')
                .ok_or_else(|| Error::UnknownKey(key.to_string()))?;
            let chords = crate::keybindings::chords_from_cli(value);
            Self::set_keybinding(paths, ctx, action, &chords)?;
            return Ok(serde_json::json!(chords));
        }
        if key.split('.').next() == Some("repos") {
            return Err(Error::UnknownKey(key.to_string()));
        }
        let schema_value = DesktopConfig::default().get(key)?;
        let coerced = coerce_desktop(key, &schema_value, value)?;
        let config = edit_document(&paths.desktop_file(), key, false, |doc| {
            insert_dotted(doc, key, json_to_toml(coerced.clone()))
        })
        .map_err(|err| match err {
            Error::BadValue {
                key: k,
                value: v,
                expected,
            } => Error::BadValue {
                key: k,
                value: if v == "<value>" { value.to_string() } else { v },
                expected,
            },
            other => other,
        })?;
        config.get(key)
    }

    /// Writes a list of strings (`ui.log_columns_hidden`, ...); an empty list
    /// is written as `[]` (the user chose "none").
    pub fn set_list_in_file(paths: &ConfigPaths, key: &str, items: &[String]) -> Result<()> {
        if !matches!(
            DesktopConfig::default().get(key)?,
            serde_json::Value::Array(_)
        ) {
            return Err(Error::UnsettableKey {
                key: key.to_string(),
                kind: "value",
            });
        }
        let value = toml::Value::Array(items.iter().cloned().map(toml::Value::String).collect());
        edit_document(&paths.desktop_file(), key, false, |doc| {
            insert_dotted(doc, key, value.clone())
        })
        .map(|_| ())
    }

    /// Removes `key` (back to its default); a no-op when the file does not
    /// say it. An emptied table is dropped too.
    pub fn unset_in_file(paths: &ConfigPaths, key: &str) -> Result<()> {
        DesktopConfig::default().get(key)?;
        Self::remove_if_present(paths, key)
    }

    /// Remembers the window: size (clamped), position (`None` leaves the
    /// window manager to place it) and whether it is maximized.
    pub fn set_window(
        paths: &ConfigPaths,
        size: (u32, u32),
        position: Option<(i32, i32)>,
        maximized: bool,
    ) -> Result<()> {
        let mut ui = DesktopUiConfig {
            window_width: size.0,
            window_height: size.1,
            window_x: position.map(|p| p.0),
            window_y: position.map(|p| p.1),
            ..Default::default()
        };
        ui.sanitize();
        edit_document(&paths.desktop_file(), "ui.window_width", true, |doc| {
            insert_dotted(doc, "ui.window_width", int(ui.window_width))?;
            insert_dotted(doc, "ui.window_height", int(ui.window_height))?;
            insert_dotted(doc, "ui.window_maximized", toml::Value::Boolean(maximized))?;
            match (ui.window_x, ui.window_y) {
                (Some(x), Some(y)) => {
                    insert_dotted(doc, "ui.window_x", toml::Value::Integer(i64::from(x)))?;
                    insert_dotted(doc, "ui.window_y", toml::Value::Integer(i64::from(y)))
                }
                _ => {
                    remove_dotted(doc, "ui.window_x");
                    remove_dotted(doc, "ui.window_y");
                    Ok(())
                }
            }
        })
        .map(|_| ())
    }

    /// Remembers the side panes: visibility and widths (clamped).
    pub fn set_panes(
        paths: &ConfigPaths,
        left_visible: bool,
        right_visible: bool,
        left_width: u32,
        right_width: u32,
    ) -> Result<()> {
        let mut ui = DesktopUiConfig {
            left_pane_width: left_width,
            right_pane_width: right_width,
            ..Default::default()
        };
        ui.sanitize();
        edit_document(&paths.desktop_file(), "ui.left_pane_width", true, |doc| {
            insert_dotted(
                doc,
                "ui.left_pane_visible",
                toml::Value::Boolean(left_visible),
            )?;
            insert_dotted(
                doc,
                "ui.right_pane_visible",
                toml::Value::Boolean(right_visible),
            )?;
            insert_dotted(doc, "ui.left_pane_width", int(ui.left_pane_width))?;
            insert_dotted(doc, "ui.right_pane_width", int(ui.right_pane_width))
        })
        .map(|_| ())
    }

    /// Remembers Log column widths (clamped): the default for every
    /// repository with `repo == None`, else that repository's own.
    pub fn set_column_widths(
        paths: &ConfigPaths,
        repo: Option<&str>,
        widths: DesktopColumnWidths,
    ) -> Result<()> {
        let value = widths_value(widths.sanitized());
        let not_a_table = || Error::UnsettableKey {
            key: "repos".into(),
            kind: "value",
        };
        edit_document(&paths.desktop_file(), "ui.column_widths_px", true, |doc| {
            let Some(repo) = repo else {
                return insert_dotted(doc, "ui.column_widths_px", value.clone());
            };
            let root = doc.as_table_mut().ok_or_else(not_a_table)?;
            let repos = root
                .entry("repos")
                .or_insert_with(|| toml::Value::Table(Default::default()))
                .as_table_mut()
                .ok_or_else(not_a_table)?;
            let entry = repos
                .entry(repo)
                .or_insert_with(|| toml::Value::Table(Default::default()))
                .as_table_mut()
                .ok_or_else(not_a_table)?;
            entry.insert("column_widths_px".into(), value.clone());
            Ok(())
        })
        .map(|_| ())
    }

    /// Forgets `repo`'s own column widths (it follows the default again). A
    /// repository entry left empty is dropped.
    pub fn reset_column_widths(paths: &ConfigPaths, repo: &str) -> Result<()> {
        let path = paths.desktop_file();
        let doc = crate::split::read_doc(&path)?;
        if doc
            .get("repos")
            .and_then(|r| r.get(repo))
            .and_then(|r| r.get("column_widths_px"))
            .is_none()
        {
            return Ok(());
        }
        edit_document(&path, "repos", true, |doc| {
            if let Some(repos) = doc.get_mut("repos").and_then(toml::Value::as_table_mut) {
                if let Some(entry) = repos.get_mut(repo).and_then(toml::Value::as_table_mut) {
                    entry.remove("column_widths_px");
                    if entry.is_empty() {
                        repos.remove(repo);
                    }
                }
                if repos.is_empty() {
                    if let Some(root) = doc.as_table_mut() {
                        root.remove("repos");
                    }
                }
            }
            Ok(())
        })
        .map(|_| ())
    }

    /// Sets `[keybindings.<ctx>] <action>`: one chord as a string, several (or
    /// none: unbound) as an array.
    pub fn set_keybinding(
        paths: &ConfigPaths,
        ctx: &str,
        action: &str,
        chords: &[String],
    ) -> Result<()> {
        let key = format!("keybindings.{ctx}.{action}");
        crate::keybindings::check_name(&key, ctx)?;
        crate::keybindings::check_name(&key, action)?;
        let value = match chords {
            [one] => toml::Value::String(one.clone()),
            many => toml::Value::Array(many.iter().cloned().map(toml::Value::String).collect()),
        };
        edit_document(&paths.desktop_file(), "keybindings", true, |doc| {
            insert_dotted(doc, &key, value.clone())
        })
        .map(|_| ())
    }

    /// Removes one binding (back to its default); emptied tables go too.
    pub fn reset_keybinding(paths: &ConfigPaths, ctx: &str, action: &str) -> Result<()> {
        Self::remove_if_present(paths, &format!("keybindings.{ctx}.{action}"))
    }

    /// Removes the whole `[keybindings]` table.
    pub fn reset_all_keybindings(paths: &ConfigPaths) -> Result<()> {
        Self::remove_if_present(paths, "keybindings")
    }

    fn remove_if_present(paths: &ConfigPaths, key: &str) -> Result<()> {
        let path = paths.desktop_file();
        if !file_says(&path, key)? {
            return Ok(());
        }
        edit_document(&path, key, true, |doc| {
            remove_dotted(doc, key);
            Ok(())
        })
        .map(|_| ())
    }
}

/// `desktop.toml` resolved like [`crate::Settings`]: built-in defaults, then
/// the file, then `GITPOULPI_DESKTOP_UI_*` environment variables (only for
/// [`DESKTOP_LIVE_KEYS`]). Keys are the full dotted spelling
/// (`desktop.ui.theme`).
#[derive(Debug)]
pub struct DesktopSettings {
    config: DesktopConfig,
    file: PathBuf,
    raw_file: Option<toml::Value>,
    overridden: BTreeMap<String, ()>,
}

impl DesktopSettings {
    /// Against the real process environment.
    pub fn resolve(paths: &ConfigPaths) -> Result<Self> {
        Self::resolve_from(&paths.desktop_file(), |name| std::env::var(name).ok())
    }

    /// The testable core (`env` is a lookup, see `Settings::resolve_from`).
    pub fn resolve_from(file: &Path, env: impl Fn(&str) -> Option<String>) -> Result<Self> {
        let raw_file = if file.exists() {
            let raw = std::fs::read_to_string(file)?;
            Some(toml::from_str::<toml::Value>(&raw)?)
        } else {
            None
        };
        let mut config = DesktopConfig::load_file(file)?;
        let mut overridden = BTreeMap::new();
        for key in DESKTOP_LIVE_KEYS {
            if let Some(value) = env(&crate::env_var_for(key)) {
                let relative = strip_prefix(key).unwrap_or(key);
                config.set(relative, &value).map_err(|err| match err {
                    Error::BadValue {
                        value, expected, ..
                    } => Error::BadValue {
                        key: crate::env_var_for(key),
                        value,
                        expected,
                    },
                    other => other,
                })?;
                overridden.insert((*key).to_string(), ());
            }
        }
        Ok(Self {
            config,
            file: file.to_path_buf(),
            raw_file,
            overridden,
        })
    }

    pub fn config(&self) -> &DesktopConfig {
        &self.config
    }

    /// The `desktop.toml` this resolved against, whether or not it exists.
    pub fn file(&self) -> &Path {
        &self.file
    }

    /// The value in force for a full key (`desktop.ui.theme`).
    pub fn get(&self, key: &str) -> Result<serde_json::Value> {
        self.config
            .get(strip_prefix(key).ok_or_else(|| Error::UnknownKey(key.to_string()))?)
    }

    /// Which layer supplied the value in force for `key`.
    pub fn source_of(&self, key: &str) -> crate::Source {
        if self.overridden.contains_key(key) {
            return crate::Source::Env;
        }
        let mut cursor = match &self.raw_file {
            Some(doc) => doc,
            None => return crate::Source::Default,
        };
        for segment in strip_prefix(key).unwrap_or(key).split('.') {
            match cursor.get(segment) {
                Some(next) => cursor = next,
                None => return crate::Source::Default,
            }
        }
        crate::Source::File
    }
}