-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
prepl.cljs
175 lines (164 loc) · 5.61 KB
/
prepl.cljs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
(ns portal.ui.viewer.prepl
(:require ["anser" :as anser]
[clojure.spec.alpha :as s]
[portal.colors :as c]
[portal.runtime.edn :as edn]
[portal.ui.icons :as icons]
[portal.ui.inspector :as ins]
[portal.ui.select :as select]
[portal.ui.state :as state]
[portal.ui.styled :as d]
[portal.ui.theme :as theme]))
;;; :spec
(s/def ::tag #{:out :err :tap :ret})
(s/def ::out
(s/keys :req-un [::tag ::val]))
(s/def ::prepl
(s/coll-of ::out :min-count 1))
;;;
(defn styles []
(let [theme (theme/use-theme)]
[:style
(d/map->css
{[:.ansi-black-fg] {:color (::c/border theme)}
[:.ansi-black-bg] {:background (::c/border theme)}
[:.ansi-red-fg] {:color (::c/exception theme)}
[:.ansi-red-bg] {:background (::c/exception theme)}
[:.ansi-green-fg] {:color (::c/string theme)}
[:.ansi-green-bg] {:background (::c/string theme)}
[:.ansi-yellow-fg] {:color (::c/tag theme)}
[:.ansi-yellow-bg] {:background (::c/tag theme)}
[:.ansi-blue-fg] {:color (::c/boolean theme)}
[:.ansi-blue-bg] {:background (::c/boolean theme)}
[:.ansi-magenta-fg] {:color (::c/number theme)}
[:.ansi-magenta-bg] {:background (::c/number theme)}
[:.ansi-cyan-fg] {:color (::c/package theme)}
[:.ansi-cyan-bg] {:background (::c/package theme)}
[:.ansi-white-fg] {:color (::c/text theme)}
[:.ansi-white-bg] {:background (::c/text theme)}
[:.ansi-bold] {:font-weight :bold}})]))
(defn- inspect-prepl-ret [value index]
(let [theme (theme/use-theme)]
[ins/with-collection
value
[d/div
(when-let [form (:form value)]
[ins/with-key
:form
[select/with-position
{:row (* 2 index) :column 0}
[ins/with-context
{:portal.viewer/code {:language "clojure"}}
[ins/inspector {:portal.viewer/default :portal.viewer/code} form]]]])
[d/div
{:style {:display :flex
:align-items :center}}
(when (= :ret (:tag value))
[icons/sign-out-alt
{:style
{:padding (:padding theme)
:color (if (:exception value)
(::c/exception theme)
(::c/border theme))}}])
[ins/with-key
:val
[select/with-position
{:row (inc (* 2 index)) :column 0}
[d/div
{:style {:width "100%"
:padding
[(:padding theme) 0]}}
[ins/inspector
(try
(edn/read-string (:val value))
(catch :default _ (:val value)))]]]]]]]))
(defn- title-bar-actions []
(let [theme (theme/use-theme)
state (state/use-state)
context (ins/use-context)
location (state/get-location context)]
[d/div
{:style
{:display :flex
:gap (:padding theme)
:box-sizing :border-box
:padding (* 1.6 (:padding theme))
:border-bottom [1 :solid (::c/border theme)]}}
[icons/times-circle
{:size :s
:style {:opacity 0.75
:color (::c/exception theme)}
:style/hover {:opacity 1}
:title "Click to do nothing. 😊"
:on-click (fn [e]
(.stopPropagation e))}]
[icons/minus-circle
{:size :s
:style {:opacity 0.75
:cursor :pointer
:color (::c/tag theme)}
:style/hover {:opacity 1}
:title "Click to collapse."
:on-click (fn [e]
(.stopPropagation e)
(state/dispatch! state assoc-in [:expanded? location] 0))}]
[icons/plus-circle
{:size :s
:style {:opacity 0.75
:cursor :pointer
:color (::c/string theme)}
:style/hover {:opacity 1}
:title "Click to expand."
:on-click (fn [e]
(.stopPropagation e)
(state/dispatch! state assoc-in [:expanded? location] 1))}]]))
(defn inspect-prepl [value]
(let [theme (theme/use-theme)
opts (ins/use-options)
bg (ins/get-background)]
[d/div
{:style
{:background bg
:border-radius (:border-radius theme)
:border [1 :solid (::c/border theme)]}}
[title-bar-actions]
[:pre
{:style
{:margin 0
:display :flex
:background bg
:max-height (when-not (:expanded? opts) "24rem")
:overflow :auto
:flex-direction :column-reverse
:white-space :pre-wrap
:box-sizing :border-box
:padding (:padding theme)
:font-size (:font-size theme)
:font-family (:font-family theme)}}
[ins/with-collection
value
(reverse
(map-indexed
(fn [index value]
(with-meta
(if (#{:tap :ret} (:tag value))
[ins/with-key
index
[inspect-prepl-ret value index]]
[d/span
{:style
{:color
(if (= (:tag value) :err)
(::c/exception theme)
(::c/text theme))}
:dangerouslySetInnerHTML
{:__html (anser/ansiToHtml (:val value) #js {:use_classes true})}}])
{:key index}))
value))]]]))
(defn io? [value]
(s/valid? ::prepl value))
(def viewer
{:predicate io?
:component inspect-prepl
:name :portal.viewer/prepl
:doc "View interlacing of stdout, stderr and tap values. Useful for build output."})