shdo 0.7.0

A kanban board for the terminal: tasks are Markdown files in a git repository
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
//! Per-machine app configuration: `~/.config/shdo/config.toml`.

use std::fs;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use rust_i18n::t;
use toml_edit::{DocumentMut, value};

use crate::i18n::Lang;

pub const DEFAULT_CONFIG: &str = r#"# shdo – settings for this machine (not synced)

# UI language of the board: "en" or "de" (the CLI is always English)
# language = "en"

# Data directory (git repository with the tasks), default ~/.local/share/shdo
# dir = "~/tasks"

# Editor for `E` / `shdo edit`; defaults to $VISUAL, then $EDITOR
# editor = "hx"

# Theme, also set with `t` in the board
# theme = "tokyonight"

# Bucket for new tasks when neither @bucket nor the filter decides (key, e.g. "work")
# default_bucket = "work"

# Show done tasks of the last N days (0 = all); `shdo archive` archives older ones
# done_days = 14

# Sync with the git remote when the board opens and before it closes
# (never on every change); `s` syncs in between
# auto_sync = false

# Public holidays in the calendar: country-state, e.g. "de-bw"; empty = none
# (states: bw, by, be, bb, hb, hh, he, mv, ni, nw, rp, sl, sn, st, sh, th)
# holidays = "de-bw"

# Send a task as a push message with `p` / `shdo push` (https://ntfy.sh); the topic
# works like a password, so pick a random one. Must stay at the end of the file.
# Server and topic can also be set on the settings page (,).
# [ntfy]
# server = "https://ntfy.sh"
# topic = "shdo-<random>"
# token = "tk_..."   # only for protected topics

# What the board remembers by itself (filters, shown columns, calendar) is not kept
# here but in ~/.local/state/shdo/state.toml.
"#;

#[derive(Debug, Default, serde::Deserialize)]
pub struct AppConfig {
    pub language: Option<String>,
    pub dir: Option<String>,
    pub editor: Option<String>,
    pub theme: Option<String>,
    pub auto_sync: Option<bool>,
    pub default_bucket: Option<String>,
    pub done_days: Option<u32>,
    pub holidays: Option<String>,
    pub ntfy: Option<crate::ntfy::NtfyConfig>,
}

/// `$SHDO_CONFIG_DIR`, else `$XDG_CONFIG_HOME/shdo`, else `~/.config/shdo`.
///
/// `SHDO_CONFIG_DIR` only redirects shdo; overriding `XDG_CONFIG_HOME` would also start
/// the editor (e.g. hx) without its config.
pub fn config_dir() -> PathBuf {
    let var = |name| std::env::var_os(name).filter(|v| !v.is_empty());
    if let Some(dir) = var("SHDO_CONFIG_DIR") {
        return PathBuf::from(dir);
    }
    match var("XDG_CONFIG_HOME") {
        Some(x) => PathBuf::from(x).join("shdo"),
        None => home().join(".config").join("shdo"),
    }
}

/// `$XDG_DATA_HOME/shdo`, else `~/.local/share/shdo`.
pub fn default_data_dir() -> PathBuf {
    match std::env::var_os("XDG_DATA_HOME").filter(|v| !v.is_empty()) {
        Some(x) => PathBuf::from(x).join("shdo"),
        None => home().join(".local").join("share").join("shdo"),
    }
}

pub fn config_path() -> PathBuf {
    config_dir().join("config.toml")
}

fn home() -> PathBuf {
    std::env::var_os("HOME")
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from("."))
}

pub fn expand_tilde(p: &str) -> PathBuf {
    match p.strip_prefix("~/") {
        Some(rest) => home().join(rest),
        None if p == "~" => home(),
        None => PathBuf::from(p),
    }
}

/// Default for `done_days`.
pub const DONE_DAYS_DEFAULT: u32 = 14;

