Save to a file
You'll read the task list from a JSON file under your home directory and write it back on every change. A missing file starts the list with a default. It starts from the app you built in 620.
Before you start
Install from crates.io. You need no Node and no package manager.
cargo install snapfire_compiler
cargo install snapfire_fsr_cli
fsr --version
Every command and screenshot on this page was captured with fsr 0.x.
The machine comes from std#
A terminal app's page and loader read the machine through file and path from @snapfire/fsr/host and system from @snapfire/fsr/terminal:
| Member | What it has |
|---|---|
file | read, write, exists, list, makeDir and stat |
path | join, resolve, normalize, dirname, basename, extname, relative and isAbsolute |
system | env(name), home(), platform(), tempDir(), lineEnd(), cwd() and memory() |
They take the place of Node's fs, path and os, which a terminal app cannot import. Each one answers at once, so you call it without await. A web page cannot use them, since a browser has no files.
Read the file in the loader#
The loader from 620 runs once, before the first frame, so it is where the list is read. It works out the file's path from the name, reads the list and hands both to the page:
;
;
;
;
;
file.read of a path with nothing there throws an error whose code is ENOENT, as Node's does. The catch turns that one error into the starter list and throws any other, so a file you cannot read is not silently replaced.
Save from the page#
The START list from 610 goes away. The page starts from the tasks the loader read and writes the list back on every change. Add save above the component:
;
;
;
;
;
file.makeDir without recursive fails when the directory is already there, as Node's mkdirSync does. With { recursive: true } it makes the parents and leaves an existing directory alone.
The rest of the component is the same as in 620.
The handler saves next, the list it just made. Saving tasks would write the list as it was before the key.
Run it#
The first time there is no ~/.tasks/tasks.json, so the list starts with the starter task:
╭──────────────────────────────────────────╮
│ Tasks tasks.json 1 open │
│ │
│ > Write the first task │
│ │
│ up and down move, space marks one done │
│ Ctrl-C quits │
╰──────────────────────────────────────────╯
Press space and quit. The directory and the file are there now:
Run it again and the task opens as done, with 0 open in the header. --file work.json keeps a second list in ~/.tasks/work.json.
Next up: 640. Type into the app.