about summary refs log tree commit diff stats
path: root/life.tlv
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-02-02 23:44:25 -0800
committerKartik K. Agaram <vc@akkartik.com>2022-02-02 23:44:25 -0800
commita8dfea1d3c686437a29938a29b4b0543d8190267 (patch)
treef7c8019ba86b35f68af8ec8726e4f860f5acaa31 /life.tlv
parent8f8a0e5a181de799ce95c5d91c0266e0a06671d8 (diff)
downloadteliva-a8dfea1d3c686437a29938a29b4b0543d8190267.tar.gz
drop io.lines()
I'd already dropped the variant without a filename. But even the variant
with a filename is very easy to use in a way that is confusing to use in
the presence of sandboxing:

* call io.lines()
* Sandboxing saves an error, io.lines() returns nil
* Caller (usually a loop) raises an error.
* We show the error and not the sandboxing failure.
* Worse, there's no way to adjust permissions from within Teliva,
  because we can't ever get to that menu while there's an error.

Best solution I can come up with: encourage a separate step for
translating filename to file handle. That way it's more obvious that we
need to check for errors.
Diffstat (limited to 'life.tlv')
-rw-r--r--life.tlv17
1 files changed, 4 insertions, 13 deletions
diff --git a/life.tlv b/life.tlv
index ed95760..245981e 100644
--- a/life.tlv
+++ b/life.tlv
@@ -161,21 +161,12 @@
     >    end
     >end
 - __teliva_timestamp: original
-  file_exists:
-    >function file_exists(filename)
-    >  local f = io.open(filename, "r")
-    >  if f ~= nil then
-    >    io.close(f)
-    >    return true
-    >  else
-    >    return false
-    >  end
-    >end
-- __teliva_timestamp: original
   load_file:
     >function load_file(window, filename)
+    >  local infile = io.open(filename, 'r')
+    >  if infile == nil then return end
     >  local line_index = lines
-    >  for line in io.lines(filename) do
+    >  for line in infile:lines() do
     >    if line:sub(1,1) ~= '!' then  -- comment; plaintext files can't have whitespace before comments
     >      local col_index = cols
     >      for c in line:gmatch(".") do
@@ -286,7 +277,7 @@
     >    grid[8][5] = 1
     >    grid[7][4] = 1
     >    grid[6][3] = 1
-    >  elseif file_exists(arg[1]) then
+    >  else
     >    -- Load a file in the standard "plaintext" format: https://www.conwaylife.com/wiki/Plaintext
     >    --
     >    -- Each pattern page at https://www.conwaylife.com/wiki provides its