impl AppConfig {
    /// `[ntfy]` with a topic, i.e. `p` / `shdo push` can send.
    pub fn ntfy(&self) -> Option<&crate::ntfy::NtfyConfig> {
        self.ntfy.as_ref().filter(|n| n.topic().is_some())
    }

    /// Days of done tasks to show; `None` = all (`done_days = 0`).
    pub fn done_days(&self) -> Option<u32> {
        match self.done_days.unwrap_or(DONE_DAYS_DEFAULT) {
            0 => None,
            n => Some(n),
        }
    }

    pub fn load(path: &Path) -> Result<AppConfig> {
        match fs::read_to_string(path) {
            Ok(raw) => toml::from_str(&raw)
                .with_context(|| t!("io.invalid", path = path.display()).into_owned()),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(AppConfig::default()),
            Err(e) => Err(e).with_context(|| t!("io.read", path = path.display()).into_owned()),
        }
    }

    /// Order: `--dir`, then `dir` from the config, then [`default_data_dir`].
    pub fn data_dir(&self, cli: Option<&Path>) -> PathBuf {
        match (cli, &self.dir) {
            (Some(p), _) => p.to_path_buf(),
            (None, Some(d)) => expand_tilde(d),
            (None, None) => default_data_dir(),
        }
    }

    /// Configured UI language, otherwise guessed from the environment.
    pub fn lang(&self) -> Lang {
        self.language
            .as_deref()
            .and_then(Lang::from_code)
            .unwrap_or_else(Lang::from_env)
    }

    pub fn editor(&self) -> String {
        self.editor
            .clone()
            .or_else(|| std::env::var("VISUAL").ok())
            .or_else(|| std::env::var("EDITOR").ok())
            .filter(|e| !e.trim().is_empty())
            .unwrap_or_else(|| "vi".into())
    }
}

/// Creates the config file from the commented template if it does not exist yet,
/// so it can be opened in an editor.
pub fn ensure_file(path: &Path) -> Result<()> {
    if path.exists() {
        return Ok(());
    }
    if let Some(dir) = path.parent() {
        fs::create_dir_all(dir)?;
    }
    fs::write(path, DEFAULT_CONFIG)
        .with_context(|| t!("io.write", path = path.display()).into_owned())
}

/// Sets a string value in the config file; comments and other values are preserved.
pub fn set_value(path: &Path, key: &str, val: &str) -> Result<()> {
    update(path, |doc| doc[key] = value(val))
}

pub fn set_int(path: &Path, key: &str, val: i64) -> Result<()> {
    update(path, |doc| doc[key] = value(val))
}

pub fn remove_key(path: &Path, key: &str) -> Result<()> {
    update(path, |doc| {
        doc.remove(key);
    })
}

pub fn set_bool(path: &Path, key: &str, val: bool) -> Result<()> {
    update(path, |doc| doc[key] = value(val))
}

/// Sets `key` in `[table]`; the table is created at the end of the file if needed.
pub fn set_in_table(path: &Path, table: &str, key: &str, val: &str) -> Result<()> {
    update(path, |doc| doc[table][key] = value(val))
}

/// Removes `key` from `[table]`, and the table once it is empty.
pub fn remove_in_table(path: &Path, table: &str, key: &str) -> Result<()> {
    update(path, |doc| {
        if let Some(t) = doc.get_mut(table).and_then(|t| t.as_table_like_mut()) {
            t.remove(key);
            if t.is_empty() {
                doc.remove(table);
            }
        }
    })
}

/// Result of the setup dialog.
pub struct Setup {
    pub language: Lang,
    /// Data directory as entered (may start with `~`).
    pub dir: String,
    /// `holidays` value such as `de-bw`; `None` = no holidays.
    pub holidays: Option<String>,
    /// ntfy (server, topic); `None` = sending switched off.
    pub ntfy: Option<(String, String)>,
}

