<commit_before>(ns planck.pprint.data
  "Pretty printing for data."
  (:refer-clojure :exclude [lift-ns])
  (:require
   [clojure.string :as string]
   [fipp.engine :refer [pprint-document]]
   [fipp.visit :refer [visit visit*]]
   [planck.themes]))

;; Derived from fipp.edn

(defn pretty-coll [{:keys [print-level print-length] :as printer}
                   open xs sep close f]
  (let [printer  (cond-> printer print-level (update :print-level dec))
        xform    (comp (if print-length (take print-length) identity)
                   (map #(f printer %))
                   (interpose sep))
        ys       (if (pos? (or print-level 1))
                   (sequence xform xs)
                   "#")
        ellipsis (when (and print-length (seq (drop print-length xs)))
                   [:span sep "..."])]
    [:group open [:align ys ellipsis] close]))


(defn wrap-theme
  [kw theme text]
  [:span [:pass (kw theme)] [:text text] [:pass (:reset-font theme)]])

(defn demunge-macros-symbol
  [sym]
  (let [sym-ns (namespace sym)]
    (if (and (some? sym-ns)
             (string/ends-with? sym-ns "$macros"))
      (symbol (subs sym-ns 0 (- (count sym-ns) 7)) (name sym))
      sym)))

(defn- lift-ns
  "Returns [lifted-ns lifted-map] or nil if m can't be lifted."
  [print-namespace-maps m]
  (when print-namespace-maps
    (loop [ns nil
           [[k v :as entry] & entries] (seq m)
           lm (empty m)]
      (if entry
        (when (or (keyword? k) (symbol? k))
          (if ns
            (when (= ns (namespace k))
              (recur ns entries (assoc lm (strip-ns k) v)))
            (when-let [new-ns (namespace k)]
              (recur new-ns entries (assoc lm (strip-ns k) v)))))
        [ns lm]))))

(defrecord PlanckPrinter [symbols print-meta print-length print-level print-namespace-maps theme keyword-ns demunge-macros-symbols?]

  fipp.visit/IVisitor

  (visit-unknown [this x]
    [:text (pr-str x)])


  (visit-nil [this]
    (wrap-theme :results-font theme "nil"))

  (visit-boolean [this x]
    (wrap-theme :results-font theme (str x)))

  (visit-string [this x]
    (wrap-theme :results-string-font theme (pr-str x)))

  (visit-character [this x]
    (wrap-theme :results-string-font theme (pr-str x)))

  (visit-symbol [this x]
    [:text (str (cond-> x demunge-macros-symbols? demunge-macros-symbol))])

  (visit-keyword [this x]
    (wrap-theme :results-keyword-font theme
      (if (and keyword-ns
               (= (namespace x) (str keyword-ns)))
        (str "::" (name x))
        (pr-str x))))
<commit_msg>Visit Eductions as sequences<commit_after>(ns planck.pprint.data
  "Pretty printing for data."
  (:refer-clojure :exclude [lift-ns])
  (:require
   [clojure.string :as string]
   [fipp.engine :refer [pprint-document]]
   [fipp.visit :refer [visit visit*]]
   [planck.themes]))

;; Derived from fipp.edn

(defn pretty-coll [{:keys [print-level print-length] :as printer}
                   open xs sep close f]
  (let [printer  (cond-> printer print-level (update :print-level dec))
        xform    (comp (if print-length (take print-length) identity)
                   (map #(f printer %))
                   (interpose sep))
        ys       (if (pos? (or print-level 1))
                   (sequence xform xs)
                   "#")
        ellipsis (when (and print-length (seq (drop print-length xs)))
                   [:span sep "..."])]
    [:group open [:align ys ellipsis] close]))


(defn wrap-theme
  [kw theme text]
  [:span [:pass (kw theme)] [:text text] [:pass (:reset-font theme)]])

(defn demunge-macros-symbol
  [sym]
  (let [sym-ns (namespace sym)]
    (if (and (some? sym-ns)
             (string/ends-with? sym-ns "$macros"))
      (symbol (subs sym-ns 0 (- (count sym-ns) 7)) (name sym))
      sym)))

(defn- lift-ns
  "Returns [lifted-ns lifted-map] or nil if m can't be lifted."
  [print-namespace-maps m]
  (when print-namespace-maps
    (loop [ns nil
           [[k v :as entry] & entries] (seq m)
           lm (empty m)]
      (if entry
        (when (or (keyword? k) (symbol? k))
          (if ns
            (when (= ns (namespace k))
              (recur ns entries (assoc lm (strip-ns k) v)))
            (when-let [new-ns (namespace k)]
              (recur new-ns entries (assoc lm (strip-ns k) v)))))
        [ns lm]))))

(defrecord PlanckPrinter [symbols print-meta print-length print-level print-namespace-maps theme keyword-ns demunge-macros-symbols?]

  fipp.visit/IVisitor

  (visit-unknown [this x]
    (if (instance? Eduction x)
      (visit this (sequence x))
      [:text (pr-str x)]))

  (visit-nil [this]
    (wrap-theme :results-font theme "nil"))

  (visit-boolean [this x]
    (wrap-theme :results-font theme (str x)))

  (visit-string [this x]
    (wrap-theme :results-string-font theme (pr-str x)))

  (visit-character [this x]
    (wrap-theme :results-string-font theme (pr-str x)))

  (visit-symbol [this x]
    [:text (str (cond-> x demunge-macros-symbols? demunge-macros-symbol))])

  (visit-keyword [this x]
    (wrap-theme :results-keyword-font theme
      (if (and keyword-ns
               (= (namespace x) (str keyword-ns)))
        (str "::" (name x))
        (pr-str x))))

{'fraction_non_alphanumeric': 0.15602154674874952, 'fraction_numerical': 0.0011542901115813775, 'mean_word_length': 2.2801261829653, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 7, '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': '5886403', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1748, 'n_words': 527}