Skip to content

Instantly share code, notes, and snippets.

@piecyk
Last active October 13, 2015 14:08
Show Gist options
  • Select an option

  • Save piecyk/29b29c2c4f3184eda83f to your computer and use it in GitHub Desktop.

Select an option

Save piecyk/29b29c2c4f3184eda83f to your computer and use it in GitHub Desktop.
Example of jqconsole in clojurescript reagent core-async
(ns cljsplayground.core
(:require [reagent.core :as reagent :refer [atom]]
[cljs.core.async :refer [put! chan <! >! timeout close!]])
(:require-macros [cljs.core.async.macros :refer [go go-loop]]))
;; jquery and jqconsole are included into index.html with <script> tag for this example
(defn jqconsole-prompt [jqconsole chan]
(.Prompt jqconsole true #(put! chan %) (fn [str] (.log js/console str) false)))
(defn jqconsole-write [jqconsole args]
(.Write jqconsole (clojure.string/join "" args)))
(defn jqconsole-eval-write
"validate and write to jqconsole result"
[jqconsole str]
(try
(jqconsole-write jqconsole ["==> " (.eval js/window str) "\n"])
(catch js/Error err
(jqconsole-write jqconsole [err "\n"]))))
(defn loop-prompt [jqconsole chan]
(go-loop []
(let [str (<! chan)]
(jqconsole-eval-write jqconsole str)
(jqconsole-prompt jqconsole chan)
(recur))))
(defn jqconsole-init
"init jqconsole object set all options also all actions that we want"
[node chan options]
(let [jquery (js* "$")
jqconsole (-> (jquery node)
(.jqconsole "Welcome to JQConsole!\n" "JS> "))]
(.RegisterMatching jqconsole "{" "}" "brace")
(.RegisterMatching jqconsole "(" ")" "paren")
(.RegisterMatching jqconsole "[" "]" "bracket")
;; Abort prompt on Ctrl+C.
(.RegisterShortcut jqconsole "C"
(fn []
(.AbortPrompt jqconsole)
(jqconsole-write jqconsole ["Cancel with Ctrl+C" "\n"])
(jqconsole-prompt jqconsole chan)))
jqconsole))
(defn jqconsole-did-mount [this]
(let [command-chan (chan)
jqconsole (jqconsole-init (reagent/dom-node this) command-chan nil)]
(jqconsole-prompt jqconsole command-chan)
(loop-prompt jqconsole command-chan)))
(defn jqconsole-render []
[:div {:class-name "console-wrapper"}])
(defn jqconsole-component []
(reagent/create-class {:reagent-render jqconsole-render
:component-did-mount jqconsole-did-mount}))
(defn repl-page []
[:div [:h2 "Welcome to cljsplayground"]
[:div [jqconsole-component]]])
(defn mount-root []
(reagent/render [repl-page] (.getElementById js/document "app")))
(defn init! []
(mount-root))
@piecyk

piecyk commented Oct 13, 2015

Copy link
Copy Markdown
Author

example:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment