qk/
utils.rs

1use crate::{Config, Template};
2use anyhow::Result;
3use clap::ArgMatches;
4use std::{env, fs, path::Path};
5
6pub fn list_dir(dir: impl AsRef<Path>) -> Result<Vec<String>> {
7    let read_dir = fs::read_dir(dir)?;
8
9    let mut items = Vec::new();
10
11    for entry in read_dir {
12        let entry = entry?;
13        if entry.file_type()?.is_dir() {
14            let item = entry.file_name().to_string_lossy().to_string();
15            items.push(item);
16        }
17    }
18
19    Ok(items)
20}
21
22pub fn get_editor(config: &Config, template: &Template, matches: &ArgMatches) -> Option<String> {
23    let mut editor = matches.get_one("editor").cloned();
24
25    if editor.is_none() {
26        editor = template.editor().cloned();
27    }
28
29    if editor.is_none() {
30        editor = config.editor().cloned();
31    }
32
33    if editor.is_none() {
34        editor = env::var("VISUAL").ok()
35    }
36
37    if editor.is_none() {
38        editor = env::var("EDITOR").ok()
39    }
40
41    // If editor is "", set editor to None
42    if let Some(true) = editor.as_ref().map(|editor| editor.is_empty()) {
43        editor = None
44    }
45
46    editor
47}
48
49pub fn get_shell(config: &Config, template: &Template) -> String {
50    if let Some(shell) = template.shell() {
51        return shell.clone();
52    }
53
54    if let Some(shell) = config.shell() {
55        return shell.clone();
56    }
57
58    #[cfg(unix)]
59    return env::var("SHELL").unwrap_or_else(|_| "sh".to_string());
60
61    #[cfg(windows)]
62    return "PowerShell.exe".to_string();
63}