name = "todo_manager" category = "filesystem" mode = "convert" description = """ A simple todo list manager. Read commands from stdin, one per line: add — append the task to the todo file list — print all tasks numbered as "NN). " remove — remove task number N (1-based) clear — remove all tasks The todo file is "todo.txt" in the working directory. When listing, pad task numbers to two digits (01, 02, …). After "add" or "remove", automatically list the remaining tasks. If the list is empty, print "No tasks found". """ bash_source = ''' #!/bin/bash TODOFILE="./todo.txt" list_tasks() { if [ -f "$TODOFILE" ] && [ -s "$TODOFILE" ]; then count=1 IFS=$'\\n' while read -r task; do num=$count if [ $count -lt 10 ]; then num="0$count"; fi echo "$num). $task" count=$(( count + 1 )) done < "$TODOFILE" else echo "No tasks found" fi } add_task() { echo "$1" >> "$TODOFILE" } remove_task() { taskNum=$1 totalLines=$(wc -l < "$TODOFILE" | tr -d ' ') if [ "$taskNum" -gt "$totalLines" ] 2>/dev/null; then echo "Error: task number $taskNum does not exist!" return 1 fi tmpfile="./todo_tmp.txt" count=1 IFS=$'\\n' while read -r task; do if [ "$count" -ne "$taskNum" ]; then echo "$task" >> "$tmpfile" fi count=$(( count + 1 )) done < "$TODOFILE" if [ -f "$tmpfile" ]; then mv "$tmpfile" "$TODOFILE" else > "$TODOFILE" fi echo "Sucessfully removed task number $taskNum" } clear_tasks() { > "$TODOFILE" echo "Tasks cleared." } if [ ! -f "$TODOFILE" ]; then touch "$TODOFILE" fi while IFS= read -r line || [[ -n "$line" ]]; do cmd=$(echo "$line" | cut -d' ' -f1) arg=$(echo "$line" | cut -d' ' -f2-) case "$cmd" in add) add_task "$arg" list_tasks ;; list) list_tasks ;; remove) remove_task "$arg" list_tasks ;; clear) clear_tasks ;; esac done ''' [[test_cases]] description = "Add tasks then list" stdin = """add Buy groceries add Walk the dog list""" expected_stdout = """01). Buy groceries 01). Buy groceries 02). Walk the dog 01). Buy groceries 02). Walk the dog""" [[test_cases]] description = "Add, remove, list" stdin = """add First task add Second task add Third task remove 2 list""" expected_stdout = """01). First task 01). First task 02). Second task 01). First task 02). Second task 03). Third task Sucessfully removed task number 2 01). First task 02). Third task 01). First task 02). Third task""" [[test_cases]] description = "Empty list and clear" stdin = """list add Something clear list""" expected_stdout = """No tasks found 01). Something Tasks cleared. No tasks found""" [[test_cases]] description = "Works with pre-existing todo file" stdin = """list add Third item list""" setup_files = { "todo.txt" = "Existing item one\nExisting item two\n" } expected_stdout = """01). Existing item one 02). Existing item two 01). Existing item one 02). Existing item two 03). Third item 01). Existing item one 02). Existing item two 03). Third item""" expected_files = { "todo.txt" = "Existing item one\nExisting item two\nThird item\n" }