Write rust cli tool for todo.txt
- User
Write rust cli tool for todo.txt
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Write rust cli tool for todo.txt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is a Rust implementation of a simple CLI tool for managing a todo.txt file. This tool supports basic operations such as adding tasks, listing tasks, marking tasks as done, and removing tasks.
use std::env;
use std::fs::{self, OpenOptions};
use std::io::{self, Write};
use std::path::Path;
const TODO_FILE: &str = "todo.txt";
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
println!("Usage: todo [add|list|done|remove] [task]");
return;
}
let command = &args[1];
match command.as_str() {
"add" => {
if args.len() < 3 {
println!("Error: Please provide a task to add.");
return;
}
let task = args[2..].join(" ");
add_task(&task);
}
"list" => list_tasks(),
"done" => {
if args.len() < 3 {
println!("Error: Please provide the task number to mark as done.");
return;
}
if let Ok(index) = args[2].parse::<usize>() {
mark_done(index);
} else {
println!("Error: Invalid task number.");
}
}
"remove" => {
if args.len() < 3 {
println!("Error: Please provide the task number to remove.");
return;
}
if let Ok(index) = args[2].parse::<usize>() {
remove_task(index);
} else {
println!("Error: Invalid task number.");
}
}
_ => println!("Error: Unknown command. Use 'add', 'list', 'done', or 'remove'."),
}
}
fn add_task(task: &str) {
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(TODO_FILE)
.expect("Failed to open todo.txt");
writeln!(file, "{}", task).expect("Failed to write to todo.txt");
println!("Task added: {}", task);
}
fn list_tasks() {
if !Path::new(TODO_FILE).exists() {
println!("No tasks found.");
return;
}
let contents = fs::read_to_string(TODO_FILE).expect("Failed to read todo.txt");
let tasks: Vec<&str> = contents.lines().collect();
if tasks.is_empty() {
println!("No tasks found.");
return;
}
for (index, task) in tasks.iter().enumerate() {
println!("{}. {}", index + 1, task);
}
}
fn mark_done(index: usize) {
if !Path::new(TODO_FILE).exists() {
println!("No tasks found.");
return;
}
let contents = fs::read_to_string(TODO_FILE).expect("Failed to read todo.txt");
let mut tasks: Vec<&str> = contents.lines().collect();
if index == 0 || index > tasks.len() {
println!("Error: Task number out of range.");
return;
}
let task = tasks[index - 1];
tasks[index - 1] = &format!("x {}", task);
write_tasks(&tasks);
println!("Task marked as done: {}", task);
}
fn remove_task(index: usize) {
if !Path::new(TODO_FILE).exists() {
println!("No tasks found.");
return;
}
let contents = fs::read_to_string(TODO_FILE).expect("Failed to read todo.txt");
let mut tasks: Vec<&str> = contents.lines().collect();
if index == 0 || index > tasks.len() {
println!("Error: Task number out of range.");
return;
}
let removed_task = tasks.remove(index - 1);
write_tasks(&tasks);
println!("Task removed: {}", removed_task);
}
fn write_tasks(tasks: &[&str]) {
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.open(TODO_FILE)
.expect("Failed to open todo.txt");
for task in tasks {
writeln!(file, "{}", task).expect("Failed to write to todo.txt");
}
}
Command Parsing:
env::args().add, list, done, or remove followed by optional arguments (e.g., task description or task index).File Operations:
todo.txt file is used to store tasks.fs::read_to_string, OpenOptions, etc.).Commands:
add: Appends a new task to the todo.txt file.list: Reads and displays all tasks with their indices.done: Marks a task as done by prepending x to the task text.remove: Deletes a task from the list.Error Handling:
Persistence:
todo.txt file, ensuring persistence across sessions.Add a Task:
$ todo add Buy groceries
Task added: Buy groceries
List Tasks:
$ todo list
1. Buy groceries
2. Clean the house
Mark a Task as Done:
$ todo done 1
Task marked as done: Buy groceries
Remove a Task:
$ todo remove 2
Task removed: Clean the house
todo.txt is stored.cargo build --release and place it in your system's PATH for easy access.This implementation provides a robust foundation for a todo.txt CLI tool in Rust.