mirror of
https://git.sr.ht/~nixgoat/vento
synced 2024-11-21 14:23:05 +00:00
Compare commits
7 commits
af0a27464b
...
dadb0d721f
Author | SHA1 | Date | |
---|---|---|---|
Lux Aliaga | dadb0d721f | ||
Lux Aliaga | ab313e2f50 | ||
Lux Aliaga | ef3dcf8acc | ||
Lux Aliaga | 1cbfc5a568 | ||
Lux Aliaga | 19aa1955c8 | ||
Lux Aliaga | c1533e6b41 | ||
Lux Aliaga | 6ad43bbf82 |
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -592,7 +592,7 @@ checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf"
|
|||
|
||||
[[package]]
|
||||
name = "vento"
|
||||
version = "1.1.0"
|
||||
version = "1.1.1"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"colored",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "vento"
|
||||
version = "1.1.0"
|
||||
version = "1.1.1"
|
||||
edition = "2021"
|
||||
readme = "README.md"
|
||||
|
||||
|
|
|
@ -60,7 +60,7 @@ $ vento
|
|||
$ vento -c
|
||||
|
||||
// undoing last action
|
||||
$ vento -c
|
||||
$ vento -u
|
||||
|
||||
// taking a file or directory
|
||||
$ take <file|directory>
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
use anyhow::{bail, Result};
|
||||
use colored::Colorize;
|
||||
use std::env;
|
||||
use vento::{help, inv, item};
|
||||
use vento::{help, history, inv};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
// Handles args in Vento
|
||||
|
@ -38,12 +38,12 @@ fn main() -> Result<()> {
|
|||
match args[1].as_str() {
|
||||
"-h" | "--help" => help::vento()?,
|
||||
"-i" | "--init" => inv::init()?,
|
||||
"-c" | "--switch" => inv::switch()?,
|
||||
"-u" | "--undo" => item::undo()?,
|
||||
"-c" | "--switch" => inv::switch(true)?,
|
||||
"-u" | "--undo" => history::undo()?,
|
||||
"-s" => match args.len() {
|
||||
4 => inv::list(&args[2], &args[3])?,
|
||||
3 => inv::list(&args[2], "")?,
|
||||
2 => bail!("{}", "You need to specify a slot.".red()),
|
||||
2 => bail!("{}", "You need to specify a slot".red()),
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
},
|
||||
_ => inv::list("active", args[1].as_str())?,
|
||||
|
|
|
@ -40,6 +40,7 @@ pub struct HistoryData {
|
|||
pub enum Action {
|
||||
Take,
|
||||
Drop,
|
||||
Switch,
|
||||
}
|
||||
|
||||
/// Provides required variables for Vento
|
||||
|
@ -118,6 +119,7 @@ pub fn history(data: HistoryData) -> Result<()> {
|
|||
match data.action {
|
||||
Action::Take => "take",
|
||||
Action::Drop => "drop",
|
||||
Action::Switch => "switch",
|
||||
}
|
||||
)?;
|
||||
|
||||
|
|
|
@ -71,7 +71,7 @@ pub fn drop() -> Result<()> {
|
|||
© 2022 Lux Aliaga. Licensed under GPLv3
|
||||
|
||||
{}
|
||||
- {}: Takes a file off the inventory and drops it.
|
||||
- {}: Takes a file off the inventory and drops it
|
||||
- {}: Displays this message",
|
||||
"Drop".bold().blue(),
|
||||
"Usage:".bold(),
|
||||
|
|
111
src/history.rs
Normal file
111
src/history.rs
Normal file
|
@ -0,0 +1,111 @@
|
|||
/*
|
||||
* Vento, a CLI inventory for your files.
|
||||
* Copyright (C) 2022 Lux Aliaga
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
use crate::{common, inv, item};
|
||||
use anyhow::{bail, Result};
|
||||
use colored::Colorize;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Undoes the last action made by Vento using the history file located on the Vento directory
|
||||
pub fn undo() -> Result<()> {
|
||||
let lastpath: PathBuf = [
|
||||
common::env_config()?.vento_dir,
|
||||
Path::new("last").to_path_buf(),
|
||||
]
|
||||
.iter()
|
||||
.collect();
|
||||
|
||||
let lastfile = fs::read_to_string(lastpath)?;
|
||||
|
||||
let mut contents = vec![];
|
||||
|
||||
for line in lastfile.lines() {
|
||||
contents.push(line);
|
||||
}
|
||||
|
||||
if contents.len() != 4 {
|
||||
bail!("Invalid history length".red());
|
||||
}
|
||||
|
||||
match contents[3] {
|
||||
"take" => {
|
||||
let destpath = Path::new(contents[0]).to_path_buf();
|
||||
item::drop(&String::from(contents[1]), contents[2], destpath, false)?;
|
||||
}
|
||||
"drop" => {
|
||||
let path = vec![contents[0], contents[1]].join("/");
|
||||
item::take(&path, contents[2], false)?;
|
||||
}
|
||||
"switch" => {
|
||||
inv::switch(false)?;
|
||||
}
|
||||
_ => bail!("Illegal action".red()),
|
||||
}
|
||||
|
||||
println!(
|
||||
"✅ {}",
|
||||
format!(
|
||||
"{}{}{}",
|
||||
match contents[3] {
|
||||
"take" => "Take",
|
||||
"drop" => "Drop",
|
||||
"switch" => "Switch",
|
||||
_ => "Unknown",
|
||||
}
|
||||
.bold(),
|
||||
" action undone".green(),
|
||||
match contents[3] {
|
||||
"take" => format!(
|
||||
"{}{}{}{}{}{}{}",
|
||||
" (".green(),
|
||||
contents[1].bold(),
|
||||
", from ".green(),
|
||||
contents[0],
|
||||
" to ".green(),
|
||||
match contents[2] {
|
||||
"active" => contents[2].green(),
|
||||
"inactive" => contents[2].blue(),
|
||||
_ => contents[2].red(),
|
||||
}
|
||||
.bold(),
|
||||
" slot)".green(),
|
||||
),
|
||||
"drop" => format!(
|
||||
"{}{}{}{}{}{}{}",
|
||||
" (".green(),
|
||||
contents[1].bold(),
|
||||
", from ".green(),
|
||||
match contents[2] {
|
||||
"active" => contents[2].green(),
|
||||
"inactive" => contents[2].blue(),
|
||||
_ => contents[2].red(),
|
||||
}
|
||||
.bold(),
|
||||
" slot to ".green(),
|
||||
contents[0],
|
||||
")".green(),
|
||||
),
|
||||
_ => String::from(""),
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
21
src/inv.rs
21
src/inv.rs
|
@ -53,7 +53,7 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
|
|||
// Detects if Vento hasn't been initialized and bails if so
|
||||
bail!(
|
||||
"{}",
|
||||
"Vento not initialized. Run \"vento -i\" to initialize Vento.".red()
|
||||
"Vento not initialized. Run \"vento -i\" to initialize Vento".red()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -70,7 +70,7 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
|
|||
|
||||
if dir.to_string().contains("..") {
|
||||
// Basically preventing from listing anything out of bounds. ls and dir exist for that
|
||||
bail!("{}", "Cannot access parent.".red());
|
||||
bail!("{}", "Cannot access parent".red());
|
||||
}
|
||||
|
||||
if !slotdir.is_dir() {
|
||||
|
@ -78,7 +78,7 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
|
|||
bail!(
|
||||
"{}",
|
||||
format!(
|
||||
"No such slot or directory. Valid slots are {} and {}.",
|
||||
"No such slot or directory. Valid slots are {} and {}",
|
||||
"active".green().bold(),
|
||||
"inactive".blue().bold()
|
||||
)
|
||||
|
@ -91,7 +91,7 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
|
|||
println!(
|
||||
"🗃️ {}",
|
||||
format!(
|
||||
"No files in {}{}.",
|
||||
"No files in {}{}",
|
||||
match slot {
|
||||
"active" => slot.bold(),
|
||||
_ => slot.blue().bold(),
|
||||
|
@ -165,7 +165,7 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
|
|||
}
|
||||
|
||||
/// Switches inevntory slots between each other, making the currently active inventory inactive and viceversa
|
||||
pub fn switch() -> Result<()> {
|
||||
pub fn switch(message: bool) -> Result<()> {
|
||||
let ventodir = &common::env_config()?.vento_dir;
|
||||
let active = &common::env_config()?.active_dir;
|
||||
let inactive = &common::env_config()?.inactive_dir;
|
||||
|
@ -179,7 +179,16 @@ pub fn switch() -> Result<()> {
|
|||
fs::rename(&inactive, &active).context(rename_error)?;
|
||||
fs::rename(&temp, &inactive).context(rename_error)?;
|
||||
|
||||
println!("🎉 {}", "Switched inventory slots!".green());
|
||||
common::history(common::HistoryData {
|
||||
path: PathBuf::new(),
|
||||
file: String::new(),
|
||||
slot: String::new(),
|
||||
action: common::Action::Switch,
|
||||
})?;
|
||||
|
||||
if message {
|
||||
println!("✅ {}", "Switched inventory slots!".green());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
52
src/item.rs
52
src/item.rs
|
@ -32,7 +32,7 @@ pub fn take(file: &String, slot: &str, message: bool) -> Result<()> {
|
|||
// Detects if Vento hasn't been initialized and bails if so
|
||||
bail!(
|
||||
"{}",
|
||||
"Vento not initialized. Run \"vento -i\" to initialize Vento.".red()
|
||||
"Vento not initialized. Run \"vento -i\" to initialize Vento".red()
|
||||
);
|
||||
};
|
||||
let slotdir: PathBuf = match slot {
|
||||
|
@ -46,7 +46,7 @@ pub fn take(file: &String, slot: &str, message: bool) -> Result<()> {
|
|||
bail!(
|
||||
"{}",
|
||||
format!(
|
||||
"No such slot. Valid slots are {} and {}.",
|
||||
"No such slot. Valid slots are {} and {}",
|
||||
"active".green().bold(),
|
||||
"inactive".blue().bold()
|
||||
)
|
||||
|
@ -78,7 +78,7 @@ pub fn take(file: &String, slot: &str, message: bool) -> Result<()> {
|
|||
let options = CopyOptions::new();
|
||||
move_dir(&file, &slotdir, &options)?;
|
||||
} else {
|
||||
bail!("{}", "No such file or directory.".red());
|
||||
bail!("{}", "No such file or directory".red());
|
||||
}
|
||||
|
||||
common::history(common::HistoryData {
|
||||
|
@ -109,7 +109,7 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
|
|||
// Detects if Vento hasn't been initialized and bails if so
|
||||
bail!(
|
||||
"{}",
|
||||
"Vento not initialized. Run \"vento -i\" to initialize Vento.".red()
|
||||
"Vento not initialized. Run \"vento -i\" to initialize Vento".red()
|
||||
);
|
||||
};
|
||||
|
||||
|
@ -124,7 +124,7 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
|
|||
bail!(
|
||||
"{}",
|
||||
format!(
|
||||
"No such slot. Valid slots are {} and {}.",
|
||||
"No such slot. Valid slots are {} and {}",
|
||||
"active".green().bold(),
|
||||
"inactive".blue().bold()
|
||||
)
|
||||
|
@ -142,7 +142,7 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
|
|||
|
||||
if Path::exists(&destpath) {
|
||||
// Checks if there's a file with the same name in the destination path.
|
||||
bail!("{}", "A file with the same name already exists in the destination! Try renaming it or dropping this file somewhere else.".red());
|
||||
bail!("{}", "A file with the same name already exists in the destination! Try renaming it or dropping this file somewhere else".red());
|
||||
}
|
||||
|
||||
if sourcepath.is_file() | sourcepath.is_symlink() {
|
||||
|
@ -154,7 +154,7 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
|
|||
let options = CopyOptions::new();
|
||||
move_dir(&sourcepath, &destpath, &options)?;
|
||||
} else {
|
||||
bail!("{}", "No such file or directory.".red());
|
||||
bail!("{}", "No such file or directory".red());
|
||||
}
|
||||
|
||||
destpath.pop();
|
||||
|
@ -177,41 +177,3 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Undoes the last action made by Vento using the history file located on the Vento directory
|
||||
pub fn undo() -> Result<()> {
|
||||
let lastpath: PathBuf = [
|
||||
common::env_config()?.vento_dir,
|
||||
Path::new("last").to_path_buf(),
|
||||
]
|
||||
.iter()
|
||||
.collect();
|
||||
|
||||
let lastfile = fs::read_to_string(lastpath)?;
|
||||
|
||||
let mut contents = vec![];
|
||||
|
||||
for line in lastfile.lines() {
|
||||
contents.push(line);
|
||||
}
|
||||
|
||||
if contents.len() != 4 {
|
||||
bail!("Invalid history length".red());
|
||||
}
|
||||
|
||||
match contents[3] {
|
||||
"take" => {
|
||||
let destpath = Path::new(contents[0]).to_path_buf();
|
||||
drop(&String::from(contents[1]), contents[2], destpath, false)?;
|
||||
}
|
||||
"drop" => {
|
||||
let path = vec![contents[0], contents[1]].join("/");
|
||||
take(&path, contents[2], false)?;
|
||||
}
|
||||
_ => bail!("Illegal action".red()),
|
||||
}
|
||||
|
||||
println!("✅ {}", "Last action undone".green());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -19,5 +19,6 @@
|
|||
|
||||
mod common;
|
||||
pub mod help;
|
||||
pub mod history;
|
||||
pub mod inv;
|
||||
pub mod item;
|
||||
|
|
Loading…
Reference in a new issue