<commit_before>(ns net.sekao.nightcode.state
  (:require [clojure.edn :as edn])
  (:import [java.util.prefs Preferences]))

; preferences

(def ^Preferences prefs (.node (Preferences/userRoot) "nightcode"))

(defn write-pref!
  "Writes a key-value pair to the preference file."
  [k v]
  (doto prefs
    (.put (name k) (pr-str v))
    .flush))

(defn remove-pref!
  "Removes a key-value pair from the preference file."
  [k]
  (doto prefs
    (.remove (name k))
    .flush))

(defn read-pref
  "Reads value from the given key in the preference file."
  [k & [default-val]]
  (if-let [string (.get prefs (name k) nil)]
    (edn/read-string string)
    default-val))

; state

(defonce state (atom {:project-set (read-pref :project-set)
                      :expansion-set (read-pref :expansion-set)
                      :selection (read-pref :selection)}))

(add-watch state :state-changed
  (fn [_ _ old-state new-state]
    (let [old-projects (:project-set old-state)
          new-projects (:project-set new-state)
          old-expansions (:expansion-set old-state)
          new-expansions (:expansion-set new-state)
          old-selection (:selection old-state)
          new-selection (:selection new-state)]
      (when (not= old-projects new-projects)
        (write-pref! :project-set new-projects))
      (when (not= old-expansions new-expansions)
        (write-pref! :expansion-set new-expansions))
      (when (not= old-selection new-selection)
        (write-pref! :selection new-selection)))))<commit_msg>Set default vals for prefs<commit_after>(ns net.sekao.nightcode.state
  (:require [clojure.edn :as edn])
  (:import [java.util.prefs Preferences]))

; preferences

(def ^Preferences prefs (.node (Preferences/userRoot) "nightcode"))

(defn write-pref!
  "Writes a key-value pair to the preference file."
  [k v]
  (doto prefs
    (.put (name k) (pr-str v))
    .flush))

(defn remove-pref!
  "Removes a key-value pair from the preference file."
  [k]
  (doto prefs
    (.remove (name k))
    .flush))

(defn read-pref
  "Reads value from the given key in the preference file."
  [k & [default-val]]
  (if-let [string (.get prefs (name k) nil)]
    (edn/read-string string)
    default-val))

; state

(defonce state (atom {:project-set (read-pref :project-set #{})
                      :expansion-set (read-pref :expansion-set #{})
                      :selection (read-pref :selection)}))

(add-watch state :state-changed
  (fn [_ _ old-state new-state]
    (let [old-projects (:project-set old-state)
          new-projects (:project-set new-state)
          old-expansions (:expansion-set old-state)
          new-expansions (:expansion-set new-state)
          old-selection (:selection old-state)
          new-selection (:selection new-state)]
      (when (not= old-projects new-projects)
        (write-pref! :project-set new-projects))
      (when (not= old-expansions new-expansions)
        (write-pref! :expansion-set new-expansions))
      (when (not= old-selection new-selection)
        (write-pref! :selection new-selection)))))
{'fraction_non_alphanumeric': 0.14281056406912293, 'fraction_numerical': 0.0, 'mean_word_length': 2.7644171779141105, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3608704', 'n_tokens_mistral': 1041, 'n_tokens_neox': 1008, 'n_words': 275}