;;; Status
;; LGPL3+
;; AlexJGriffith 2026
;; Get the CPU used by the running love2d process

;;; Example
;; (local status (require :status))
;; (status.start)

;; (fn love.update [dt]
;;   (print (status.get :cpu))
;;   (print (status.get :mem)))

;; (fn love.keypressed [key]
;;   (case key
;;     :q (status.stop)))


(local fennel (require :lib.fennel))

;; tweak this command to get the cpu usage for your own love2d process (could use improvement)
(local command "ps -p $(ps aux | grep love | head -1 | awk '{print $2}') -o %%cpu,%%mem > %s/love2d-cpu")
;; returns cpu and mem in the following format
;; "%CPU %MEM
;;  8.2  0.7
;; "
(local parse-output "[^\n]+\n *([^%s]*) *([^\n]+)")
;; -> "8.2"  "0.7"
(local order [:cpu :mem] )
(local info {:cpu 0 :mem 0})

(fn get-cpu-status [ret]
  (love.filesystem.write ".blank" "") ;; creates save directory if it doesn't exist
  ;; we use os.execute and this work around writing to file because io.popen doesn't work
  ;; with fennel repl in love2d
  (os.execute (string.format command (love.filesystem.getSaveDirectory)))
  ;; os.execute is blocking so we should be able to skip the sleep step
  ;; (love.timer.sleep 0.2)
  (let [input (love.filesystem.read "love2d-cpu")]
    (let [parsed [((string.gmatch (love.filesystem.read "love2d-cpu") parse-output))]]
      (each [index key (ipairs order)]
        (tset ret key  (or (tonumber (. parsed index)) 0))
        )))
    ret)

(fn looper [tick-time in out]
  ;; iterate every tick-time if we don't recieve a command
  ;; to exit
  (case (in:demand tick-time)    
    :exit :nothing
    _ (do  (out:push (get-cpu-status info))
           (looper tick-time in out))))

;; when this is called as a thread ... will be :status plus the in and out channels
(case ...
  (:status tick-time in out) (looper tick-time in out))

;; cache of values between ticks
(var previous-value info)
(local state {:running false})

;; on require we can pass the above case and run the following
{:start (let [luac 
              (love.filesystem.newFileData
               (fennel.compileString (love.filesystem.read "status.fnl") {}) "status")]
          (fn [tick-time?]
            ;; right now this only works on linux, I need to test it on mac windows and
            ;; web and look into sensible default commands.
            (when (= :Linux (love.system.getOS))
              (local thread (love.thread.newThread luac))
              (set state.running true)
              (set state.o-channel (love.thread.newChannel))
              (set state.i-channel (love.thread.newChannel))
              (thread:start :status (or tick-time? 1) state.i-channel state.o-channel)
              (state.i-channel:push :run))))
 
 :stop (fn [] (when state.running
                (set state.running false)
                (state.i-channel:push :exit)
                (set previous-value info)))
 :get (fn [key?]
        (when state.running
          (let [ret (or (state.o-channel:pop) previous-value)]
            (set previous-value ret)))
        (if key? (. previous-value key?) previous-value))}

Generated by alexjgriffith using scpaste at Fri Oct 23 17:43:16 2026. EDT. (original)