Page 1 of 2
Building a text editor in LÖVE
Posted: Fri Jun 17, 2022 1:40 am
by Kartik Agaram
I've always wanted an editor for plain text that would let me seamlessly insert line drawings. I finally got around to this desire after discovering LÖVE.
http://akkartik.name/lines.html
I've had only good experiences so far with Lua and LÖVE. They let me build apps with just Lua that is easy for anyone to take apart and make changes to, all while being fast enough for simple applications like this that I don't need to bump down to a compiled language or think about GPUs. Where things felt sluggish I've been able to locate memory leaks and reduce the load on the garbage collector.
Re: Building a text editor in LÖVE
Posted: Fri Jun 17, 2022 3:52 am
by BrotSagtMist
You call the freaking save function whenever a button is pressed.
Are you actually trying to destroy your harddrive?
Re: Building a text editor in LÖVE
Posted: Fri Jun 17, 2022 4:09 am
by Kartik Agaram
My editor autosaves whenever something changes in the file. Clicking on the button mutates the underlying file.
Re: Building a text editor in LÖVE
Posted: Fri Jun 17, 2022 7:18 am
by GVovkiv
Kartik Agaram wrote: ↑Fri Jun 17, 2022 4:09 am
My editor autosaves whenever something changes in the file. Clicking on the button mutates the underlying file.
It's nice, but it's better to set to timer, that user can change...
Like, saving every 5 seconds or so...
I will especially sucks to use save for every button press on ssd, it will kill it pretty quickly
Re: Building a text editor in LÖVE
Posted: Fri Jun 17, 2022 10:56 am
by pgimeno
The system will cache those saves unless calling fsync() after each write. I don't think that Löve does an fsync() at every write, or does it?
Re: Building a text editor in LÖVE
Posted: Fri Jun 17, 2022 11:23 am
by BrotSagtMist
Well its a full file open/close circle. Löve by default buffers, yea, but since it closes the file this is left to the OS id say.
But so far i know even if the write is postponed the opening should trigger a sync.
And since my drives access LED blinks on every keystroke i can confirm it does sync for me.
Re: Building a text editor in LÖVE
Posted: Fri Jun 17, 2022 1:12 pm
by Kartik Agaram
https://superuser.com/questions/1117900 ... hard-drive
Thanks for raising the issue. SSD lifetime wasn't on my radar at all. I'm still thinking about the best way to deal with it.
Re: Building a text editor in LÖVE
Posted: Fri Jun 17, 2022 5:44 pm
by BrotSagtMist
Without the reopening and closing it would indeed be cached, you just have to rewind instead.
Also simply add a timer condition, add a timestamp and skip the save if the last is say less than a minute ago.
Re: Building a text editor in LÖVE
Posted: Fri Jun 17, 2022 5:58 pm
by pgimeno
Well, in Linux it's well known that you can open a temporary file, write to it, read from it, close it and delete it, all without a single access to disk. I guess it depends on the cache timeout; see
https://unix.stackexchange.com/question ... em-caching for example for how to configure said timeout on Linux.
I'd suggest using two timers. The idea is to use a retriggerable timer to perform a save some time after the last keypress, but just in case you're typing constantly without giving that timer a rest, have a second timer that saves anyway every few tens of second or every minute or so, regardless of whether you keep typing. Both timers should be reset after a save, no matter which one is the source of the save, and neither should run if there's no change to the file.
Re: Building a text editor in LÖVE
Posted: Fri Jun 17, 2022 10:55 pm
by Kartik Agaram
Thanks everyone, I reduced the write frequency. I ended up going with a single timer. Now all the places where I was writing before simply set a dirty bit to schedule a save. Then I periodically save inside update based on the timestamp of the dirty bit. This seems equivalent to all your suggestions? Please tell me if you see some problem with this logic.
(I also updated all my tests. This kind of delay always makes me a little nervous.)