diff options
| author | Autumn | 2026-08-23 11:33:35 +0100 |
|---|---|---|
| committer | Autumn | 2026-08-23 11:33:35 +0100 |
| commit | 6337a8d398a223345e2e22ede0911d5e6dcab019 (patch) | |
| tree | 297121cbda8bf765d9d9cd64b1d31fd850882900 | |
| parent | 47a3890c0055e04ffba2984f1870452cb1653435 (diff) | |
[bunnytask] added basic TODO.TXT reading
| -rw-r--r-- | lib/slice.ha | 21 | ||||
| -rw-r--r-- | main.ha | 20 | ||||
| -rw-r--r-- | todo/open.ha | 23 | ||||
| -rw-r--r-- | todo/print.ha | 5 |
4 files changed, 69 insertions, 0 deletions
diff --git a/lib/slice.ha b/lib/slice.ha new file mode 100644 index 0000000..7cd2e30 --- /dev/null +++ b/lib/slice.ha @@ -0,0 +1,21 @@ +use fmt; + +export fn slice_remove_empty(slice: *[]str) void = { + for (let i = 0z; i < len(slice); i += 1) { + if (slice[i] == "") { + delete(slice[i]); + i -= 1; + }; + }; +}; + +@test fn slice_remove_empty() void = { + const slice: []str = alloc(["autumn", "", "winter"])!; + defer free(slice); + + slice_remove_empty(&slice); + + assert(len(slice) == 2); + assert(slice[0] == "autumn"); + assert(slice[1] == "winter"); +}; @@ -0,0 +1,20 @@ +use fmt; +use strings; +use todo; +use fs; + +def TODO_FILE = "todo.txt"; + +export fn main() void = { + const todos = match(todo::open(TODO_FILE)) { + case let todos: []str => yield todos; + case let err: fs::error => fmt::fatalf("Error opening {}: {}", TODO_FILE, fs::strerror(err)); + }; + defer strings::freeall(todos); + + fmt::println("To-dos:")!; + + for (let item .. todos) { + if (item != "") todo::print(item); + }; +}; diff --git a/todo/open.ha b/todo/open.ha new file mode 100644 index 0000000..7692635 --- /dev/null +++ b/todo/open.ha @@ -0,0 +1,23 @@ +use fs; +use io; +use lib; +use os; +use strings; + +export fn open(file: str) ([]str | fs::error) = { + + // open + const todos = os::open(file)?; + defer io::close(todos)!; + + const todos = io::drain(todos)!; + defer free(todos); + + // read + const todos = strings::fromutf8(todos)!; + const todos = strings::split(todos, "\n")!; + + lib::slice_remove_empty(&todos); + + return strings::dupall(todos)?; +}; diff --git a/todo/print.ha b/todo/print.ha new file mode 100644 index 0000000..4106fd9 --- /dev/null +++ b/todo/print.ha @@ -0,0 +1,5 @@ +use fmt; + +export fn print(todo: str) void = { + fmt::printfln("* {}", todo)!; +}; |
