Updating to 1.0.2 #3
|
|
@ -21,4 +21,6 @@ Cargo.lock
|
|||
/target
|
||||
/coverage
|
||||
/llvm
|
||||
tarpaulin-report.html
|
||||
/releases
|
||||
tarpaulin-report.html
|
||||
!coverage/html/badges/flat.svg
|
||||
|
|
@ -1,14 +1,18 @@
|
|||
[package]
|
||||
name = "snp"
|
||||
author = "Dandaedre"
|
||||
authors = ["Dandaedre"]
|
||||
version = "1.0.1"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
test = []
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[profile.test]
|
||||
opt-level = 0
|
||||
|
||||
[profile.release]
|
||||
opt-level = "s"
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.2.7", features = ["derive"] }
|
||||
|
|
|
|||
|
|
@ -20,7 +20,15 @@ script = [
|
|||
script_runner = "@shell"
|
||||
ignore_errors = true
|
||||
script = [
|
||||
"CROSS_CONTAINER_ENGINE=podman cross build --target x86_64-pc-windows-gnu"
|
||||
"CROSS_CONTAINER_ENGINE=podman cross build --target x86_64-pc-windows-gnu --release"
|
||||
]
|
||||
|
||||
[tasks.linux_build]
|
||||
script_runner = "@shell"
|
||||
ignore_errors = true
|
||||
script = [
|
||||
"cargo build --release",
|
||||
# "CROSS_CONTAINER_ENGINE=podman cross build --target x86_64-pc-linux-gnu --release"
|
||||
]
|
||||
|
||||
[tasks.win_test]
|
||||
|
|
@ -30,3 +38,15 @@ script = [
|
|||
"CROSS_CONTAINER_ENGINE=podman cross test --features test --target x86_64-pc-windows-gnu"
|
||||
]
|
||||
|
||||
[tasks.release]
|
||||
dependencies = ["win_build","linux_build"]
|
||||
script_runner = "@shell"
|
||||
ignore_errors = true
|
||||
script = [
|
||||
"mkdir -p releases",
|
||||
"rm -f releases/*",
|
||||
"cp target/x86_64-pc-windows-gnu/release/snp.exe releases/",
|
||||
"cp target/release/snp releases/",
|
||||
"strip releases/snp",
|
||||
"strip releases/snp.exe",
|
||||
]
|
||||
|
|
|
|||
55
README.md
55
README.md
|
|
@ -1,29 +1,60 @@
|
|||
# snp
|
||||
# SNP : A (simple) Code Snippet Manager
|
||||
|
||||

|
||||
<img src="https://proxy.dandaedre.com/gitea/repo-avatars/30-fed01296b47bdd2e84f8f0a1a96e4094" alt="drawing" width="140" align="right"/>
|
||||
|
||||
A simple cli utility to manage snippets, while it was created to be used with Helix, it can work with any tool integrating input from stdout
|
||||
|
||||
## Usage
|
||||
|
||||
```sh
|
||||
To create a new snippet and edit it immediately
|
||||
$ snp snippet_name -c -e list of default tokens
|
||||
### Create and Edit Snippet
|
||||
|
||||
To edit the content of a snippet
|
||||
To create a new snippet and edit its content immediately, use the following command:
|
||||
```
|
||||
$ snp snippet_name -c -e list_of_default_tokens
|
||||
```
|
||||
Replace `snippet_name` with the desired name for your snippet and provide a space-separated `list_of_default_tokens` that you want to include in the snippet. This command will create the snippet and open the editor for you to modify its content.
|
||||
|
||||
### Edit Snippet Content
|
||||
|
||||
To edit the content of an existing snippet, use the following command:
|
||||
```
|
||||
$ snp snippet_name -e
|
||||
```
|
||||
Replace `snippet_name` with the name of the snippet you want to edit. This command will open the editor for you to modify the content of the specified snippet.
|
||||
|
||||
To edit the whole snippet (allow for editing of name and list of args)
|
||||
### Edit Whole Snippet
|
||||
|
||||
To edit the entire snippet, including its name and list of arguments, use the following command:
|
||||
```
|
||||
$ snp snippet_name -E
|
||||
```
|
||||
Replace `snippet_name` with the name of the snippet you want to edit. This command will open the editor for you to modify the name, content, and arguments of the specified snippet.
|
||||
|
||||
To delete a snippet
|
||||
#### Wonky Not definitive behaviour
|
||||
|
||||
- If you change the name of the snippet this way, the snippet will not be saved under a different name, this behaviour will change very soon
|
||||
- If you mess up in any way, the snippet manager will save your work under `snippet_name.bad.yaml`
|
||||
|
||||
### Delete Snippet
|
||||
|
||||
To delete a snippet, use the following command:
|
||||
```
|
||||
$ snp snippet_name -d
|
||||
```
|
||||
Replace `snippet_name` with the name of the snippet you want to delete. This command will remove the specified snippet from the snippet folder .
|
||||
|
||||
To use a snippet
|
||||
### Use Snippet
|
||||
|
||||
To use a snippet, simply execute the following command:
|
||||
```
|
||||
$ snp snippet_name
|
||||
```
|
||||
Replace `snippet_name` with the name of the snippet you want to use. This command will execute the specified snippet.
|
||||
|
||||
## Testing
|
||||
|
||||
For reasons too long to explain here, test must be run with the optional feature ``--feature "test"`.
|
||||
Failure to do so will result in the editor popping in during tests.
|
||||
For testing purposes, it is important to run tests with the optional feature `--feature "test"`. Failing to include this feature may cause the editor to pop up during tests. To run tests, use the following command:
|
||||
```
|
||||
$ cargo test --feature "test"
|
||||
```
|
||||
Make sure to include the `--feature "test"` option to ensure smooth testing without interruptions from the editor.
|
||||
|
||||
|
|
|
|||
4
TODO.md
4
TODO.md
|
|
@ -2,7 +2,6 @@
|
|||
[ ] Benchmarks
|
||||
[ ] Better use of file modes
|
||||
[ ] Document non coverage
|
||||
[ ] Have the the EditWhole save under a different path if name was changed (and edit too)
|
||||
[ ] Find a solution for the .badpocalypse
|
||||
|
||||
### Currently working on
|
||||
|
|
@ -17,6 +16,7 @@
|
|||
[*] edit_snippet
|
||||
[*] edit_whole_snippet
|
||||
[*] print_snippet
|
||||
[*] rename_snippet
|
||||
[*] Finish tests on
|
||||
[*] locate_snippet
|
||||
[*] create_snippet
|
||||
|
|
@ -24,10 +24,12 @@
|
|||
[*] edit_whole_snippet
|
||||
[*] print_snippet
|
||||
[*] delete snippet
|
||||
[*] rename snippet
|
||||
[*] Have identical attributes with {0} {1} {0} (^^^^^)
|
||||
[*] Add an option to delete a snippet
|
||||
[*] Coverage
|
||||
[*] Test populate_actions
|
||||
[*] Have the the EditWhole save under a different path if name was changed (and edit too)
|
||||
|
||||
## Posponed or rendered useless
|
||||
[x] Chain the methods when possible with result ?
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ pub enum Actions {
|
|||
Create,
|
||||
/// Deletes the snippet
|
||||
Delete,
|
||||
/// Renames the snippet
|
||||
Rename,
|
||||
}
|
||||
|
||||
/// The main worker, execute all commands given as Args
|
||||
|
|
@ -34,6 +36,7 @@ pub fn execute_parsed_cli(cli: Cli) -> Result<(), errors::SnippetError> {
|
|||
Actions::Edit => wrapper_functions::edit_snippet(&cli),
|
||||
Actions::EditAll => wrapper_functions::edit_whole_snippet(&cli),
|
||||
Actions::Delete => wrapper_functions::delete_snippet(&cli),
|
||||
Actions::Rename => wrapper_functions::rename_snippet(&cli),
|
||||
} {
|
||||
println!("An error occured during runtime:\n\t{}", e);
|
||||
if let Some(e) = e.source() {
|
||||
|
|
@ -49,15 +52,23 @@ pub fn execute_parsed_cli(cli: Cli) -> Result<(), errors::SnippetError> {
|
|||
/// even when the arguments don't make sense
|
||||
fn populate_actions(cli: &Cli) -> Vec<Actions> {
|
||||
let mut actions = Vec::new();
|
||||
match (cli.locate, cli.create, cli.edit, cli.edit_all, cli.delete) {
|
||||
(true, _, _, _, false) => actions.push(Actions::Locate),
|
||||
(_, false, false, true, false) => actions.push(Actions::EditAll),
|
||||
(_, true, _, true, false) => actions.push(Actions::EditAll),
|
||||
(_, true, false, false, false) => actions.push(Actions::Create),
|
||||
(_, false, true, _, false) => actions.push(Actions::Edit),
|
||||
(_, true, true, false, false) => actions.push(Actions::Edit),
|
||||
(false, false, false, false, false) => actions.push(Actions::Show),
|
||||
(_, _, _, _, true) => actions.push(Actions::Delete),
|
||||
match (
|
||||
cli.locate,
|
||||
cli.create,
|
||||
cli.edit,
|
||||
cli.edit_all,
|
||||
cli.delete,
|
||||
&cli.rename,
|
||||
) {
|
||||
(true, _, _, _, false, _) => actions.push(Actions::Locate),
|
||||
(_, false, false, true, false, _) => actions.push(Actions::EditAll),
|
||||
(_, true, _, true, false, _) => actions.push(Actions::EditAll),
|
||||
(_, true, false, false, false, _) => actions.push(Actions::Create),
|
||||
(_, false, true, _, false, _) => actions.push(Actions::Edit),
|
||||
(_, true, true, false, false, _) => actions.push(Actions::Edit),
|
||||
(false, false, false, false, false, None) => actions.push(Actions::Show),
|
||||
(_, _, _, _, true, _) => actions.push(Actions::Delete),
|
||||
(_, _, _, _, _, Some(_)) => actions.push(Actions::Rename),
|
||||
}
|
||||
actions
|
||||
}
|
||||
|
|
@ -109,6 +120,18 @@ mod test {
|
|||
false_cli!("snp", "name", "-d", "-c", "-E", "-e", "-l"),
|
||||
vec![Actions::Delete]
|
||||
),
|
||||
(
|
||||
false_cli!("snp", "name", "-r", "new_name"),
|
||||
vec![Actions::Rename]
|
||||
),
|
||||
(
|
||||
false_cli!("snp", "name", "-r", "new_name", "-f"),
|
||||
vec![Actions::Rename]
|
||||
),
|
||||
(
|
||||
false_cli!("snp", "name", "-c", "-r", "new_name", "-f"),
|
||||
vec![Actions::Create]
|
||||
),
|
||||
);
|
||||
for (cli, res) in group.iter() {
|
||||
println!("Testing {:?}..actions expected {:?}", cli, res);
|
||||
|
|
@ -124,7 +147,10 @@ mod test {
|
|||
false_cli!("snp", "parsed_cli", "-c", "-f"),
|
||||
false_cli!("snp", "parsed_cli"),
|
||||
false_cli!("snp", "parsed_cli", "-E", "-f"),
|
||||
false_cli!("snp", "parsed_cli", "-d"),
|
||||
false_cli!("snp", "parsed_cli", "-r", "new_name", "-f"),
|
||||
false_cli!("snp", "parsed_cli", "-c"),
|
||||
false_cli!("snp", "parsed_cli", "-r", "new_name", "-f"),
|
||||
false_cli!("snp", "new_name", "-d"),
|
||||
];
|
||||
|
||||
let bad_cli = vec![
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ impl fmt::Display for SnippetError {
|
|||
p.file_name().unwrap(),
|
||||
),
|
||||
SnippetError::ReparsingError => write!(f,"The snipped couldn't be re-parsed, it might be caused by a bad value in your edit. \nThe snipped was saved as a raw file instead but cant be used as is."),
|
||||
SnippetError::IndexError(ref i) => write!(f,"An error occured while trying to access the index {} of the given arguments.\nThe default arguments for this command likely do not support {0} arguments",i),
|
||||
SnippetError::IndexError(ref i) => write!(f,"An error occured while trying to access the index {} of the given arguments.\nThe default arguments for this command likely do not support {} arguments",i,i+1),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,6 +47,10 @@ pub struct Cli {
|
|||
/// Wether to delete a snippet, takes precedence over everything
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
delete: bool,
|
||||
|
||||
/// Wether to rename the snippet
|
||||
#[arg(short, long)]
|
||||
rename: Option<String>,
|
||||
}
|
||||
|
||||
/// Main entry point, everything is defered for testing and coverage
|
||||
|
|
|
|||
|
|
@ -109,6 +109,12 @@ fn create_token_from_pos(
|
|||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_derive() {
|
||||
println!("{:?}", CustFormatter::new("45"));
|
||||
println!("{:?}", SnippetToken::Text("Bar"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_token_from_pos() {
|
||||
// All calls
|
||||
|
|
|
|||
|
|
@ -14,17 +14,6 @@ use crate::errors::SnippetError;
|
|||
use crate::snippet::Snippet;
|
||||
use crate::{dynamic_formatter, Cli};
|
||||
|
||||
// Potential way to chain all actions without re-opening
|
||||
// /// The status of the snippet in memory
|
||||
// pub enum PotentialSnippet {
|
||||
// /// The snippet wasn't loaded
|
||||
// FileDest(File),
|
||||
// /// The snippet was loaded
|
||||
// LoadedSnippet(Snippet),
|
||||
// /// No snippet currently exists
|
||||
// None,
|
||||
// }
|
||||
|
||||
/// Locate and print a snippet location
|
||||
///
|
||||
/// Will error on snippet not existing
|
||||
|
|
@ -61,6 +50,44 @@ pub fn create_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Rename a snippet with the new given name
|
||||
pub fn rename_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
||||
// Check wether the snippet exitsts
|
||||
let new_name = cli.rename.as_ref().unwrap();
|
||||
if new_name == &cli.snippet {
|
||||
return Ok(()); // The noop is a success ?
|
||||
}
|
||||
let (f, oldpath) = match check_dir_and_open(&cli.snippet)? {
|
||||
FileStatus::Opened((p, f)) => (f, p),
|
||||
FileStatus::NotFound(e) => return Err(SnippetError::NotFound(e)),
|
||||
};
|
||||
let mut snp = Snippet::from_file(&f)?;
|
||||
// Check whether the destination is writable
|
||||
let (f, _) = match check_dir_and_open(new_name)? {
|
||||
FileStatus::Opened((p, _)) => {
|
||||
if cli.force {
|
||||
snp.name = new_name.clone();
|
||||
(File::create(&p)?, p)
|
||||
} else {
|
||||
return Err(SnippetError::FoundNoEraseError(p));
|
||||
}
|
||||
}
|
||||
FileStatus::NotFound(p) => {
|
||||
snp.name = new_name.clone();
|
||||
(File::create(&p)?, p)
|
||||
}
|
||||
};
|
||||
// Save and delete
|
||||
snp.save_in(&f)?;
|
||||
std::fs::remove_file(oldpath)?;
|
||||
// Save snippet
|
||||
println!(
|
||||
"The snippet {} was sucessfuly renamed as {}",
|
||||
cli.snippet, new_name
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Open and load snippet and edit it
|
||||
pub fn edit_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
||||
// Try loading, if not existant check if create or error
|
||||
|
|
@ -109,7 +136,6 @@ pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
// During tests, the editor cannot be tested
|
||||
#[cfg(not(feature = "test"))]
|
||||
let buf = edit::edit(buf)?;
|
||||
|
|
@ -117,12 +143,35 @@ pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
|||
// Try reparsing, on error no modifications will be made
|
||||
match Snippet::from_string(&buf) {
|
||||
Ok(snp) => {
|
||||
snp.write_over(&p)?; // TODO: Consider not giving a path, as name can be changed in config
|
||||
println!(
|
||||
"The snippet {:?} was succesfully saved",
|
||||
p.file_name().unwrap()
|
||||
);
|
||||
Ok(())
|
||||
if cli.snippet == snp.name {
|
||||
// No renaming has occured, the file can safely be overriten
|
||||
snp.write_over(&p)?;
|
||||
println!(
|
||||
"The snippet {:?} was succesfully saved",
|
||||
p.file_name().unwrap()
|
||||
);
|
||||
Ok(())
|
||||
} else {
|
||||
let (f, p) = match check_dir_and_open(&snp.name)? {
|
||||
FileStatus::Opened((p, f)) => {
|
||||
if cli.force {
|
||||
// Erasing will take place
|
||||
(File::create(&p)?, p)
|
||||
} else {
|
||||
// No erasing/overriting
|
||||
println!(
|
||||
"Impossible to save the snippet as {:}, as it already exists. The snippet will be saved under it's old name.",
|
||||
snp.name
|
||||
);
|
||||
(f, p)
|
||||
}
|
||||
}
|
||||
FileStatus::NotFound(p) => (File::create(&p)?, p),
|
||||
};
|
||||
snp.save_in(&f)?;
|
||||
println!("The snippet was successfuly saved in {:?}", p);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Err(_) => {
|
||||
|
|
@ -256,8 +305,8 @@ pub mod test {
|
|||
|
||||
#[test]
|
||||
fn test_delete_snippet() {
|
||||
assert!(create_snippet(false_cli!("-c", "delete_snippet")).is_ok());
|
||||
assert!(delete_snippet(false_cli!("-d", "delete_snippet")).is_ok());
|
||||
assert!(create_snippet(false_cli!("snp", "-c", "-f", "delete_snippet")).is_ok());
|
||||
assert!(delete_snippet(false_cli!("snp", "-d", "delete_snippet")).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -343,4 +392,73 @@ pub mod test {
|
|||
));
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rename_snippet() {
|
||||
let oldpath = snippet_path("rename_snippet").unwrap();
|
||||
let newpath = snippet_path("renamed_snippet").unwrap();
|
||||
let filledpath = snippet_path("filled_destination").unwrap();
|
||||
_ = std::fs::remove_file(&oldpath);
|
||||
_ = std::fs::remove_file(&newpath);
|
||||
_ = std::fs::remove_file(filledpath);
|
||||
assert!(create_snippet(false_cli!("-c", "rename_snippet", "-f")).is_ok());
|
||||
assert!(
|
||||
rename_snippet(false_cli!("snp", "-r", "renamed_snippet", "rename_snippet")).is_ok()
|
||||
);
|
||||
|
||||
assert!(std::fs::remove_file(oldpath).is_err());
|
||||
assert!(std::fs::remove_file(newpath).is_ok());
|
||||
|
||||
assert!(rename_snippet(false_cli!("snp", "-r", "non_ex", "non_ex")).is_ok());
|
||||
assert!(rename_snippet(false_cli!("snp", "-r", "non_ex", "renamed_snippet")).is_err());
|
||||
assert!(create_snippet(false_cli!("-c", "filled_destination", "-f")).is_ok());
|
||||
assert!(create_snippet(false_cli!("-c", "rename_snippet", "-f")).is_ok());
|
||||
assert!(matches!(
|
||||
rename_snippet(false_cli!(
|
||||
"snp",
|
||||
"rename_snippet",
|
||||
"-r",
|
||||
"filled_destination"
|
||||
)),
|
||||
Err(SnippetError::FoundNoEraseError(_))
|
||||
));
|
||||
let res = rename_snippet(false_cli!(
|
||||
"snp",
|
||||
"rename_snippet",
|
||||
"-r",
|
||||
"filled_destination",
|
||||
"-f"
|
||||
));
|
||||
println!("{:?}", res);
|
||||
assert!(res.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resaving_new_name() {
|
||||
// Try to resave under another name
|
||||
_ = std::fs::remove_file(snippet_path("resave_snippet").unwrap());
|
||||
_ = std::fs::remove_file(snippet_path("new_snippet").unwrap());
|
||||
let p = snippet_path("resave_snippet").unwrap();
|
||||
let mut f = File::create(p).unwrap();
|
||||
|
||||
// Populate file with another name
|
||||
let snp_str = Snippet::empty("new_snippet".to_string(), vec![]);
|
||||
write!(f, "{}", snp_str).unwrap();
|
||||
|
||||
// Try to resave
|
||||
let res = edit_whole_snippet(false_cli!("snp", "resave_snippet"));
|
||||
println!("Trying to resave without destination {:?}", res);
|
||||
assert!(res.is_ok());
|
||||
|
||||
assert!(create_snippet(false_cli!("-c", "new_snippet")).is_err());
|
||||
assert!(create_snippet(false_cli!("-c", "resave_snippet")).is_err());
|
||||
|
||||
let res = edit_whole_snippet(false_cli!("snp", "resave_snippet", "-f"));
|
||||
println!("Sucessfully erasing {:?}", res);
|
||||
assert!(res.is_ok());
|
||||
|
||||
let res = edit_whole_snippet(false_cli!("snp", "resave_snippet"));
|
||||
println!("Failing to overwrite {:?}", res);
|
||||
assert!(res.is_err());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in New Issue