Building a text editor in LÖVE

Show off your games, demos and other (playable) creations.
User avatar
Kartik Agaram
Prole
Posts: 14
Joined: Fri Apr 01, 2022 4:46 am

Building a text editor in LÖVE

Post 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.
User avatar
BrotSagtMist
Party member
Posts: 309
Joined: Fri Aug 06, 2021 10:30 pm

Re: Building a text editor in LÖVE

Post by BrotSagtMist »

You call the freaking save function whenever a button is pressed.
Are you actually trying to destroy your harddrive?
obey
User avatar
Kartik Agaram
Prole
Posts: 14
Joined: Fri Apr 01, 2022 4:46 am

Re: Building a text editor in LÖVE

Post by Kartik Agaram »

My editor autosaves whenever something changes in the file. Clicking on the button mutates the underlying file.
User avatar
GVovkiv
Party member
Posts: 430
Joined: Fri Jan 15, 2021 7:29 am
Contact:

Re: Building a text editor in LÖVE

Post 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
User avatar
pgimeno
Party member
Posts: 3288
Joined: Sun Oct 18, 2015 2:58 pm

Re: Building a text editor in LÖVE

Post 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?
User avatar
BrotSagtMist
Party member
Posts: 309
Joined: Fri Aug 06, 2021 10:30 pm

Re: Building a text editor in LÖVE

Post 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.
obey
User avatar
Kartik Agaram
Prole
Posts: 14
Joined: Fri Apr 01, 2022 4:46 am

Re: Building a text editor in LÖVE

Post 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.
User avatar
BrotSagtMist
Party member
Posts: 309
Joined: Fri Aug 06, 2021 10:30 pm

Re: Building a text editor in LÖVE

Post 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.
obey
User avatar
pgimeno
Party member
Posts: 3288
Joined: Sun Oct 18, 2015 2:58 pm

Re: Building a text editor in LÖVE

Post 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.
User avatar
Kartik Agaram
Prole
Posts: 14
Joined: Fri Apr 01, 2022 4:46 am

Re: Building a text editor in LÖVE

Post 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.)
Post Reply

Who is online

Users browsing this forum: No registered users and 28 guests