<commit_before>(ns lt.objs.console
  (:require [lt.object :as object]
            [lt.objs.app :as app]
            [lt.objs.files :as files]
            [lt.objs.bottombar :as bottombar]
            [lt.objs.command :as cmd]
            [lt.objs.status-bar :as status-bar]
            [lt.objs.tabs :as tabs]
            [clojure.string :as string]
            [lt.util.dom :refer [$ append empty parents] :as dom]
            [lt.objs.platform :as platform])
  (:require-macros [crate.def-macros :refer [defpartial]]
                   [lt.macros :refer [behavior defui]]))

(def console-limit 50)
(def util-inspect (.-inspect (js/require "util")))
(def logs-dir (files/lt-user-dir "logs"))
(def core-log (try
                (when-not (files/exists? logs-dir)
                  (files/mkdir logs-dir))
                (.. (js/require "fs") (createWriteStream (files/join logs-dir (str "window" (app/window-number) ".log"))))
                (catch js/global.Error e
                  (.error js/console (str "Failed to initialize the log writer: " e)))
                (catch js/Error e
                  (.error js/console (str "Failed to initialize the log writer: " e)))))

(defn ->ui [c]
  (object/->content c))

(defn dom-like? [thing]
  (or (vector? thing)
      (.-nodeType thing)
      (string? thing)))

(declare console)

(defn write [$console msg]
  (when (> (count (dom/children $console)) (dec console-limit))
    (dom/remove (aget (dom/children $console) 0)))
  (when-not (bottombar/active? console)
    (status-bar/dirty))
  (append $console msg))

(defn write-to-log [thing]
  (when core-log
    (.write core-log thing)))

(defpartial ->item [l & [class]]
  [:li {:class class} l])

(defn log
  ([l class] (log l class nil))
  ([l class str-content]
   (when-not (= "" l)
     (let [$console (->ui console)]
       (when (or (string? l) str-content) (write-to-log (if (string? l)
                                                          l
                                                          str-content))
         (write $console (->item [:pre (if-not (dom-like? l)
                                         (pr-str l)
                                         l)] class))
         (dom/scroll-top $console 10000000000)
         nil)))))

(defn error [e]
  (status-bar/console-class "error")
  (log (str (if (.-stack e)
              (.-stack e)
              (let [pr-e (pr-str e)]
                (if (not= pr-e "[object Object]")
                  pr-e
                  (str e)))))
       "error"))

(.on js/process "uncaughtException" #(error %))<commit_msg>fix display of error reporting of strings<commit_after>(ns lt.objs.console
  (:require [lt.object :as object]
            [lt.objs.app :as app]
            [lt.objs.files :as files]
            [lt.objs.bottombar :as bottombar]
            [lt.objs.command :as cmd]
            [lt.objs.status-bar :as status-bar]
            [lt.objs.tabs :as tabs]
            [clojure.string :as string]
            [lt.util.dom :refer [$ append empty parents] :as dom]
            [lt.objs.platform :as platform])
  (:require-macros [crate.def-macros :refer [defpartial]]
                   [lt.macros :refer [behavior defui]]))

(def console-limit 50)
(def util-inspect (.-inspect (js/require "util")))
(def logs-dir (files/lt-user-dir "logs"))
(def core-log (try
                (when-not (files/exists? logs-dir)
                  (files/mkdir logs-dir))
                (.. (js/require "fs") (createWriteStream (files/join logs-dir (str "window" (app/window-number) ".log"))))
                (catch js/global.Error e
                  (.error js/console (str "Failed to initialize the log writer: " e)))
                (catch js/Error e
                  (.error js/console (str "Failed to initialize the log writer: " e)))))

(defn ->ui [c]
  (object/->content c))

(defn dom-like? [thing]
  (or (vector? thing)
      (.-nodeType thing)
      (string? thing)))

(declare console)

(defn write [$console msg]
  (when (> (count (dom/children $console)) (dec console-limit))
    (dom/remove (aget (dom/children $console) 0)))
  (when-not (bottombar/active? console)
    (status-bar/dirty))
  (append $console msg))

(defn write-to-log [thing]
  (when core-log
    (.write core-log thing)))

(defpartial ->item [l & [class]]
  [:li {:class class} l])

(defn log
  ([l class] (log l class nil))
  ([l class str-content]
   (when-not (= "" l)
     (let [$console (->ui console)]
       (when (or (string? l) str-content) (write-to-log (if (string? l)
                                                          l
                                                          str-content))
         (write $console (->item [:pre (if-not (dom-like? l)
                                         (pr-str l)
                                         l)] class))
         (dom/scroll-top $console 10000000000)
         nil)))))

(defn error [e]
  (status-bar/console-class "error")
  (log (str (cond
             (.-stack e) (.-stack e)
             (string? e) e
             (not= (pr-str e) "[object Object]") (pr-str e)
             :else (str e)))
       "error"))

(.on js/process "uncaughtException" #(error %))
{'fraction_non_alphanumeric': 0.15640084958486195, 'fraction_numerical': 0.0054064491214520175, 'mean_word_length': 1.7035490605427974, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3274564', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1609, 'n_words': 461}