Skip to content

Commit

Permalink
delete bookmark by index
Browse files Browse the repository at this point in the history
  • Loading branch information
0xhenrique committed Apr 27, 2024
1 parent e8acf8f commit ba7d034
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
2 changes: 1 addition & 1 deletion assets/lum-marker.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"owner":"admin","created_at":1714182655,"bookmarks":[{"title":"Hyouka - Saint Pepsi","link":"https://www.youtube.com/watch?v=SpQCuPTWrrI"},{"title":"【original anime MV】SHINKIRO【hololive/宝鐘マリン・Gawr Gura】","link":"https://www.youtube.com/watch?v=9ehwhQJ50gs"},{"title":"【MV】Lucky☆Orb feat. Hatsune Miku by emon(Tes.) / ラッキー☆オーブ feat. 初音ミク by emon(Tes.) 【MIKU EXPO 5th】","link":"https://www.youtube.com/watch?v=AufydOsiD6M"},{"title":"[Ochame Kinou] hololive Fukkireta Chorus [24 VTubers]","link":"https://www.youtube.com/watch?v=9kQ2GtvDV3s"},{"title":"【MV】White Love/SPEED (Cover)【FUWAMOCO】","link":"https://www.youtube.com/watch?v=D8YflSQi1Vk"},{"title":"Re:Zero ED / Ending 2 Full『Emilia (Rie Takahashi) - Stay Alive』【ENG Sub】","link":"https://www.youtube.com/watch?v=z-NuxWkYtlI"},{"title":"arrays and vectors in rust (btw)","link":"https://www.cs.brandeis.edu/~cs146a/rust/doc-02-21-2015/book/arrays-vectors-and-slices.html"},{"title":"lambda expressions","link":"https://www.gnu.org/software/emacs/manual/html_node/eintr/lambda.html"}]}
{"owner":"admin","created_at":1714182655,"bookmarks":[{"title":"Hyouka - Saint Pepsi","link":"https://www.youtube.com/watch?v=SpQCuPTWrrI"},{"title":"【original anime MV】SHINKIRO【hololive/宝鐘マリン・Gawr Gura】","link":"https://www.youtube.com/watch?v=9ehwhQJ50gs"},{"title":"【MV】Lucky☆Orb feat. Hatsune Miku by emon(Tes.) / ラッキー☆オーブ feat. 初音ミク by emon(Tes.) 【MIKU EXPO 5th】","link":"https://www.youtube.com/watch?v=AufydOsiD6M"},{"title":"[Ochame Kinou] hololive Fukkireta Chorus [24 VTubers]","link":"https://www.youtube.com/watch?v=9kQ2GtvDV3s"},{"title":"【MV】White Love/SPEED (Cover)【FUWAMOCO】","link":"https://www.youtube.com/watch?v=D8YflSQi1Vk"},{"title":"Re:Zero ED / Ending 2 Full『Emilia (Rie Takahashi) - Stay Alive』【ENG Sub】","link":"https://www.youtube.com/watch?v=z-NuxWkYtlI"},{"title":"arrays and vectors in rust (btw)","link":"https://www.cs.brandeis.edu/~cs146a/rust/doc-02-21-2015/book/arrays-vectors-and-slices.html"},{"title":"lambda expressions","link":"https://www.gnu.org/software/emacs/manual/html_node/eintr/lambda.html"}]}
68 changes: 65 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,81 @@ fn view_bookmarks() {

println!("Bookmarks for {}: ", bookmark_data.owner);
for (index, bookmark) in bookmark_data.bookmarks.iter().enumerate() {
println!("{}. {}\n - {}\n", index + 1, bookmark.title, bookmark.link);
println!("{}. {}\n - {}\n", index, bookmark.title, bookmark.link);
}
}
Err(error) => {
println!("Failed to read bookmark file due to: {}", error);
return;
}
}
handle_options_from_user_input();
}

fn delete_bookmark() {
println!("Deleting bookmark now...");
view_bookmarks();

println!("Enter the index of the bookmark you want to delete:");
let mut index_input = String::new();
io::stdin().read_line(&mut index_input).expect("Failed to read input.");
let index_input: usize = match index_input.trim().parse() {
Ok(index) => index,
Err(error) => {
println!("Invalid input. Please enter a valid index due to: {}", error);
return;
}
};

let mut file = match File::open("assets/lum-marker.json") {
Ok(file) => file,
Err(error) => {
println!("Failed to open bookmark file: {}", error);
return;
}
};

let mut contents = String::new();
if let Err(error) = file.read_to_string(&mut contents) {
println!("Failed to read bookmark file: {}", error);
return;
}

let mut bookmark_data: BookmarkData = match serde_json::from_str(&contents) {
Ok(data) => data,
Err(error) => {
println!("Failed to parse bookmark data: {}", error);
return;
}
};

if index_input >= bookmark_data.bookmarks.len() {
println!("Index out of bounds. This bookmark probably DO NOT exist.");
return;
}

bookmark_data.bookmarks.remove(index_input);

let new_contents = match serde_json::to_string(&bookmark_data) {
Ok(data) => data,
Err(error) => {
println!("Failed to serialize bookmark data: {}", error);
return;
}
};

let mut file = match OpenOptions::new().write(true).truncate(true).open("assets/lum-marker.json") {
Ok(file) => file,
Err(error) => {
println!("Failed to open bookmark file for writing: {}", error);
return;
}
};

if let Err(error) = file.write_all(new_contents.as_bytes()) {
println!("Failed to write updated bookmark data to file: {}", error);
return;
}

println!("Bookmark deleted successfully.");
}

fn purge_bookmarks() {
Expand Down

0 comments on commit ba7d034

Please sign in to comment.