mirror of https://git.sr.ht/~nixgoat/vento
Compare commits
3 Commits
986c0f7f20
...
791cdf3193
Author | SHA1 | Date |
---|---|---|
Lux Aliaga | 791cdf3193 | |
Lux Aliaga | 4427dadcfc | |
Lux Aliaga | 9e5e0716d1 |
|
@ -17,11 +17,14 @@
|
|||
*
|
||||
*/
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use colored::Colorize;
|
||||
use anyhow::Result;
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
use vento::{help, item};
|
||||
use vento::{
|
||||
common::get_current_dir,
|
||||
error::{throw_error, ErrorType},
|
||||
help, item,
|
||||
};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
// Handles args in Drop
|
||||
|
@ -30,34 +33,40 @@ fn main() -> Result<()> {
|
|||
if args[1].contains("--slot=") {
|
||||
// Checks if the user has provided the long argument "--slot="
|
||||
match args.len() {
|
||||
4 => item::drop(&args[2], &args[1].as_str().replace("--slot=", ""), Path::new(&args[4]).to_path_buf(), true)?,
|
||||
3 => item::drop(&args[2], &args[1].as_str().replace("--slot=", ""), match env::current_dir() {
|
||||
Ok(dir) => dir,
|
||||
Err(_) => bail!("{}", "Vento was unable to detect your current directory. Have you configured your environment correctly?".red())
|
||||
}, true)?,
|
||||
2 => bail!("{}", "You need to specify a file".red()),
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
4 => item::drop(
|
||||
&args[2],
|
||||
&args[1].as_str().replace("--slot=", ""),
|
||||
Path::new(&args[4]).to_path_buf(),
|
||||
true,
|
||||
)?,
|
||||
3 => item::drop(
|
||||
&args[2],
|
||||
&args[1].as_str().replace("--slot=", ""),
|
||||
get_current_dir()?,
|
||||
true,
|
||||
)?,
|
||||
2 => throw_error(ErrorType::SpecifyFile)?,
|
||||
_ => throw_error(ErrorType::TooManyArgs)?,
|
||||
};
|
||||
} else {
|
||||
match args[1].as_str() {
|
||||
"--help" | "-h" => help::drop()?,
|
||||
"-s" => match args.len() {
|
||||
5 => item::drop(&args[3], &args[2], Path::new(&args[4]).to_path_buf(), true)?,
|
||||
4 => item::drop(&args[3], &args[2], match env::current_dir() {
|
||||
Ok(dir) => dir,
|
||||
Err(_) => bail!("{}", "Vento was unable to detect your current directory. Have you configured your environment correctly?".red())
|
||||
}, true)?,
|
||||
3 => bail!("{}", "You need to specify a file".red()),
|
||||
2 => bail!("{}", "You need to specify a slot".red()),
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
4 => item::drop(&args[3], &args[2], get_current_dir()?, true)?,
|
||||
3 => throw_error(ErrorType::SpecifyFile)?,
|
||||
2 => throw_error(ErrorType::SpecifySlot)?,
|
||||
_ => throw_error(ErrorType::TooManyArgs)?,
|
||||
},
|
||||
_ => match args.len() {
|
||||
3 => item::drop(&args[1], &String::from("active"), Path::new(&args[2]).to_path_buf(), true)?,
|
||||
2 => item::drop(&args[1], &String::from("active"), match env::current_dir() {
|
||||
Ok(dir) => dir,
|
||||
Err(_) => bail!("{}", "Vento was unable to detect your current directory. Have you configured your environment correctly?".red())
|
||||
}, true)?,
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
3 => item::drop(
|
||||
&args[1],
|
||||
&String::from("active"),
|
||||
Path::new(&args[2]).to_path_buf(),
|
||||
true,
|
||||
)?,
|
||||
2 => item::drop(&args[1], &String::from("active"), get_current_dir()?, true)?,
|
||||
_ => throw_error(ErrorType::TooManyArgs)?,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,10 +17,12 @@
|
|||
*
|
||||
*/
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use colored::Colorize;
|
||||
use anyhow::Result;
|
||||
use std::env;
|
||||
use vento::{help, item};
|
||||
use vento::{
|
||||
error::{throw_error, ErrorType},
|
||||
help, item,
|
||||
};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
// Handles args in Vento
|
||||
|
@ -30,21 +32,21 @@ fn main() -> Result<()> {
|
|||
// Checks if the user has provided the long argument "--slot="
|
||||
match args.len() {
|
||||
3 => item::take(&args[2], &args[1].replace("--slot=", ""), true)?,
|
||||
2 => bail!("{}", "You need to specify a file".red()),
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
2 => throw_error(ErrorType::SpecifyFile)?,
|
||||
_ => throw_error(ErrorType::TooManyArgs)?,
|
||||
};
|
||||
} else {
|
||||
match args[1].as_str() {
|
||||
"--help" | "-h" => help::take()?,
|
||||
"-s" => match args.len() {
|
||||
4 => item::take(&args[3], &args[2], true)?,
|
||||
3 => bail!("{}", "You need to specify a file".red()),
|
||||
2 => bail!("{}", "You need to specify a slot".red()),
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
3 => throw_error(ErrorType::SpecifyFile)?,
|
||||
2 => throw_error(ErrorType::SpecifySlot)?,
|
||||
_ => throw_error(ErrorType::TooManyArgs)?,
|
||||
},
|
||||
_ => match args.len() {
|
||||
2 => item::take(&args[1], &String::from("active"), true)?,
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
_ => throw_error(ErrorType::TooManyArgs)?,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,10 +17,12 @@
|
|||
*
|
||||
*/
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use colored::Colorize;
|
||||
use anyhow::Result;
|
||||
use std::env;
|
||||
use vento::{help, history, inv};
|
||||
use vento::{
|
||||
error::{throw_error, ErrorType},
|
||||
help, history, inv,
|
||||
};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
// Handles args in Vento
|
||||
|
@ -32,7 +34,7 @@ fn main() -> Result<()> {
|
|||
match args.len() {
|
||||
3 => inv::list(&args[1].replace("--slot=", ""), &args[2])?,
|
||||
2 => inv::list(&args[1].replace("--slot=", ""), "")?,
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
_ => throw_error(ErrorType::TooManyArgs)?,
|
||||
};
|
||||
} else {
|
||||
match args[1].as_str() {
|
||||
|
@ -43,8 +45,8 @@ fn main() -> Result<()> {
|
|||
"-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()),
|
||||
_ => bail!("{}", "Too many arguments".red()),
|
||||
2 => throw_error(ErrorType::SpecifySlot)?,
|
||||
_ => throw_error(ErrorType::TooManyArgs)?,
|
||||
},
|
||||
_ => inv::list("active", args[1].as_str())?,
|
||||
}
|
||||
|
|
|
@ -17,9 +17,11 @@
|
|||
*
|
||||
*/
|
||||
|
||||
use crate::error::{throw_error, ErrorType};
|
||||
use anyhow::{bail, Result};
|
||||
use colored::Colorize;
|
||||
use config::Config;
|
||||
use std::env::current_dir;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
@ -125,3 +127,17 @@ pub fn history(data: HistoryData) -> Result<()> {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Gets current directory for commands
|
||||
pub fn get_current_dir() -> Result<PathBuf> {
|
||||
let currentdir = match current_dir() {
|
||||
Ok(dir) => dir,
|
||||
Err(_) => PathBuf::new(),
|
||||
};
|
||||
|
||||
if currentdir == PathBuf::new() {
|
||||
throw_error(ErrorType::NoCurrentDirectory)?;
|
||||
}
|
||||
|
||||
Ok(currentdir)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Vento, a CLI inventory for your files.
|
||||
* Copyright (C) 2023 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 anyhow::{bail, Result};
|
||||
use colored::Colorize;
|
||||
|
||||
pub enum ErrorType {
|
||||
TooManyArgs,
|
||||
SpecifySlot,
|
||||
SpecifyFile,
|
||||
NoCurrentDirectory,
|
||||
}
|
||||
|
||||
pub fn throw_error(error: ErrorType) -> Result<()> {
|
||||
bail!(
|
||||
"{}",
|
||||
match error {
|
||||
ErrorType::TooManyArgs => "Too many arguments",
|
||||
ErrorType::SpecifySlot => "You need to specify a file",
|
||||
ErrorType::SpecifyFile => "You need to specify a slot",
|
||||
ErrorType::NoCurrentDirectory => "Vento was unable to detect your current directory. Have you configured your environment correctly?",
|
||||
}
|
||||
.red()
|
||||
);
|
||||
}
|
|
@ -60,51 +60,48 @@ pub fn undo() -> Result<()> {
|
|||
}
|
||||
|
||||
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(""),
|
||||
}
|
||||
)
|
||||
"✅ {}{}{}",
|
||||
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(())
|
||||
|
|
|
@ -36,7 +36,7 @@ pub fn init() -> Result<()> {
|
|||
let _ = io::stdout().flush();
|
||||
io::stdin().read_line(&mut answer)?;
|
||||
match answer.as_str().trim() {
|
||||
"y" | "Y" => fs::remove_dir_all(&ventodir)?,
|
||||
"y" | "Y" => fs::remove_dir_all(ventodir)?,
|
||||
_ => process::exit(0),
|
||||
};
|
||||
};
|
||||
|
@ -175,9 +175,9 @@ pub fn switch(message: bool) -> Result<()> {
|
|||
|
||||
let rename_error = "Vento was unable to switch slots. Try running \"vento -i\" and try again";
|
||||
|
||||
fs::rename(&active, &temp).context(rename_error)?;
|
||||
fs::rename(&inactive, &active).context(rename_error)?;
|
||||
fs::rename(&temp, &inactive).context(rename_error)?;
|
||||
fs::rename(active, &temp).context(rename_error)?;
|
||||
fs::rename(inactive, active).context(rename_error)?;
|
||||
fs::rename(&temp, inactive).context(rename_error)?;
|
||||
|
||||
common::history(common::HistoryData {
|
||||
path: PathBuf::new(),
|
||||
|
|
|
@ -72,11 +72,11 @@ pub fn take(file: &String, slot: &str, message: bool) -> Result<()> {
|
|||
|
||||
if sourcepath.is_file() | sourcepath.is_symlink() {
|
||||
// Checks the path's file type
|
||||
fs::copy(&file, &destpath)?;
|
||||
fs::remove_file(&file)?;
|
||||
fs::copy(file, &destpath)?;
|
||||
fs::remove_file(file)?;
|
||||
} else if sourcepath.is_dir() {
|
||||
let options = CopyOptions::new();
|
||||
move_dir(&file, &slotdir, &options)?;
|
||||
move_dir(file, &slotdir, &options)?;
|
||||
} else {
|
||||
bail!("{}", "No such file or directory".red());
|
||||
}
|
||||
|
@ -161,7 +161,7 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
|
|||
} else if sourcepath.is_dir() {
|
||||
let destpath: PathBuf = Path::new(&dest).to_path_buf();
|
||||
let options = CopyOptions::new();
|
||||
move_dir(&sourcepath, &destpath, &options)?;
|
||||
move_dir(&sourcepath, destpath, &options)?;
|
||||
} else {
|
||||
bail!("{}", "No such file or directory".red());
|
||||
}
|
||||
|
|
|
@ -17,7 +17,8 @@
|
|||
*
|
||||
*/
|
||||
|
||||
mod common;
|
||||
pub mod common;
|
||||
pub mod error;
|
||||
pub mod help;
|
||||
pub mod history;
|
||||
pub mod inv;
|
||||
|
|
Loading…
Reference in New Issue