/// Writes the setup choices; other settings and comments in an existing file stay.
pub fn write_setup(path: &Path, setup: &Setup) -> Result<()> {
    update(path, |doc| {
        doc["language"] = value(setup.language.code());
        doc["dir"] = value(setup.dir.as_str());
        match &setup.holidays {
            Some(h) => doc["holidays"] = value(h.as_str()),
            None => {
                doc.remove("holidays");
            }
        }
        match &setup.ntfy {
            Some((server, topic)) => {
                doc["ntfy"]["server"] = value(server.as_str());
                doc["ntfy"]["topic"] = value(topic.as_str());
            }
            // Without a topic nothing is sent; a token stays for later
            None => {
                if let Some(t) = doc.get_mut("ntfy").and_then(|t| t.as_table_like_mut()) {
                    t.remove("server");
                    t.remove("topic");
                    if t.is_empty() {
                        doc.remove("ntfy");
                    }
                }
            }
        }
    })
}

/// `/home/me/x` → `~/x`, for display and for storing paths in the config.
pub fn contract_tilde(p: &Path) -> String {
    let home = home();
    match p.strip_prefix(&home) {
        Ok(rest) if rest.as_os_str().is_empty() => "~".into(),
        Ok(rest) => format!("~/{}", rest.display()),
        Err(_) => p.display().to_string(),
    }
}

fn update(path: &Path, f: impl FnOnce(&mut DocumentMut)) -> Result<()> {
    update_file(path, DEFAULT_CONFIG, f)
}

/// Edits a TOML file in place (keeping comments); `template` is used if it does not exist.
pub(crate) fn update_file(
    path: &Path,
    template: &str,
    f: impl FnOnce(&mut DocumentMut),
) -> Result<()> {
    let raw = match fs::read_to_string(path) {
        Ok(raw) => raw,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => template.into(),
        Err(e) => return Err(e).with_context(|| t!("io.read", path = path.display()).into_owned()),
    };
    let mut doc: DocumentMut = raw
        .parse()
        .with_context(|| t!("io.invalid", path = path.display()).into_owned())?;
    let before: Vec<String> = doc.iter().map(|(k, _)| k.to_string()).collect();
    f(&mut doc);
    let out = place_new_keys(doc, &before);
    if let Some(dir) = path.parent() {
        fs::create_dir_all(dir)?;
    }
    fs::write(path, out).with_context(|| t!("io.write", path = path.display()).into_owned())
}

