about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-02-10 07:35:54 -0800
committerKartik K. Agaram <vc@akkartik.com>2022-02-10 07:35:54 -0800
commit1a40bc7f5f3517e0fb17ffdea33fd8f5529195b9 (patch)
treebcca00b396a157d26da75632cfa73ce916f5fe60
parent10dab7ae175b2bbea07ed6c7f11c1aafe3f3ec21 (diff)
downloadteliva-1a40bc7f5f3517e0fb17ffdea33fd8f5529195b9.tar.gz
zet.tlv: saving/loading to/from disk
-rw-r--r--zet.tlv76
1 files changed, 76 insertions, 0 deletions
diff --git a/zet.tlv b/zet.tlv
index ebff583..2e42402 100644
--- a/zet.tlv
+++ b/zet.tlv
@@ -1342,3 +1342,79 @@
     >    end
     >  end
     >end
+- __teliva_timestamp:
+    >Thu Feb 10 07:27:43 2022
+  write_zettels:
+    >function write_zettels(outfile)
+    >  outfile:write(json.encode(zettels))
+    >  outfile:close()
+    >end
+- __teliva_timestamp:
+    >Thu Feb 10 07:28:30 2022
+  read_zettels:
+    >function read_zettels(infile)
+    >  zettels = json.decode(infile:read('*a'))
+    >  infile:close()
+    >end
+- __teliva_timestamp:
+    >Thu Feb 10 07:30:25 2022
+  __teliva_note:
+    >saving/loading zettels to/from disk
+  main:
+    >function main()
+    >  init_colors()
+    >  curses.curs_set(0)  -- hide cursor except when editing
+    >
+    >  -- read zettels from disk if possible
+    >  local infile = io.open('zet', 'r')
+    >  if infile then
+    >    read_zettels(infile)
+    >  else
+    >    local outfile = io.open('zet', 'w')
+    >    if outfile then
+    >      write_zettels(outfile)
+    >    end
+    >  end
+    >  current_zettel_id = zettels.root
+    >
+    >  while true do
+    >    render(window)
+    >    update(window)
+    >
+    >    -- save zettels, but hold on to previous state on disk
+    >    -- until last possible second
+    >    local filename = os.tmpname()
+    >    local outfile = io.open(filename, 'w')
+    >    write_zettels(outfile)
+    >    os.rename(filename, 'zet')
+    >  end
+    >end
+- __teliva_timestamp:
+    >Thu Feb 10 07:32:46 2022
+  __teliva_note:
+    >stop writing sample zettels to disk
+    >
+    >That was just for ease of testing write_zettels()
+  main:
+    >function main()
+    >  init_colors()
+    >  curses.curs_set(0)  -- hide cursor except when editing
+    >
+    >  local infile = io.open('zet', 'r')
+    >  if infile then
+    >    read_zettels(infile)
+    >  end
+    >  current_zettel_id = zettels.root
+    >
+    >  while true do
+    >    render(window)
+    >    update(window)
+    >
+    >    -- save zettels, but hold on to previous state on disk
+    >    -- until last possible second
+    >    local filename = os.tmpname()
+    >    local outfile = io.open(filename, 'w')
+    >    write_zettels(outfile)
+    >    os.rename(filename, 'zet')
+    >  end
+    >end