Skip to content

Commit 5b10797

Browse files
committed
✨ Add remove functionality
1 parent 8e9efa4 commit 5b10797

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Rust Bookmark (d)Menu is a dmenu/ncurses based interface to manage bookmarks ind
55
## Features
66
- Insert Bookmark
77
- List Bookmark(s) \[With coloured output]
8+
- Remove Bookmark
89

910
## FAQ
1011
**Location of Bookmark file ?**
@@ -53,6 +54,11 @@ Without the name option, `rbmenu -l` displays all the available bookmarks. Give
5354

5455
`rbmenu -ln "git*"`
5556

57+
**Remove Bookmark**
58+
59+
Use the `-r` option with a valid unsigned integer (+ve only) to delete the bookmark with the specific id.
60+
`rbmenu -r 1`
61+
5662
## License
5763

5864
RBMenu is licensed under the GPL-3 license.

res/rbmenu.1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Name of the bookmark. Required for
4040
Optional argument to
4141
.B \-l
4242
for regex matching. If the value contains spaces, it is converted to underscores.
43+
.TP
44+
.B \-r
45+
Remove bookmark with the given id. Exits if bookmark with the given id is not found.
4346
.SH USAGE
4447
.TP
4548
.B echo 'https://github.com' | rbmenu -in 'github'
@@ -53,6 +56,9 @@ Displays all the bookmarks saved in a "space" seperated value. This allows for e
5356
.TP
5457
.B rbmenu -ln 'git*'
5558
Displays all the bookmarks that match the given regular expression pattern.
59+
.TP
60+
.B rbemnu -r 1
61+
Removes the bookmark with id = 1
5662
.SH FILES
5763
.TP
5864
.B ~/.local/share/rbmenu/bookmark.json

src/commands.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::config::Config;
22
use super::data::{Bookmark, Data};
33

44
use regex::Regex;
5+
use std::process;
56

67
/// Insert commands
78
/// Adds the bookmark to the data list and increments the last id
@@ -39,6 +40,27 @@ pub fn list(data: Data, config: Config) {
3940
print_bookmark(&search_results);
4041
}
4142

43+
/// Remove command
44+
/// Exits if bookmark with the said id is not available
45+
/// Remove the bookmark with the given id and exit.
46+
pub fn remove(data: &mut Data, index: u32) {
47+
// Loop through bookmarks using a index so we can remove it later.
48+
for i in 0..data.bookmarks.len() {
49+
let bookmark = &data.bookmarks[i];
50+
51+
// Continue if its not the one we want
52+
if bookmark.id != index {
53+
continue;
54+
}
55+
56+
data.bookmarks.remove(i);
57+
data.write_to_file();
58+
return;
59+
}
60+
eprintln!("Bookmark with id = {} was not found!", index);
61+
process::exit(1);
62+
}
63+
4264
/// Print all bookmarks in the vector (in color)
4365
fn print_bookmark(input: &Vec<Bookmark>) {
4466
for i in input {

src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ pub struct Config {
1717
///Name of bookmark ; Required for -i or -l
1818
#[structopt(short, long)]
1919
pub name: Option<String>,
20+
21+
///Remove a bookmark with the given id
22+
#[structopt(short, long)]
23+
pub remove: Option<u32>,
2024
}

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod config;
33
mod data;
44
mod parser;
55

6-
use commands::{insert, list};
6+
use commands::{insert, list, remove};
77
use config::Config;
88
use data::read_data_file;
99

@@ -13,7 +13,7 @@ use structopt::StructOpt;
1313
/// Call command functions based on given options
1414
pub fn run() {
1515
let opts = Config::from_args();
16-
let data = read_data_file();
16+
let mut data = read_data_file();
1717

1818
// Run based on options
1919
if opts.insert {
@@ -25,5 +25,7 @@ pub fn run() {
2525
insert(input_url, data, opts);
2626
} else if opts.list {
2727
list(data, opts);
28+
} else if opts.remove.is_some() {
29+
remove(&mut data, opts.remove.unwrap());
2830
}
2931
}

0 commit comments

Comments
 (0)