bugfix: open empty files

Why this matters:
- Right now there tends to be no way (in this app and for most of my
  LÖVE apps) to open a new file.
- The obvious workaround without access to a terminal is to create a new
  file and drop it on the app window.

So it's important for my apps, when they edit a single file, to handle
an empty input file.

It's also important to remember that json.decode will raise an error on
an empty string. I don't think my other apps (except forks of snap.love)
have an issue with this. They tend to open existing files, create files
before opening them (mastodon-unfurl) or not have purely JSON-based file
formats.
This commit is contained in:
Kartik K. Agaram 2023-07-10 12:52:49 -07:00
parent 2f5c8bf3d8
commit 913c59322a
3 changed files with 8 additions and 9 deletions

View File

@ -6,8 +6,6 @@ on.initialize = function(arg)
if Settings[Filename] then
Viewport = Settings[Filename].viewport
end
if file_exists(Filename) then
load_graph_from_disk()
end
load_graph_from_disk()
A()
end
end

View File

@ -1,7 +1,10 @@
load_graph_from_disk = function()
local f = App.open_for_reading(Filename)
local data = json.decode(f:read('*a'))
if f == nil then return end
local contents = f:read('*a')
f:close()
if contents == '' then return end
local data = json.decode(contents)
Nodes = data.nodes or {}
First_available_id = data.next
end

View File

@ -5,8 +5,6 @@ on.file_drop = function(file)
end
Filename = file:getFilename()
love.window.setTitle('snap.love - '..Filename)
if file_exists(Filename) then
load_graph_from_disk()
end
load_graph_from_disk()
A()
end
end