vento/
history.rs

1/*
2 * Vento, a CLI inventory for your files.
3 * Copyright (C) 2022 Lux Aliaga
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 *
18 */
19
20use crate::{
21    common::{env_config, parse_config},
22    inv, item,
23    message::{append_emoji, throw_error, EmojiType, ErrorType},
24};
25use anyhow::Result;
26use colored::Colorize;
27use std::fs;
28use std::path::{Path, PathBuf};
29
30/// Undoes the last action made by Vento using the history file located on the Vento directory
31pub fn undo() -> Result<()> {
32    let lastpath: PathBuf = [env_config()?.vento_dir, Path::new("last").to_path_buf()]
33        .iter()
34        .collect();
35
36    let lastfile = fs::read_to_string(lastpath)?;
37
38    let mut contents = vec![];
39
40    for line in lastfile.lines() {
41        contents.push(line);
42    }
43
44    if contents.len() != 4 {
45        throw_error(ErrorType::InvalidHistoryLength)?;
46    }
47
48    match contents[3] {
49        "take" => {
50            let destpath = Path::new(contents[0]).to_path_buf();
51            item::drop(
52                &String::from(contents[1]),
53                contents[2],
54                destpath,
55                false,
56                false,
57            )?;
58        }
59        "drop" => {
60            let path = vec![contents[0], contents[1]].join("/");
61            item::take(&path, contents[2], false, false)?;
62        }
63        "switch" => {
64            inv::switch(false)?;
65        }
66        _ => throw_error(ErrorType::IllegalAction)?,
67    }
68
69    println!(
70        "{}{}{}{}",
71        append_emoji(EmojiType::Success)?,
72        match contents[3] {
73            "take" => "Take",
74            "drop" => "Drop",
75            "switch" => "Switch",
76            _ => "Unknown",
77        }
78        .bold(),
79        " action undone".green(),
80        match contents[3] {
81            "take" => format!(
82                "{}{}{}{}{}{}{}",
83                " (".green(),
84                contents[1].bold(),
85                ", ".green(),
86                match parse_config()?.history_display_dir {
87                    true => format!("{} {} ", "from".green(), contents[0],),
88                    _ => String::new(),
89                },
90                "to ".green(),
91                match contents[2] {
92                    "active" => contents[2].green(),
93                    "inactive" => contents[2].blue(),
94                    _ => contents[2].red(),
95                }
96                .bold(),
97                " slot)".green(),
98            ),
99            "drop" => format!(
100                "{}{}{}{}{}{}{}",
101                " (".green(),
102                contents[1].bold(),
103                ", from ".green(),
104                match contents[2] {
105                    "active" => contents[2].green(),
106                    "inactive" => contents[2].blue(),
107                    _ => contents[2].red(),
108                }
109                .bold(),
110                " slot".green(),
111                match parse_config()?.history_display_dir {
112                    true => format!(" {} {}", "to".green(), contents[0],),
113                    false => String::new(),
114                },
115                ")".green(),
116            ),
117            _ => String::from(""),
118        }
119    );
120
121    Ok(())
122}