-
Notifications
You must be signed in to change notification settings - Fork 19
/
util.cljc
144 lines (119 loc) · 4 KB
/
util.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
(ns debux.cs.util
"Utilities for clojurescript only"
(:require [clojure.string :as str]
[clojure.set :as set]
#?(:cljs [cljs.pprint :as pp])
[cljs.analyzer.api :as ana]
[debux.common.util :as ut] ))
;;; caching
(def ^:private prev-returns* (atom {}))
(defn changed?
"Checks if prev-returns* contains <form>.
<form str> the key of prev-returns* map
<return str> the value of prev-returns* map"
[form return]
;; init
(when-not (contains? @prev-returns* form)
(swap! prev-returns* assoc form ""))
;; update
(and (not= return (get @prev-returns* form))
(swap! prev-returns* assoc form return) ))
;;; styling
(def style*
(atom {:error "background: red; color: white"
:warn "background: green; color: white"
:info "background: #0000cd; color: white"
:debug "background: #ffc125; color: black"
:text "color: black"
:title "color: #8b008b"} ))
(defn merge-styles
"Merges <new-style> into style*.
<new-style {<style-name kw, style-value str>+}>"
[new-style]
(swap! style* merge new-style))
(defn- get-style
"<styke kw|str> style-name
<return str?>"
[style]
(cond
(keyword? style)
(cond
(#{:error :e} style)
(:error @style*)
(#{:warn :w} style)
(:warn @style*)
(#{:info :i} style)
(:info @style*)
(#{:debug :d} style)
(:debug @style*)
:else
(get @style* style))
(string? style)
style))
;;; printing for browser console
(defn form-header [form & [msg]]
(str "%c " (ut/truncate (pr-str form))
" %c" (and msg (str " <" msg ">"))
" =>"))
#?(:cljs
(defn cgroup
[header form-style]
(.group js/console header (:title @style*)
(get-style form-style) (:text @style*) )))
#?(:cljs
(defn clog-header
[header form-style]
(.log js/console header (:title @style*)
(get-style form-style) (:text @style*) )))
#?(:cljs
(defn cgroup-end
[]
(.groupEnd js/console) ))
#?(:cljs
(defn clog-form-with-indent
[form style indent-level]
(.log js/console (ut/prepend-bars form indent-level)
(get-style style) (:text @style*) )))
#?(:cljs
(defn clog-result-with-indent
[result indent-level & [js-mode]]
(let [pprint (str/trim (with-out-str (pp/pprint result)))
pprint' (if (fn? result)
(str (first (str/split pprint #" " 2)) "]")
pprint)]
(.log js/console (->> (str/split pprint' #"\n")
ut/prepend-blanks
(mapv #(ut/prepend-bars % indent-level))
(str/join "\n") ))
(when js-mode
(.log js/console "%O" result) ))))
;;; spy functions
#?(:cljs
(def spy-first
(fn [result quoted-form {:keys [n msg style js] :as opts}]
(clog-form-with-indent (form-header quoted-form msg)
(or style :debug)
(:indent-level @ut/config*))
(clog-result-with-indent (ut/take-n-if-seq n result)
(:indent-level @ut/config*) js)
result) ))
#?(:cljs
(def spy-last
(fn [quoted-form {:keys [n msg style js] :as opts} result]
(clog-form-with-indent (form-header quoted-form msg)
(or style :debug)
(:indent-level @ut/config*))
(clog-result-with-indent (ut/take-n-if-seq n result)
(:indent-level @ut/config*) js)
result) ))
#?(:cljs
(defn spy-comp
[quoted-form form {:keys [n msg style js] :as opts}]
(fn [& arg]
(let [result (apply form arg)]
(clog-form-with-indent (form-header quoted-form msg)
(or style :debug)
(:indent-level @ut/config*))
(clog-result-with-indent (ut/take-n-if-seq n result)
(:indent-level @ut/config*) js)
result) )))