script / Console, Files // A to-do list in a file: `frits run todo` shows it, `frits run todo add // Buy milk` adds a line, `frits run todo done 2` takes the second off. let PATH = "todo.txt" fn items(text: Str) -> List { text.split("\n").filter(fn(line) { line.trim() != "" }) } fn rows(items: List) -> List> { var out: List> = [] var n = 0 for item in items { n += 1 out.push(["{n}", item]) } out } fn show(list: List) / Console { if len(list) == 0 { Console.print("Nothing to do.") } else { Console.table(["", "To do"], rows(list)) } } on main() { let args = Console.args() let list = match Files.read(PATH) { Ok(text) => items(text), Err(_) => [], } match args.get(0) { Some(verb) if verb == "add" => { let item = args.slice(1, len(args)).join(" ") match Files.append(PATH, "{item}\n") { Ok(_) => show(list + [item]), Err(e) => Console.print("Could not write the list: {e}"), } } Some(verb) if verb == "done" => { let n = match args.get(1) { Some(word) => word.parse_int(), None => None, } match n { Some(i) if i >= 1 && i <= len(list) => { var left = list left.remove_at(i - 1) match Files.write(PATH, left.join("\n") + "\n") { Ok(_) => show(left), Err(e) => Console.print("Could not write the list: {e}"), } } _ => Console.print("Which one? Give its number: done 2"), } } Some(verb) => Console.print("“{verb}” is not something I do: add, done, or nothing to see the list"), None => show(list), } } test "blank lines are not items" { assert(items("milk\n\n bread \n") == ["milk", " bread "], "two items") } test "rows are numbered from one" { assert(rows(["a", "b"]) == [["1", "a"], ["2", "b"]], "numbered") }