sniplab 0.3.2

A filesystem-native snippet library and agent-friendly CLI, installed as `snip`
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
use std::env;
use std::io::{self, BufRead, IsTerminal, Write};
use std::path::{Path, PathBuf};

use snip::Library;
use snip::config::{AppConfig, config_path};
use snip::error::{Result, SnipError};

use crate::cli::{Cli, ColorMode, InitArgs, OutputMode};

pub struct Outcome {
    pub library: Library,
}

enum Start {
    Choose,
    Create,
}

/// Runs the interactive setup. Returns `Ok(None)` when the user cancels.
pub fn run(config: &AppConfig, color: ColorMode) -> Result<Option<Outcome>> {
    run_with(config, color, Start::Choose, None, false)
}

pub fn run_init(config: &AppConfig, args: &InitArgs, color: ColorMode) -> Result<Option<Outcome>> {
    run_with(config, color, Start::Create, args.name.as_deref(), args.git)
}

/// True when a wizard is allowed to run at all.
pub fn is_interactive(cli: &Cli, output: OutputMode) -> bool {
    // `Cli::library` includes SNIP_LIBRARY through clap's `env` declaration.
    output == OutputMode::Human
        && io::stdin().is_terminal()
        && io::stdout().is_terminal()
        && cli.library.is_none()
}

pub fn is_init_interactive(args: &InitArgs, output: OutputMode) -> bool {
    args.path.is_none()
        && !args.yes
        && output == OutputMode::Human
        && io::stdin().is_terminal()
        && io::stdout().is_terminal()
}

fn run_with(
    config: &AppConfig,
    color: ColorMode,
    start: Start,
    supplied_name: Option<&str>,
    supplied_git: bool,
) -> Result<Option<Outcome>> {
    let color = color_enabled(color);
    println!(
        "\n  snip — set up your library\n\n  A library is an ordinary folder of files. You can move it, sync it,\n  or put it in Git at any time.\n"
    );

    let create = match start {
        Start::Create => true,
        Start::Choose => {
            println!("  1  create a new library\n  2  connect to a library I already have\n");
            match ask_choice_stdio("choice", &["create", "connect"], 0, color)? {
                Some(choice) => choice == 0,
                None => return cancelled(),
            }
        }
    };

    if create {
        create_library(config, color, supplied_name, supplied_git)
    } else {
        connect_library(config, color)
    }
}

fn create_library(
    config: &AppConfig,
    color: bool,
    supplied_name: Option<&str>,
    supplied_git: bool,
) -> Result<Option<Outcome>> {
    let path = match ask_line_stdio("path", Some("~/Main.sniplib"), color)? {
        Some(path) => expand_user_path(&path)?,
        None => return cancelled(),
    };
    if path.join("snip.toml").is_file() {
        println!("  that path already holds a snip library — connecting to it instead.");
        return finish_connected(config, color, &path);
    }
    let default_name = path
        .file_stem()
        .and_then(|value| value.to_str())
        .filter(|value| !value.is_empty())
        .unwrap_or("Main");
    let name = match supplied_name {
        Some(name) => name.to_owned(),
        None => match ask_line_stdio("name", Some(default_name), color)? {
            Some(name) => name,
            None => return cancelled(),
        },
    };
    let set_as_default = match ask_default(config, color)? {
        Some(answer) => answer,
        None => return cancelled(),
    };
    let git_initialized = if supplied_git {
        true
    } else {
        match ask_yes_no_stdio("initialize a Git repo inside it?", false, color)? {
            Some(answer) => answer,
            None => return cancelled(),
        }
    };
    let library = Library::init(&path, Some(&name))?;
    if git_initialized {
        snip::git::init(library.root())?;
    }
    finish(config, library, set_as_default, git_initialized, true)
}

