diff options
| -rw-r--r-- | lib/config.ha | 50 | ||||
| -rw-r--r-- | main.ha | 22 |
2 files changed, 67 insertions, 5 deletions
diff --git a/lib/config.ha b/lib/config.ha new file mode 100644 index 0000000..def33dd --- /dev/null +++ b/lib/config.ha @@ -0,0 +1,50 @@ +use dirs; +use format::ini; +use fs; +use io; +use strings; +use os; + +export type config = struct { files: []str }; + +fn load_item(scanner: *ini::scanner) (ini::entry | done) = { + match(ini::next(scanner)) { + case let entry: ini::entry => return entry; + case => return done; + }; +}; + +// caller must free after use using [[config_free]] +export fn config_read() (*config | fs::error | nomem) = { + + const config_file = dirs::config("bunnytask"); + const config_file = strings::concat(config_file, "/config.ini")?; + defer free(config_file); + + // open + const config_file = os::open(config_file)?; + defer io::close(config_file)!; + + const scanner = ini::scan(config_file); + defer ini::finish(&scanner); + + // read + let files: []str = []; + + for (let entry => load_item(&scanner)) { + if (strings::trim(entry.1) == "todo_file") { + // TODO shell expand + const file = strings::trim(entry.2); + + append(files, strings::dup(file)?)?; + }; + }; + + // return + return alloc(config { files = files })?; +}; + +export fn config_free(config: *config) void = { + strings::freeall(config.files); + free(config); +}; @@ -1,17 +1,29 @@ use fmt; +use fs; +use lib; use strings; use todo; -use fs; - -def TODO_FILE = "todo.txt"; export fn main() void = { - const todos = match(todo::open(TODO_FILE)) { + + // load config + const config = match (lib::config_read()) { + case let config: *lib::config => yield config; + case let err: fs::error => fmt::fatalf("Error opening config: {}", fs::strerror(err)); + }; + defer lib::config_free(config); + + // TODO handle multiple todo files + const todo_file = config.files[0]; + + // load todos + 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)); + case let err: fs::error => fmt::fatalf("Error opening {}: {}", todo_file, fs::strerror(err)); }; defer strings::freeall(todos); + // print todos fmt::println("To-dos:")!; for (let item .. todos) { |
