Skip to content

Commit 74518a5

Browse files
committed
Remove tags, Unmark paths
1 parent 6d8a45e commit 74518a5

File tree

6 files changed

+64
-40
lines changed

6 files changed

+64
-40
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
2-
.DS_Store
2+
.DS_Store
3+
.vscode

.vscode/launch.json

-36
This file was deleted.

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tag"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,18 @@ $ brew install char/tap/tag
1515
```sh
1616
$ tag mark <path> "tag1,tag2"
1717
```
18+
1819
### Search a tag
1920
```sh
2021
$ tag find "tag"
2122
```
23+
24+
### Remove all tags from a path
25+
```sh
26+
$ tag unmark <path>
27+
```
28+
29+
### Delete a tag from all paths
30+
```sh
31+
$ tag deltag <path>
32+
```

src/main.rs

+49-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@ pub struct Location {
1313
location: String,
1414
}
1515

16+
fn unmark_path(mut connection: Connection, path: &str) -> Result<(), GeneralError> {
17+
let dir = PathBuf::from(path.trim());
18+
if !Path::new(&dir).exists() {
19+
panic!("Path does not exist");
20+
}
21+
22+
let absolute_path = fs::canonicalize(&dir)?.to_string_lossy().to_string();
23+
24+
// Use a transaction in-case we fail to insert a tag at some point.
25+
let tx = connection.transaction()?;
26+
27+
tx.execute(
28+
"DELETE FROM tagged WHERE location = ?1;",
29+
&[&absolute_path],
30+
)?;
31+
32+
tx.commit()?;
33+
34+
Ok(())
35+
}
36+
1637
fn mark_path(mut connection: Connection, path: &str, tags: &str) -> Result<(), GeneralError> {
1738
let dir = PathBuf::from(path.trim());
1839
if !Path::new(&dir).exists() {
@@ -82,7 +103,7 @@ fn main() -> Result<(), GeneralError> {
82103
}
83104
}
84105

85-
let connection = Connection::open(dir)?;
106+
let mut connection = Connection::open(dir)?;
86107

87108
connection.execute(
88109
"CREATE TABLE IF NOT EXISTS tagged (
@@ -98,6 +119,11 @@ fn main() -> Result<(), GeneralError> {
98119
.propagate_version(true)
99120
.subcommand_required(true)
100121
.arg_required_else_help(true)
122+
.subcommand(
123+
Command::new("unmark")
124+
.about("Remove all tags from a specified path")
125+
.arg(clap::arg!([PATH]))
126+
)
101127
.subcommand(
102128
Command::new("mark")
103129
.about("Give a path specified tags")
@@ -108,6 +134,11 @@ fn main() -> Result<(), GeneralError> {
108134
clap::arg!(-c --"in-cwd" [IN_CWD] "filters by paths in the current working directory"),
109135
clap::arg!([TAGS]),
110136
]))
137+
.subcommand(
138+
Command::new("deltag")
139+
.about("Remove specified tags from all paths")
140+
.arg(clap::arg!([TAGS]))
141+
)
111142
.subcommand(Command::new("tags").about("Get a list of all tags"))
112143
.get_matches();
113144

@@ -117,6 +148,10 @@ fn main() -> Result<(), GeneralError> {
117148
let tags: String = sub_matches.value_of_t_or_exit("TAGS");
118149
mark_path(connection, &path, &tags)?;
119150
}
151+
Some(("unmark", sub_matches)) => {
152+
let path: String = sub_matches.value_of_t_or_exit("PATH");
153+
unmark_path(connection, &path)?;
154+
}
120155
Some(("find", sub_matches)) => {
121156
let tags: String = sub_matches.value_of_t_or_exit("TAGS");
122157
let in_cwd: bool = sub_matches
@@ -132,6 +167,19 @@ fn main() -> Result<(), GeneralError> {
132167
println!("{}", row?);
133168
}
134169
}
170+
Some(("deltag", sub_matches)) => {
171+
let tags: String = sub_matches.value_of_t_or_exit("TAGS");
172+
let tx = connection.transaction()?;
173+
174+
for tag in tags.split(',') {
175+
tx.execute(
176+
"DELETE FROM tagged WHERE tag = ?1;",
177+
&[&tag],
178+
)?;
179+
}
180+
181+
tx.commit()?;
182+
}
135183
_ => unreachable!("Exhausted list of subcommands and subcommand_required prevents `None`"),
136184
};
137185

0 commit comments

Comments
 (0)