fn connect_library(config: &AppConfig, color: bool) -> Result<Option<Outcome>> {
    loop {
        let path = match ask_line_stdio("path to your .sniplib", None, color)? {
            Some(path) => expand_user_path(&path)?,
            None => return cancelled(),
        };
        if !path.join("snip.toml").is_file() {
            let display_path = absolute_path(&path)?;
            println!(
                "  no snip library there — {} has no snip.toml inside.",
                display_path.display()
            );
            continue;
        }
        let library = Library::open(&path)?;
        let set_as_default = match ask_default(config, color)? {
            Some(answer) => answer,
            None => return cancelled(),
        };
        return finish(config, library, set_as_default, false, false);
    }
}

fn finish_connected(config: &AppConfig, color: bool, path: &Path) -> Result<Option<Outcome>> {
    let library = Library::open(path)?;
    let set_as_default = match ask_default(config, color)? {
        Some(answer) => answer,
        None => return cancelled(),
    };
    finish(config, library, set_as_default, false, false)
}

fn ask_default(config: &AppConfig, color: bool) -> Result<Option<bool>> {
    if let Some(current) = &config.default_library {
        println!("  current default: {}", current.display());
        ask_yes_no_stdio("replace your current default library?", false, color)
    } else {
        ask_yes_no_stdio("set as snip's default library?", true, color)
    }
}

fn finish(
    config: &AppConfig,
    library: Library,
    set_as_default: bool,
    git_initialized: bool,
    created: bool,
) -> Result<Option<Outcome>> {
    if set_as_default {
        let mut updated = config.clone();
        updated.default_library = Some(library.root().to_path_buf());
        updated.save()?;
    }
    println!();
    println!(
        "  {:<9} {}",
        if created { "created" } else { "connected" },
        library.root().display()
    );
    if set_as_default {
        println!("  {:<9} written to {}", "default", config_path()?.display());
    } else {
        println!(
            "  {:<9} not changed — pass --library or set SNIP_LIBRARY to use this library",
            "default"
        );
    }
    if git_initialized {
        println!("  {:<9} initialized", "git");
    }
    println!();
    Ok(Some(Outcome { library }))
}

pub fn print_init_next_steps() {
    println!(
        "  {:<9} snip create --title \"First snippet\"\n            snip      open the browser\n",
        "next"
    );
}

fn cancelled<T>() -> Result<Option<T>> {
    println!("\n  cancelled — nothing was created.");
    Ok(None)
}

fn color_enabled(color: ColorMode) -> bool {
    color != ColorMode::Never && env::var_os("NO_COLOR").is_none()
}

fn prompt(label: &str, default: Option<&str>, color: bool) -> String {
    if color {
        match default {
            Some(default) => {
                format!("  \x1b[36m❯\x1b[0m {label} \x1b[2m[{default}]\x1b[0m: ")
            }
            None => format!("  \x1b[36m❯\x1b[0m {label}: "),
        }
    } else {
        match default {
            Some(default) => format!("  ❯ {label} [{default}]: "),
            None => format!("  ❯ {label}: "),
        }
    }
}

fn ask_line_stdio(label: &str, default: Option<&str>, color: bool) -> Result<Option<String>> {
    let stdin = io::stdin();
    let mut input = stdin.lock();
    ask_line_from(&mut input, label, default, color, &mut io::stdout())
}

fn ask_choice_stdio(
    label: &str,
    options: &[&str],
    default: usize,
    color: bool,
) -> Result<Option<usize>> {
    let stdin = io::stdin();
    let mut input = stdin.lock();
    ask_choice_from(
        &mut input,
        label,
        options,
        default,
        color,
        &mut io::stdout(),
    )
}

fn ask_yes_no_stdio(label: &str, default: bool, color: bool) -> Result<Option<bool>> {
    let stdin = io::stdin();
    let mut input = stdin.lock();
    ask_yes_no_from(&mut input, label, default, color, &mut io::stdout())
}

