99 lines
2.5 KiB
JavaScript
Raw Normal View History

2026-01-25 11:42:49 +01:00
#!/usr/local/bin/wren
2026-01-25 10:50:20 +01:00
// wren source code generator v1.0
// Generation time: 0.101419
import "io" for File, Directory, Stdin, Stdout
import "sqlite" for Database
class Phonebook {
construct new() {
_db = Database.memory()
init_()
}
init_() {
_db.execute("CREATE TABLE IF NOT EXISTS contacts (id INTEGER PRIMARY KEY, name TEXT, phone TEXT)", [])
}
add(name, phone) {
_db.execute("INSERT INTO contacts (name, phone) VALUES (?, ?)", [name, phone])
System.print("Contact added: %(name)")
}
list() {
var rows = _db.query("SELECT * FROM contacts", [])
if (rows.isEmpty) {
System.print("No contacts found.")
return
}
System.print("\n--- Phonebook ---")
for (row in rows) {
System.print("%(row["name"]) : %(row["phone"])")
}
System.print("")
}
search(name) {
var rows = _db.query("SELECT * FROM contacts WHERE name LIKE ?", ["\%" + name + "\%"])
if (rows.isEmpty) {
System.print("No contacts found for '%(name)'.")
return
}
System.print("\n--- Search Results ---")
for (row in rows) {
System.print("%(row[1]) : %(row[2])")
}
System.print("")
}
delete(name) {
_db.execute("DELETE FROM contacts WHERE name = ?", [name])
System.print("Contact deleted: %(name)")
}
run() {
System.print("\n=== CLI Phonebook ===")
var running = true
while (running) {
System.print("\n1. Add contact")
System.print("2. List contacts")
System.print("3. Search contact")
System.print("4. Delete contact")
System.print("5. Exit")
System.write("Choose option: ")
Stdout.flush()
var choice = Stdin.readLine().trim()
if (choice == "1") {
System.write("Name: ")
Stdout.flush()
var name = Stdin.readLine().trim()
System.write("Phone: ")
Stdout.flush()
var phone = Stdin.readLine().trim()
add(name, phone)
} else if (choice == "2") {
list()
} else if (choice == "3") {
System.write("Search name: ")
Stdout.flush()
var name = Stdin.readLine().trim()
search(name)
} else if (choice == "4") {
System.write("Contact name to delete: ")
Stdout.flush()
var name = Stdin.readLine().trim()
delete(name)
} else if (choice == "5") {
System.print("Goodbye!")
running = false
} else {
System.print("Invalid option.")
}
}
}
}
var pb = Phonebook.new()
pb.run()