balls
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/target
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "notes-rust"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "notes-rust"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
+177
@@ -0,0 +1,177 @@
|
||||
use std::io::{self, Write};
|
||||
|
||||
struct Notes {
|
||||
id: u32,
|
||||
title: String,
|
||||
content: Vec<String>,
|
||||
}
|
||||
|
||||
impl Notes {
|
||||
fn new(id: u32, title: String, content: Vec<String>) -> Notes {
|
||||
Notes {
|
||||
id,
|
||||
title,
|
||||
content
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct NotesCollection {
|
||||
notes: Vec<Notes>,
|
||||
next_id: u32,
|
||||
}
|
||||
|
||||
impl NotesCollection {
|
||||
fn new() -> NotesCollection {
|
||||
NotesCollection {
|
||||
notes: Vec::new(),
|
||||
next_id: 1,
|
||||
}
|
||||
}
|
||||
|
||||
fn add(&mut self, title: String, content: Vec<String>) -> u32 {
|
||||
let note = Notes::new(self.next_id, title, content);
|
||||
let id = note.id;
|
||||
|
||||
self.notes.push(note);
|
||||
self.next_id += 1;
|
||||
|
||||
id
|
||||
}
|
||||
|
||||
fn remove(&mut self, id: u32) -> bool {
|
||||
if let Some(pos) = self.notes.iter().position(|n| n.id == id) {
|
||||
self.notes.remove(pos);
|
||||
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn list(&self) -> Vec<(u32, &String)> {
|
||||
self.notes.iter().map(|n| (n.id, &n.title)).collect()
|
||||
}
|
||||
|
||||
fn count(&self) -> usize {
|
||||
self.notes.len()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_input(prompt: &str) -> String {
|
||||
print!("{prompt}");
|
||||
io::stdout().flush().expect("Failed to flush");
|
||||
|
||||
let mut input = String::new();
|
||||
|
||||
match io::stdin().read_line(&mut input) {
|
||||
Ok(_) => {}
|
||||
Err(e) => eprintln!("Error reading input: {e}"),
|
||||
}
|
||||
|
||||
input.trim().to_string()
|
||||
}
|
||||
|
||||
fn mode_add(collection: &mut NotesCollection) {
|
||||
let title: String;
|
||||
|
||||
loop {
|
||||
let temp_title = get_input("Set title: ");
|
||||
|
||||
if temp_title.is_empty() {
|
||||
println!("Pls no empty title");
|
||||
} else {
|
||||
title = temp_title.to_string();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let mut content: Vec<String> = Vec::new();
|
||||
|
||||
loop {
|
||||
let item = get_input("Enter item (- to exit): ");
|
||||
|
||||
if item.is_empty() {
|
||||
println!("Pls no empty item");
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if item == "-" {
|
||||
break;
|
||||
}
|
||||
|
||||
content.push(item);
|
||||
}
|
||||
|
||||
let id = collection.add(title, content);
|
||||
|
||||
println!("Note #{} saved!", id);
|
||||
}
|
||||
|
||||
fn mode_delete(collection: &mut NotesCollection) {
|
||||
if collection.count() == 0 {
|
||||
println!("Nothing to delete bruh");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
for (id, title) in collection.list() {
|
||||
println!("[{}] {}", id, title);
|
||||
}
|
||||
|
||||
let input = get_input("Enter note ID to delete (- to cancel): ");
|
||||
|
||||
if input == "-" {
|
||||
return;
|
||||
}
|
||||
|
||||
match input.trim().parse::<u32>() {
|
||||
Ok(id) => {
|
||||
if collection.remove(id) {
|
||||
println!("Note #{} deleted!", id);
|
||||
} else {
|
||||
println!("No note with the ID {}!!!", id);
|
||||
}
|
||||
}
|
||||
Err(_) => println!("Invalid ID!"),
|
||||
}
|
||||
}
|
||||
|
||||
fn mode_view(collection: &NotesCollection) {
|
||||
if collection.count() == 0 {
|
||||
println!("Nothing here bruh");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
for note in &collection.notes {
|
||||
println!("{}", note.title);
|
||||
|
||||
for item in ¬e.content {
|
||||
println!(" - {}", item);
|
||||
}
|
||||
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut notes: NotesCollection = NotesCollection::new();
|
||||
|
||||
loop {
|
||||
let mode = get_input("Select mode (v | a | d): "); // view, add, delete
|
||||
// println!("{mode}");
|
||||
|
||||
match mode.as_str() {
|
||||
"a" => mode_add(&mut notes),
|
||||
"d" => mode_delete(&mut notes),
|
||||
"v" => mode_view(¬es),
|
||||
"q" => break,
|
||||
_ => {
|
||||
println!("not a valid input");
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user