fn ask_line_from(
    input: &mut dyn BufRead,
    label: &str,
    default: Option<&str>,
    color: bool,
    output: &mut dyn Write,
) -> Result<Option<String>> {
    write!(output, "{}", prompt(label, default, color))?;
    output.flush()?;
    let mut answer = String::new();
    if input.read_line(&mut answer)? == 0 {
        return Ok(None);
    }
    let answer = answer.trim();
    Ok(Some(match (answer.is_empty(), default) {
        (true, Some(default)) => default.to_owned(),
        _ => answer.to_owned(),
    }))
}

fn ask_choice_from(
    input: &mut dyn BufRead,
    label: &str,
    options: &[&str],
    default: usize,
    color: bool,
    output: &mut dyn Write,
) -> Result<Option<usize>> {
    loop {
        let default = (default + 1).to_string();
        let Some(answer) = ask_line_from(input, label, Some(&default), color, output)? else {
            return Ok(None);
        };
        if let Ok(number) = answer.parse::<usize>()
            && (1..=options.len()).contains(&number)
        {
            return Ok(Some(number - 1));
        }
    }
}

fn ask_yes_no_from(
    input: &mut dyn BufRead,
    label: &str,
    default: bool,
    color: bool,
    output: &mut dyn Write,
) -> Result<Option<bool>> {
    let display = if default { "Y/n" } else { "y/N" };
    loop {
        write!(output, "{}", prompt(label, Some(display), color))?;
        output.flush()?;
        let mut answer = String::new();
        if input.read_line(&mut answer)? == 0 {
            return Ok(None);
        }
        let answer = answer.trim();
        if answer.is_empty() {
            return Ok(Some(default));
        }
        match answer.to_ascii_lowercase().as_str() {
            "y" | "yes" => return Ok(Some(true)),
            "n" | "no" => return Ok(Some(false)),
            _ => {}
        }
    }
}

fn absolute_path(path: &Path) -> Result<PathBuf> {
    if path.is_absolute() {
        Ok(path.to_path_buf())
    } else {
        Ok(env::current_dir()?.join(path))
    }
}

fn expand_user_path(value: &str) -> Result<PathBuf> {
    if let Some(rest) = value.strip_prefix("~/") {
        let home = env::var_os("HOME")
            .or_else(|| env::var_os("USERPROFILE"))
            .ok_or_else(|| SnipError::io("cannot locate home directory"))?;
        Ok(PathBuf::from(home).join(rest))
    } else {
        Ok(PathBuf::from(value))
    }
}

#[cfg(test)]
mod tests {
    use super::{ask_choice_from, ask_line_from, ask_yes_no_from, prompt};
    use std::io::Cursor;

    #[test]
    fn line_uses_default_and_eof_cancels() {
        let mut output = Vec::new();
        assert_eq!(
            ask_line_from(
                &mut Cursor::new(b"\n"),
                "path",
                Some("Main"),
                false,
                &mut output
            )
            .unwrap(),
            Some("Main".to_owned())
        );
        assert_eq!(
            ask_line_from(
                &mut Cursor::new(b""),
                "path",
                Some("Main"),
                false,
                &mut output
            )
            .unwrap(),
            None
        );
        assert_eq!(
            prompt("path to your .sniplib", None, false),
            "  ❯ path to your .sniplib: "
        );
    }

    #[test]
    fn yes_no_and_choices_validate_input() {
        let mut output = Vec::new();
        assert_eq!(
            ask_yes_no_from(&mut Cursor::new(b"y\n"), "git", false, false, &mut output).unwrap(),
            Some(true)
        );
        assert_eq!(
            ask_yes_no_from(&mut Cursor::new(b"N\n"), "git", true, false, &mut output).unwrap(),
            Some(false)
        );
        assert_eq!(
            ask_yes_no_from(&mut Cursor::new(b"\n"), "git", false, false, &mut output).unwrap(),
            Some(false)
        );
        assert_eq!(
            ask_choice_from(
                &mut Cursor::new(b"bad\n2\n"),
                "choice",
                &["one", "two"],
                0,
                false,
                &mut output
            )
            .unwrap(),
            Some(1)
        );
    }
}