/// toml_edit puts new top-level keys at the start of the file. If the file has a
/// commented example for such a key (`# theme = "tokyonight"`), the value replaces that
/// line instead, so it sits next to its explanation.
fn place_new_keys(mut doc: DocumentMut, before: &[String]) -> String {
    let new_keys: Vec<String> = doc
        .iter()
        .map(|(k, _)| k.to_string())
        .filter(|k| !before.contains(k))
        .collect();
    let mut replacements = vec![];
    for key in new_keys {
        let example = format!("# {key} =");
        if !doc.to_string().lines().any(|l| l.starts_with(&example)) {
            continue;
        }
        if let Some(item) = doc.remove(&key)
            && let Some(v) = item.as_value()
        {
            replacements.push((example, format!("{key} = {}", v.to_string().trim())));
        }
    }
    let mut out = doc.to_string();
    for (example, line) in replacements {
        let mut done = false;
        out = out
            .lines()
            .map(|l| {
                if !done && l.starts_with(&example) {
                    done = true;
                    line.clone()
                } else {
                    l.to_string()
                }
            })
            .collect::<Vec<_>>()
            .join("\n")
            + "\n";
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn missing_file_is_default() {
        let tmp = tempfile::tempdir().unwrap();
        let cfg = AppConfig::load(&tmp.path().join("nope.toml")).unwrap();
        assert!(cfg.dir.is_none());
        assert_eq!(cfg.data_dir(Some(Path::new("/x"))), PathBuf::from("/x"));
    }

    #[test]
    fn new_values_replace_their_commented_example() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("config.toml");
        set_value(&path, "theme", "nord").unwrap();
        set_bool(&path, "auto_sync", true).unwrap();
        set_value(&path, "theme", "gruvbox-dark").unwrap();
        let raw = fs::read_to_string(&path).unwrap();
        assert!(raw.starts_with("# shdo"), "header stays first:\n{raw}");
        assert!(raw.contains("# Theme, also set with `t` in the board\ntheme = \"gruvbox-dark\""));
        assert!(!raw.contains("# theme ="));
        assert!(raw.contains("auto_sync = true"));
        // a key without a commented example still works
        set_value(&path, "custom", "x").unwrap();
        let cfg = AppConfig::load(&path).unwrap();
        assert_eq!(cfg.theme.as_deref(), Some("gruvbox-dark"));
        assert_eq!(cfg.auto_sync, Some(true));
        assert!(
            fs::read_to_string(&path)
                .unwrap()
                .contains("custom = \"x\"")
        );
    }

    #[test]
    fn setup_is_written_and_read_back() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("config.toml");
        let mut setup = Setup {
            language: Lang::De,
            dir: "~/notes/tasks".into(),
            holidays: Some("de-by".into()),
            ntfy: Some(("https://ntfy.sh".into(), "shdo-abc".into())),
        };
        write_setup(&path, &setup).unwrap();
        let cfg = AppConfig::load(&path).unwrap();
        assert_eq!(cfg.ntfy().unwrap().topic(), Some("shdo-abc"));
        setup.ntfy = None;
        write_setup(&path, &setup).unwrap();
        let cfg = AppConfig::load(&path).unwrap();
        assert_eq!(cfg.lang(), Lang::De);
        assert_eq!(cfg.data_dir(None), home().join("notes/tasks"));
        assert_eq!(cfg.holidays.as_deref(), Some("de-by"));
        setup.holidays = None;
        write_setup(&path, &setup).unwrap();
        assert!(AppConfig::load(&path).unwrap().holidays.is_none());
        assert!(
            fs::read_to_string(&path)
                .unwrap()
                .contains("# Data directory")
        );
        assert_eq!(contract_tilde(&home().join("a/b")), "~/a/b");
    }

    #[test]
    fn table_values_go_below_the_top_level_keys() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("config.toml");
        set_value(&path, "theme", "nord").unwrap();
        set_in_table(&path, "ntfy", "topic", "shdo-abc").unwrap();
        set_value(&path, "language", "de").unwrap();
        let cfg = AppConfig::load(&path).unwrap();
        assert_eq!(cfg.ntfy().unwrap().topic(), Some("shdo-abc"));
        assert_eq!(cfg.lang(), Lang::De);
        assert_eq!(cfg.theme.as_deref(), Some("nord"));
        remove_in_table(&path, "ntfy", "topic").unwrap();
        let cfg = AppConfig::load(&path).unwrap();
        assert!(cfg.ntfy.is_none());
        assert!(!fs::read_to_string(&path).unwrap().contains("\n[ntfy]"));
    }

    #[test]
    fn set_value_keeps_comments() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("sub/config.toml");
        set_value(&path, "theme", "nord").unwrap();
        fs::write(
            &path,
            fs::read_to_string(&path).unwrap() + "dir = \"~/t\" # mein Repo\n",
        )
        .unwrap();
        set_value(&path, "theme", "gruvbox").unwrap();
        let raw = fs::read_to_string(&path).unwrap();
        assert!(raw.contains("# Data directory"));
        assert!(raw.contains("# mein Repo"));
        let cfg = AppConfig::load(&path).unwrap();
        assert_eq!(cfg.theme.as_deref(), Some("gruvbox"));
        assert_eq!(cfg.data_dir(None), home().join("t"));
        set_value(&path, "default_bucket", "work").unwrap();
        let cfg = AppConfig::load(&path).unwrap();
        assert_eq!(cfg.default_bucket.as_deref(), Some("work"));
        assert_eq!(cfg.theme.as_deref(), Some("gruvbox"));
    }
}