From a3894ab02a80e739ceb3adf8ccb000d9231b8fea Mon Sep 17 00:00:00 2001 From: mint Date: Wed, 14 Sep 2022 23:20:17 -0300 Subject: [PATCH] item: Implement taking files You can now take files and put them in your inventory! An essential function of this program that should've been implemented from the beginning! --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/item.rs | 26 ++++++++++++++++++++++++-- src/main.rs | 14 ++++++++++++-- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 54de213..4616237 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -56,6 +56,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "fs_extra" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394" + [[package]] name = "getrandom" version = "0.2.7" @@ -169,6 +175,7 @@ version = "0.1.0" dependencies = [ "colored", "dirs", + "fs_extra", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 795d3ba..bfb8217 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ edition = "2021" [dependencies] dirs = "4.0.0" colored = "2.0.0" +fs_extra = "1.2.0" diff --git a/src/item.rs b/src/item.rs index 2147881..132aaee 100644 --- a/src/item.rs +++ b/src/item.rs @@ -17,8 +17,30 @@ * */ -pub fn take() { - // to be implemented +use std::fs; +use fs_extra::dir::{CopyOptions, move_dir}; +use std::path::{Path, PathBuf}; +use colored::Colorize; +use super::common; + +pub fn take(file: &String) { + let ventodir = common::env_config(); + let active: PathBuf = [ventodir.to_path_buf(), Path::new("active").to_path_buf()].iter().collect(); + + let sourcepath: PathBuf = Path::new(&file).to_path_buf(); + let destpath: PathBuf = [&active, &Path::new(file).to_path_buf()].iter().collect(); + + if Path::exists(&sourcepath) { + println!("❌ {}", format!("A file with the same name already exists in your inventory!").red()); + } else if sourcepath.is_file() | sourcepath.is_symlink() { + fs::copy(&file, &destpath).expect("❌ Vento was unable to copy the file."); + fs::remove_file(&file).expect("❌ Vento was unable to remove the file."); + } else if sourcepath.is_dir() { + let options = CopyOptions::new(); + move_dir(&file, &active, &options).expect("❌ Vento was unable to move the directory."); + } else { + println!("❌ {}", format!("No such file or directory.").red()); + } } pub fn drop() { diff --git a/src/main.rs b/src/main.rs index ac51eea..cc3562c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ use std::env; use colored::Colorize; mod inv; +mod item; mod common; fn main() { @@ -37,6 +38,13 @@ fn main() { }; }, "switch" => inv::switch(), + "take" => { + if args.len() == 3 { + item::take(&args[2]); + } else { + println!("❌ {}", format!("You need to specify a file.").red()) + }; + } _ => println!("❔ Command not found. Type \"vento help\" to see all commands available.") } } else { @@ -49,14 +57,16 @@ fn help() { © 2022 Lux Aliaga. Licensed under GPLv3 {} - - {}: Initializes Vento + - {}: Takes a file or directory and saves it in your inventory - {}: Lists files in selected inventory - {}: Switches slots + - {}: Initializes Vento - {}: Displays this message", format!("Vento").bold().blue(), format!("Usage:").bold(), - format!("init").bold().green(), + format!("take [file | directory]").bold().green(), format!("list [slot]").bold().green(), format!("switch").bold().green(), + format!("init").bold().green(), format!("help").bold().green()); }