aboutsummaryrefslogtreecommitdiff
path: root/src/commands/mod.rs
blob: 3aee90ff6407d14da6f8338e1aeccc75bf473ae6 (plain)
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
use std::{
    path::PathBuf,
    process::{Command, Stdio},
    collections::HashMap,
    io::Write,
    fs,
};
use indoc::formatdoc;

mod sit;
pub use sit::start_sit;

fn open_xournal(path: &PathBuf) {
    let _ = Command::new("xournalpp")
        .arg(path.as_os_str())
        .arg("--name=xoppdoggin")
        .arg("--disable-audio")
        .stderr(Stdio::null())
        .spawn()
        .expect("Unable to launch xournal!");
}

fn latex_template(name: &String, path: &PathBuf) -> String {
    formatdoc! {r"
        \begin{{figure}}[ht]
            \centering
            \incfig{{{}}}
            \caption{{{name}}}
            \label{{fig:{name}}}
        \end{{figure}}
        ",
        path.with_extension("").file_name().unwrap().to_string_lossy()
    }
}

pub fn shake_figure(name: String, root: PathBuf) {
    let template_path = dirs::config_dir()
                   .expect("Could not resolve config directory.")
                   .join("xoppdog/template.xopp");

    if !template_path.try_exists().unwrap() {
        panic!("Please make a template at {}!", template_path.to_str().unwrap());
    }

    if !root.try_exists().unwrap() {
        fs::create_dir_all(&root).unwrap();
    }

    let filename = name.trim().replace(" ", "-");
    let target = root.join(filename).with_extension("xopp");

    fs::copy(template_path, &target).expect("Could not copy template to target file.");

    open_xournal(&target);
    print!("{}", latex_template(&name, &target));
}

pub fn fetch_figure(root: PathBuf) {
    let figures = find_figures(&root);
    let figure_path = pick_figure(&figures);

    open_xournal(&figure_path);
}

fn find_figures(root: &PathBuf) -> HashMap<String, PathBuf> {
    if !root.is_dir() {
        panic!("Root path not a directory!");
    }

    let directory = fs::read_dir(root)
        .expect("Can't sniff out the given figures directory.");

    let mut figures = HashMap::new();

    for entry in directory {
        match entry {
            Ok(entry) => {
                if entry.path().extension().unwrap() == "xopp" {
                    let name = entry.path()
                        .with_extension("")
                        .file_name()
                        .unwrap().to_string_lossy()
                        .replace("-", " ").replace("_", " ");
                    figures.insert(name, entry.path());
                }
            },
            Err(error) => {
                eprintln!("Error reading file in figures directory: {:?}", error);
                continue;
            },
        }
    }

    return figures;
}

fn pick_figure<'a>(figures: &'a HashMap<String, PathBuf>) -> &'a PathBuf{
    let mut options = String::new();
    for key in figures.keys() {
        options.push_str(key);
        options.push_str("\n");
    }

    let mut rofi = Command::new("rofi")
        .arg("-dmenu")
        .args(["-p", "Figures"])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn().expect("Rofi failed to launch.");

    rofi.stdin.as_mut().unwrap().write(options.trim().as_bytes())
        .expect("Could not send input to Rofi process.");

    let rofi_output = rofi.wait_with_output()
        .expect("Waiting on Rofi failed.");
    let choice = String::from_utf8(rofi_output.stdout)
        .expect("Couldn't make sense of Rofi output.");
    let choice_path = figures.get(choice.trim())
        .expect("Somehow Rofi output didn't match with a figure.");

    return choice_path;
}