├── .circleci └── config.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── doc └── intro.md ├── notes.md ├── project.clj ├── resources ├── fully-interactive.gif └── semi-interactive.gif ├── src └── datawalk │ ├── core.cljc │ ├── datawalk.cljc │ ├── parse.cljc │ ├── print.cljc │ ├── todo.org │ └── util.cljc └── test └── datawalk └── core_test.cljc /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # Clojure CircleCI 2.0 configuration file 2 | # 3 | # Check https://circleci.com/docs/2.0/language-clojure/ for more details 4 | # 5 | version: 2 6 | jobs: 7 | build: 8 | docker: 9 | # specify the version you desire here 10 | - image: circleci/clojure:lein-2.7.1 11 | 12 | # Specify service dependencies here if necessary 13 | # CircleCI maintains a library of pre-built images 14 | # documented at https://circleci.com/docs/2.0/circleci-images/ 15 | # - image: circleci/postgres:9.4 16 | 17 | working_directory: ~/repo 18 | 19 | environment: 20 | LEIN_ROOT: "true" 21 | # Customize the JVM maximum heap limit 22 | JVM_OPTS: -Xmx3200m 23 | 24 | steps: 25 | - checkout 26 | 27 | # Download and cache dependencies 28 | - restore_cache: 29 | keys: 30 | - v1-dependencies-{{ checksum "project.clj" }} 31 | # fallback to using the latest cache if no exact match is found 32 | - v1-dependencies- 33 | 34 | - run: lein deps 35 | 36 | - save_cache: 37 | paths: 38 | - ~/.m2 39 | key: v1-dependencies-{{ checksum "project.clj" }} 40 | 41 | # run tests, build 42 | - run: lein do test, uberjar 43 | 44 | # Uberjar is probably fairly unnecessary, but in the interest of 45 | # exercising more of CircleCI... 46 | - store_artifacts: 47 | path: target/uberjar/cci-demo-clojure.jar 48 | destination: uberjar 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | .cljs_rhino_repl/ 13 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [0.1.2] - 2017-11-10 4 | 5 | - Bugfix: ClojureScript doesn't have blocking derefables 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor to control, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # datawalk 2 | 3 | https://github.com/eggsyntax/datawalk 4 | 5 | Provides a simple interactive data explorer. 6 | 7 | ``` 8 | (datawalk.core/repl my-data-structure) 9 | ``` 10 | 11 | In Clojure and ClojureScript, we spend a lot of time exploring and manipulating 12 | data structures, sometimes enormous ones. I've whiled away innumerable hours at 13 | the REPL navigating through complex nested data structures, often heavily-nested 14 | maps with long namespaced keys. I prefer to minimize the amount of typing I have 15 | to do for repetitive tasks, and this is certainly one of those tasks. 16 | 17 | So datawalk pretty-prints the top level of your data structure, and then lets 18 | you drill down repeatedly, often with just a single keystroke (the number 19 | representing the item's position in the printed list, as shown next to the 20 | item) plus enter. 21 | 22 | Often the reason for drilling down at the REPL is to determine a path to pass to 23 | `get-in`, so datawalk will track and [p]rint the path from the original root to 24 | the level you're on (as long as it's a viable get-in path, which it's not in the 25 | case of sets and lists). 26 | 27 | At any point in this process, you can [s]ave the current item or sa[v]e the 28 | path (from root to item) into a map of saved items which will be returned when 29 | you [q]uit. You can also e[x]it and return just the current item. 30 | 31 | As mentioned, you can [p]rint the path to current data. You can also print 32 | [c]urrent data (useful when you want to see it untruncated), print the [m]ap 33 | of saved data, or print [h]elp to get a reminder of these commands. Capital 34 | C and M give you pretty-printed versions of current and saved data. 35 | 36 | You can also move [b]ackward or [f]orward in time, or move [u]p a level, or jump 37 | back to the original [r]oot, or use [!] (function) to call an arbitrary 38 | single-arg │ function on the current data (jumping to the result). 39 | 40 | datawalk tries to do one thing well: eliminate tedium and typing when navigating 41 | data structures. The learning curve should be trivial; please let me know if it's 42 | not and I'll try to change that. I hope you find it as useful as I have. 43 | 44 | ## Demo (fully-interactive mode): 45 | 46 | 47 | ![Demo](resources/fully-interactive.gif?raw=true "Demo") 48 | 49 | 50 | ## Installation: 51 | 52 | Leiningen: 53 | ``` 54 | [datawalk "0.1.12"] 55 | ``` 56 | 57 | Details at https://clojars.org/datawalk 58 | 59 | ## Usage summary: 60 | 61 | ``` 62 | Clojure: 63 | (datawalk.core/repl my-data-structure) to start datawalking. 64 | Clojure and ClojureScript: 65 | (look-at my-data-structure), and then (w [command]) 66 | 67 | Commands: 68 | numbers: Enter any number to jump to the corresponding item 69 | q :quit ; exit and return saved values if any 70 | x :exit-with-current ; exit & return just this value 71 | s :save-current ; save to map of return values 72 | v :save-path ; save path to map of return values 73 | b :backward ; step backward in history 74 | f :forward ; step forward in history 75 | r :root ; jump back to original root 76 | ! :function ; call a 1-arg fn on data, jump to result (clj only) 77 | u :up ; step upward [provides list of referring entities] 78 | h :help ; print help & return same ent 79 | p :print-path ; print path from root to current item. 80 | m :print-saved-map ; print the map of saved data 81 | c :print-full-cur ; print the current data in full, without truncation 82 | ``` 83 | 84 | ## Fully-interactive version (Clojure-only) 85 | 86 | Start the fully-interactive version with `(datawalk.core/repl my-data-structure)`. 87 | From there, you just enter a single character followed by enter -- a number 88 | to drill down to a specific item in the displayed list, `b` to step back to 89 | where you just were, `h` for help, and so on. It should be extremely self- 90 | explanatory. 91 | 92 | ## Semi-interactive version (Clojure and ClojureScript) 93 | 94 | To use the semi-interactive version, let's assume you've referred the two 95 | key functions: 96 | 97 | `(require '[datawalk.core :as dw :refer [look-at w]])` 98 | 99 | Start by calling 100 | 101 | `user> (look-at my-data-structure)` 102 | 103 | This initializes datawalk's internal state, so that it's always tracking a 104 | piece of current data. From there, call `(w *)`, where `*` represents any of 105 | the commands listed in the usage summary. So for example: 106 | ``` 107 | user> (w 2) ; drill down to item 2 in the numbered sequence 108 | user> (w b) ; step backward in time 109 | user> (w s) ; save the current data to the map of saved values 110 | user> (w h) ; view the help (command summary) 111 | ``` 112 | 113 | After each step, the current state will be stored in datawalk atoms and can 114 | be freely accessed: 115 | 116 | `datawalk.datawalk/data` stores the current data. 117 | 118 | `datawalk.datawalk/saved` holds the map of stored values. 119 | 120 | In the same ns, you can also access `the-root`, `the-past`, and `the-future`. 121 | 122 | Additionally, datawalk's built-in printing tools are available: 123 | 124 | ``` 125 | user> (w m) prints the map of saved values. 126 | user> (w p) prints the path from the root to the current data. 127 | ``` 128 | 129 | ## Demo (semi-interactive mode): 130 | 131 | 132 | ![Demo](resources/semi-interactive.gif?raw=true "Demo") 133 | 134 | 135 | ## Why two versions? 136 | 137 | I developed the semi-interactive version so I could use datawalk in 138 | ClojureScript. ClojureScript presents a challenge: while you can trivially get 139 | user input in Clojure with `read-line`, ClojureScript has no consistent way to 140 | do so (although some specific cljs repls like planck add that functionality 141 | themselves). Because of this issue, it's very difficult to create a true 142 | interactive program as described above. 143 | 144 | I've gotten some interesting tips about it, and @mfikes kindly pointed to 145 | his abio library (https://github.com/abiocljs) which begins to address the 146 | problem. I'm hoping that at some point I'll manage to put together a truly 147 | agnostic solution (PRs welcome!). 148 | 149 | But now that I've created the semi-interactive version as an interim solution, 150 | it turns out it has some real advantages. Some people may actually prefer it, 151 | since it keeps you in an ordinary REPL at all times. Its only downside is that 152 | you have to type a few more characters. 153 | 154 | ``` 155 | Fully-interactive: | Semi-interactive: 156 | | 157 | PROS: - fewer keystrokes | - Runs anywhere 158 | - extremely fast | - Easy to do other stuff in the middle of a session 159 | | - You're always in a normal REPL 160 | | 161 | CONS: - Clojure-only | - Requires more keystrokes 162 | - Can't do other stuff in the | - Not quite as fast to explore data 163 | middle of a session | 164 | ``` 165 | 166 | ## Configuration 167 | 168 | `datawalk.print` contains a `config` atom holding several entries which you can 169 | alter to change the behavior of datawalk. These configs all affect the 170 | representation of the current data structure, not the underlying behavior. 171 | 172 | * `:max-items` the top level of your data structure may be a sequence with 173 | hundreds or thousands of items; you probably don't want to print them all. 174 | Defaults to 30. 175 | 176 | * `:max-line-length` on each printed line, datawalk attempts to represent the 177 | entire contents of the data on that line. This can sometimes be enormously 178 | long. You may wish to tune it to match the width of your repl environment. 179 | 180 | * `:max-key-length` when displaying maps, the keys may be so long that they take 181 | up most of the space defined by \*max-line-length\*. This is most often true 182 | with namespaced keys. \*max-key-length\* limits the space taken up by the 183 | keys, in order to be sure to leave room for the values. When keys must be 184 | chopped, they're chopped from the right to try to capture the name rather than 185 | the namespace. 186 | 187 | * `:debug-mode` when this is truthy, datawalk will print the values of the current 188 | path, saved values, the-past, and the-future at each step. 189 | 190 | `datawalk.core` also has a convenience function, `set-line-length`, which can be 191 | passed a single number. `max-line-length` will be set to this value, and 192 | `max-key-length` will be set to 20% of the value. 193 | 194 | ## Note for Emacs users 195 | 196 | Emacs accepts user input (via `read-line`) in the minibuffer -- this can be 197 | confusing when you first encounter it; the REPL buffer looks like it's hung. 198 | Just look down at the minibuffer and you'll see you can enter a command there. 199 | Note that it's important to q)uit the datawalk session, or your REPL buffer may 200 | be left in a problematic state. 201 | 202 | ## License 203 | 204 | Copyright © 2017 Jesse Davis (aka Egg Syntax) 205 | 206 | Distributed under the Eclipse Public License either version 1.0 or (at 207 | your option) any later version. 208 | -------------------------------------------------------------------------------- /doc/intro.md: -------------------------------------------------------------------------------- 1 | # Introduction to datawalk 2 | 3 | TODO: write [great documentation](http://jacobian.org/writing/what-to-write/) 4 | -------------------------------------------------------------------------------- /notes.md: -------------------------------------------------------------------------------- 1 | - ClojureScript ideas: 2 | - Would use a customized clojure.main repl, but AFAIK it won't work w cljs. 3 | - A key challenge for making this work in cljs is that generic user input 4 | isn't available in cljs. I brought it up in #clojurescript, and found the 5 | following resources: 6 | - https://clojurians.slack.com/archives/C03S1L9DN/p1508602683000025 7 | - discussion goes at least until 2:30pm 8 | - note especially: `:special-fns` get processed by clj 9 | - https://github.com/abiocljs (@mfikes) 10 | - https://github.com/potetm/tire-iron 11 | - https://www.google.com/search?q=IReplEnvOption 12 | - As far as I can tell, special fns are generally defined by the repl, so it 13 | may not be possible to inject them for my own needs unless I create my own 14 | repl. 15 | - see https://github.com/bhauman/lein-figwheel/blob/bddbcc0fe326ea1e4aafb649a0b95c9113377611/sidecar/src/figwheel_sidecar/system.clj 16 | - Other options: 17 | - I could just make a totally ordinary fn and make users use that at the 18 | cljs repl -- ie just print it out, and then do 19 | user> (x b) ; backward 20 | user> (x 3) ; drill to item 3 21 | ...and so on. 22 | - I could make users run in a specific env, eg planck or node. Unfortunately, 23 | reading input seems to be wildly inconsistent across browsers (eg 24 | SpiderMonkey has `read-line`), so I couldn't even make something that 25 | worked in all browser repls :( 26 | - I could make users copy-paste over into clj :( 27 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject datawalk "0.1.13-SNAPSHOT" 2 | :description "A single-purpose tool for rapid REPL exploration of complex data structures" 3 | :url "https://github.com/eggsyntax/datawalk" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.9.0-RC1"] 7 | [org.clojure/clojurescript "1.9.908"]] 8 | :plugins [[lein-cljsbuild "1.1.7"]] 9 | :cljsbuild {:builds [{:source-paths ["src"] 10 | :compiler {:output-to "war/javascripts/main.js" ; default: target/cljsbuild-main.js 11 | :optimizations :whitespace 12 | :pretty-print true}}]} 13 | :lein-release {:deploy-via :lein-install} 14 | :profiles {:dev {:dependencies [[org.clojure/test.check "0.9.0"] 15 | [org.clojure/tools.nrepl "0.2.10"] 16 | [com.cemerick/piggieback "0.2.2"]] 17 | 18 | :repl-options {:nrepl-middleware [cemerick.piggieback/wrap-cljs-repl]}}}) 19 | -------------------------------------------------------------------------------- /resources/fully-interactive.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eggsyntax/datawalk/6e1a3fcde694515e3bf82d19dfdcfa06436530eb/resources/fully-interactive.gif -------------------------------------------------------------------------------- /resources/semi-interactive.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eggsyntax/datawalk/6e1a3fcde694515e3bf82d19dfdcfa06436530eb/resources/semi-interactive.gif -------------------------------------------------------------------------------- /src/datawalk/core.cljc: -------------------------------------------------------------------------------- 1 | (ns datawalk.core 2 | (:require [datawalk.datawalk :as dw] 3 | [datawalk.print :as pr] 4 | [datawalk.parse :as ps] 5 | [clojure.string :as string] 6 | ;; Maybe later, for attempting read-line in cljs 7 | #_[clojure.tools.reader :as rdr] 8 | #_[clojure.tools.reader.reader-types :as rdrt]) 9 | #?(:cljs (:require-macros [datawalk.core :refer [w]]))) 10 | 11 | ;; Notes: 12 | ;; - Throughout: d = data 13 | ;; - Assumption: the order of (keys m) will be consistent during a session. 14 | ;; Empirically, this seems to be true, although I wouldn't necessarily trust 15 | ;; it in production code. 16 | ;; - Goals: 17 | ;; - Get some fspecs in there -- hmm, can I access before-and-after values 18 | ;; for the state atoms? 19 | ;; - This is based on a more special-purpose program I wrote to explore 20 | ;; Datomic data, which took heavy advantage of the laziness of Datomic's 21 | ;; EntityMap. I'd ideally like to bring some of that over to datawalk at 22 | ;; some point. 23 | 24 | (def prompt "[datawalk] > ") 25 | 26 | ;; Commands (in addition to drill) which advance the time step 27 | (def ^:private time-stepping? #{dw/root dw/up dw/function}) 28 | 29 | (defn initialize-state [d] 30 | (reset! dw/data d) 31 | (reset! dw/the-root d) 32 | (reset! dw/paths {d []}) ; Start with (empty) path to root 33 | (reset! dw/saved {}) 34 | (reset! dw/the-past []) 35 | (reset! dw/the-future [])) 36 | 37 | 38 | (defn print-globals [] (pr/print-globals (@dw/paths @dw/data) @dw/saved @dw/the-past @dw/the-future)) 39 | 40 | (defn- read-input 41 | "Get user input (at repl) -- later this needs to be generalized for both clj 42 | and the various cljs environments." 43 | [] 44 | (flush) 45 | (let [input #?(:clj (read-line) 46 | :cljs nil)] ; (see cljs section of README) 47 | (println input) 48 | input)) 49 | 50 | (defn set-line-length [n] 51 | (let [key-len (int (* 0.2 n))] 52 | (swap! pr/config assoc 53 | :max-line-length n 54 | :max-key-length key-len))) 55 | 56 | (defn datawalk 57 | "Run a single step of exploration. Inner fn called by both `repl` and `w`. 58 | Takes a data structure to act on, and input to parse and act on. Both prints 59 | and returns the new value of current data." 60 | [data in] 61 | (flush) 62 | (let [f (ps/parse in) 63 | next-data (if f (f data) data)] 64 | ;; We store data in an atom only so that it can be referred 65 | ;; to elsewhere; we don't need it in this fn. 66 | (reset! dw/data next-data) 67 | (when (:debug-mode @pr/config) (print-globals)) 68 | (pr/print-data next-data) 69 | (flush) 70 | (when (and 71 | (or (ps/read-int in) (time-stepping? f)) 72 | (not= data next-data)) ; complicaton from various edge cases 73 | (swap! dw/the-past conj data) 74 | (reset! dw/the-future [])) 75 | next-data)) 76 | 77 | ;; datawalk essentially has two versions, a fully-interactive version which 78 | ;; (currently) only runs in Clojure, and a semi-interactive version which runs 79 | ;; anywhere. See README for more details. In short, `repl` is the fully- 80 | ;; interactive version, and `look-at` + `w` is the semi-interactive. Note that 81 | ;; the two versions are equal in power; the main advantage of the fully- 82 | ;; interactive version is that it requires typing fewer characters. 83 | 84 | #?(:clj 85 | (defn repl 86 | "Runs a small, self-contained, fully-interactive REPL for exploring data 87 | (Clojure-only for the moment)." 88 | [d] 89 | (println "Exploring interactively.\n") 90 | (initialize-state d) 91 | (pr/maybe-initialize-config) 92 | (pr/print-data d) 93 | (loop [data d] 94 | (print prompt) 95 | (flush) ; no-op in cljs 96 | (let [in (read-input)] 97 | (cond 98 | (= "q" in) @dw/saved 99 | (= "x" in) data 100 | :else (recur (datawalk data in))))))) 101 | 102 | (defn look-at 103 | "Initializes the semi-interactive version. Typically you should call look-at 104 | once, and then w many times. Usable in all environments." 105 | [d] 106 | (println "Exploring semi-interactively. 107 | Now that you've initialized the data, use w to continue. 108 | (w h) will give you a summary of available commands.\n") 109 | (initialize-state d) 110 | (pr/maybe-initialize-config) 111 | (pr/print-data d) 112 | (flush)) 113 | 114 | (defmacro w 115 | "Take a single step through the data, using any of the commands. For example, 116 | user> (w 2) ; drill to item 2 117 | user> (w p) ; print path to current data 118 | user> (w b) ; step backward 119 | Use (w h) to get a summary of available commands. w presumes you've already 120 | called `look-at` once to specify what data is being explored. 121 | Usable in all environments." 122 | [& args] 123 | (let [string-args# (mapv str args)] 124 | `(if @dw/data 125 | (do (datawalk @dw/data ~@string-args#) ; updates state, 126 | nil) ; ignore return val (so it's not printed again) 127 | (println "No data to explore. Perhaps you haven't called look-at?")))) 128 | 129 | 130 | (comment 131 | ;; fully-interactive 132 | (repl {:a 1 :b {:c #{2 3 4} :d "5" :e [6 7 {:f "8" :g {:h :9}}]}}) 133 | 134 | ;; semi-interactive 135 | (look-at {:a 1 :b {:c #{2 3 4} :d "5" :e [6 7 {:f "8" :g {:h :9}}]}})) 136 | ;; followed by any number of calls to w 137 | -------------------------------------------------------------------------------- /src/datawalk/datawalk.cljc: -------------------------------------------------------------------------------- 1 | (ns datawalk.datawalk 2 | "Transforms data" 3 | (:require [clojure.pprint :refer [pprint]] 4 | [datawalk.print :as pr] 5 | [datawalk.util :as u])) 6 | 7 | ;;;;;;; State: 8 | 9 | ;; We track current data, 10 | (def data (atom nil)) ; we don't rely on this except for non-interactive use 11 | 12 | ;; the root / starting-point 13 | (def the-root (atom nil)) 14 | 15 | ;; the paths from root to each visited node (we save them all so we can 16 | ;; know the correct path when we time travel), 17 | (def paths (atom {})) 18 | 19 | ;; a map of values to return on exit, 20 | (def saved (atom {})) 21 | 22 | ;; the past (visited nodes), 23 | (def the-past (atom [])) 24 | 25 | ;; and the future. 26 | (def the-future (atom [])) 27 | 28 | ;; (I originally wrote a small initial version of datawalk that passed all of 29 | ;; these around at every recur step, differently for each (or most) operations. 30 | ;; It works fine, but it's clumsier and less readable, so I'm happy to use a few 31 | ;; namespaced globals for a small, self-contained project like this.) 32 | 33 | ;;;;;;; Helpers 34 | 35 | ;; The past and the future are both stacks; to move backward or forward, we pop 36 | ;; something off one stack, push current data onto the other, and return the 37 | ;; popped value as the new present. 38 | ;; `from-time` and `to-time` are the-past & the-future in some order. `present` 39 | ;; is just the current data, which may not yet be part of the-past or the-future. 40 | (defn- time-travel [from-time present to-time] 41 | (if (seq @to-time) 42 | (let [new-present (peek @to-time)] 43 | (swap! from-time conj present) 44 | (swap! to-time pop) 45 | new-present) 46 | (do (println "You have reached the end of time. You shall go no further.\n") 47 | present))) 48 | 49 | ;;;;;;; User API 50 | 51 | ;; All API fns take data as their final argument, and return updated data. 52 | ;; Changes to paths, saved, the-past, and the-future atoms are made inline as 53 | ;; side effects. 54 | 55 | (defn no-op [data] 56 | data) 57 | 58 | ;; TODO added a quick fix in 0.1.12 because there was an outstanding bug due 59 | ;; to clj derefable protocols not being available in cljs. Find something more 60 | ;; elegant ;P 61 | 62 | ;; TODO remove some repetition 63 | #?(:clj 64 | (defn drill 65 | "Given a number n, drill down to that numbered item" 66 | [n data] 67 | ;; (println "drilling into" data) 68 | (try 69 | (cond 70 | (sequential? data) 71 | , (let [next-data (nth data n) 72 | next-path (conj (@paths data) n)] 73 | (swap! paths assoc (u/not-set next-data) next-path) 74 | next-data) 75 | (set? data) 76 | , (let [next-data (nth (u/not-set data) n) 77 | next-path (conj (@paths data) n)] 78 | (swap! paths assoc next-data next-path) 79 | next-data) 80 | (map? data) 81 | , (let [k (nth (keys data) n) 82 | next-data (get data k) 83 | next-path (conj (@paths data) k)] 84 | ;; (println "next-path:" next-path) 85 | (swap! paths assoc (u/not-set next-data) next-path) 86 | next-data) 87 | (instance? clojure.lang.IBlockingDeref data) 88 | ;; Drilling into a future/promise dereferences it with a fast 89 | ;; timeout so we don't block if it doesn't contain a value yet. 90 | , (if-let [next-data (deref data 100 nil)] 91 | (do (swap! paths assoc 92 | (u/not-set next-data) 93 | (conj (@paths data) 'deref)) 94 | next-data) 95 | (do (prn "Can't deref, no data yet!") 96 | data)) 97 | ;; Otherwise it's a non-blocking refable, so we can just deref it 98 | (instance? clojure.lang.IDeref data) 99 | ;; Drilling into a derefable dereferences it 100 | , (let [next-data (deref data)] 101 | (swap! paths assoc 102 | (u/not-set next-data) 103 | (conj (@paths data) 'deref)) 104 | next-data) 105 | :else ; not drillable; no-op 106 | , (do (println "Can't drill into a" (type data) "\n") 107 | data)) 108 | (catch #?(:clj IndexOutOfBoundsException 109 | :cljs js/Error) e 110 | (do (println "\nThere is no item numbered" n "in the list of current data. Try again.\n") 111 | data)))) 112 | 113 | :cljs 114 | (defn drill 115 | "Given a number n, drill down to that numbered item" 116 | [n data] 117 | ;; (println "drilling into" data) 118 | (try 119 | (cond 120 | (sequential? data) 121 | , (let [next-data (nth data n) 122 | next-path (conj (@paths data) n)] 123 | (swap! paths assoc (u/not-set next-data) next-path) 124 | next-data) 125 | (set? data) 126 | , (let [next-data (nth (u/not-set data) n) 127 | next-path (conj (@paths data) n)] 128 | (swap! paths assoc next-data next-path) 129 | next-data) 130 | (map? data) 131 | , (let [k (nth (keys data) n) 132 | next-data (get data k) 133 | next-path (conj (@paths data) k)] 134 | ;; (println "next-path:" next-path) 135 | (swap! paths assoc (u/not-set next-data) next-path) 136 | next-data) 137 | (instance? cljs.core/Atom data) 138 | ;; Drilling into a derefable dereferences it 139 | , (let [next-data (deref data)] 140 | (prn "drilling into atom") 141 | (swap! paths assoc 142 | (u/not-set next-data) 143 | (conj (@paths data) 'deref)) 144 | next-data) 145 | :else ; not drillable; no-op 146 | , (do (println "Can't drill into a" (type data) "\n") 147 | data)) 148 | (catch #?(:clj IndexOutOfBoundsException 149 | :cljs js/Error) e 150 | (do (println "\nThere is no item numbered" n "in the list of current data. Try again.\n") 151 | data))))) 152 | 153 | (defn quit [data] 154 | ;; Returns saved data 155 | @saved) 156 | 157 | (defn exit-with-current [data] 158 | data) 159 | 160 | (defn- save-to-saved-map 161 | [data] 162 | (let [next-index (+ 1 (count @saved))] 163 | (swap! saved assoc next-index data) 164 | data)) 165 | 166 | (defn save-current 167 | "Save the current data for later availability. @saved is a map containing 168 | numeric indices (for easy retrieval) & arbitrary items. It's 1-indexed for 169 | ease of keyboarding, and to serve as a reminder that this is in some sense not 170 | a 'natural' index but an arbitrarily constructed one (eg we could have just as 171 | easily indexed by a, b, c...). " 172 | [data] 173 | (println "Saving current data to saved-data map") 174 | (save-to-saved-map data)) 175 | 176 | (defn save-path [data] 177 | (println "Saving the path to current to saved-data map") 178 | (save-to-saved-map (@paths data)) 179 | data) 180 | 181 | (defn backward [data] 182 | (time-travel the-future data the-past)) 183 | 184 | (defn forward [data] 185 | (time-travel the-past data the-future)) 186 | 187 | (defn root [data] 188 | @the-root) 189 | 190 | (defn up [data] 191 | (get-in @the-root (butlast (@paths data)))) 192 | 193 | (def help-text 194 | ;; quit and exit-with-current are only relevant in the fully interactive 195 | ;; repl version, but this is not actually enforced in any way. 196 | ["q: quit ; exit and return saved values if any (repl-only)" 197 | "x: exit-with-current ; exit & return just this ent (repl-only)" 198 | "s: save-current ; save to map of return values" 199 | "v: save-path ; save path to map of return values" 200 | "b: backward ; step backward in history" 201 | "f: forward ; step forward in history" 202 | "r: root ; jump back to root" 203 | "u: up ; step upward [provides list of referring entities]" 204 | "h: help ; print help & return same ent" 205 | "p: print-path ; path: print path to current item." 206 | "m: print-saved-map ; print data saved so far" 207 | "c: print-full-cur ; print the current data in full, not truncated" 208 | "!: function ; call a 1-arg fn on data, jump to result (clj only)"]) 209 | 210 | 211 | (defn help [ent] 212 | (println "COMMANDS:") 213 | (println "# Enter any listed number to drill to that item") 214 | (run! println help-text) 215 | (println) 216 | ent) 217 | 218 | (defn print-path [data] 219 | (println "PATH:") 220 | (println (@paths data)) 221 | (println) 222 | data) 223 | 224 | (defn prn-saved-map [data] 225 | (println "SAVED:") 226 | (prn @saved) 227 | (println) 228 | data) 229 | 230 | (defn pprint-saved-map [data] 231 | (println "SAVED:") 232 | (pprint @saved) 233 | (println) 234 | data) 235 | 236 | (defn prn-full-cur [data] 237 | (println "CURRENT:") 238 | (prn data) 239 | (println) 240 | data) 241 | 242 | (defn pprint-full-cur [data] 243 | (println "CURRENT:") 244 | (pprint data) 245 | (println) 246 | data) 247 | 248 | #?(:clj (defn- read-input [prompt] 249 | (print prompt) 250 | (let [input (read-line)] 251 | (println input) 252 | input))) 253 | 254 | (defn function [data] 255 | #?(:clj 256 | (let [_ (println "Please enter a function of one variable") 257 | inp (read-input "Enter a fn >> ") 258 | f (eval (read-string inp))] 259 | (if (contains? #{"q" ""} inp) ; in case they want to abort 260 | data 261 | (if (fn? f) 262 | (f data) ; typical outcome 263 | (do (println inp "is not a function; try again.") 264 | (function data))))) 265 | :cljs 266 | (do (println "! command is only available in Clojure.") 267 | (println " In cljs, just call a fn on the data as you usually would.") 268 | (println) 269 | data))) 270 | -------------------------------------------------------------------------------- /src/datawalk/parse.cljc: -------------------------------------------------------------------------------- 1 | (ns datawalk.parse 2 | "Parses user input into a call to a fn in datawalk.datawalk" 3 | (:require [datawalk.datawalk :as dw])) 4 | 5 | 6 | (defn read-int [s] 7 | #?(:clj (try (Integer/parseInt s) 8 | (catch NumberFormatException _ nil)) 9 | :cljs (let [n (js/parseInt s)] 10 | ;; fails to #NaN, so we check for int? (NaN is not an int) 11 | (if (int? n) n nil)))) 12 | 13 | (def cmd-map 14 | {"q" dw/quit ; exit and return saved values if any 15 | "x" dw/exit-with-current ; exit & return just this value 16 | "s" dw/save-current ; save to map of return values 17 | "v" dw/save-path ; save path to map of return values 18 | "b" dw/backward ; step backward in history 19 | "f" dw/forward ; step forward in history 20 | "r" dw/root ; jump back to original root 21 | "u" dw/up ; step upward [provides list of referring entities] 22 | "h" dw/help ; print help & return same ent 23 | "p" dw/print-path ; print path from root to current item. 24 | "m" dw/prn-saved-map ; print the map of saved data 25 | "M" dw/pprint-saved-map ; print the map of saved data 26 | "c" dw/prn-full-cur ; print the current data in full, not truncated 27 | "C" dw/pprint-full-cur ; pretty-print the current data in full, not truncated 28 | "!" dw/function ; call a 1-arg fn on data, jump to result (clj-only) 29 | "" nil ; all others become no-op 30 | }) 31 | 32 | (defn parse [inp] 33 | ;; (println "raw input: " inp "is a" (type inp)) 34 | ;; If #: drill into that value 35 | (if-let [n (read-int inp)] 36 | (partial dw/drill n) 37 | ;; else: get fn to call on data 38 | (get cmd-map inp 39 | (fn [data] (do (println "Unknown command:" inp) 40 | (dw/no-op data)))))) 41 | -------------------------------------------------------------------------------- /src/datawalk/print.cljc: -------------------------------------------------------------------------------- 1 | (ns datawalk.print 2 | ;; We use cl-format because cljs doesn't have core fn format 3 | (:require [clojure.pprint :refer [cl-format]])) 4 | 5 | (def config (atom nil)) 6 | 7 | (defn maybe-initialize-config [] 8 | (when-not @config 9 | (reset! config {:max-items 30 10 | :max-line-length 120 11 | :max-key-length 24 12 | :debug-mode false}))) 13 | 14 | (defn- longest-length 15 | "Return the length of the longest (in # of chars) item in the coll" 16 | [coll] 17 | (min (:max-key-length @config) 18 | (apply max (map #(count (str %)) coll)))) 19 | 20 | (defn- quote-strings 21 | "Surround strings with quotation marks; return other values unchanged" 22 | [v] 23 | (if (string? v) (str "\"" v "\"") v)) 24 | 25 | (defn- limit-left 26 | "Ensure that string s doesn't exceed n chars" 27 | [n s] 28 | (subs (str s) 0 (min n (count (str s))))) 29 | 30 | (defn- limit-right 31 | "Ensure that string s doesn't exceed n chars; chops from right" 32 | [n s] 33 | (let [s (str s) 34 | slen (count s) 35 | cutoff (min n slen)] 36 | (subs s (- slen cutoff) slen))) 37 | 38 | (defn limitln 39 | ([n s] 40 | (limitln n s true)) 41 | ([n s from-left?] 42 | (let [limit (if from-left? limit-left limit-right)] 43 | (str (limit n s) "\n")))) 44 | 45 | (defn- is-seqable? 46 | "seqable? is added in clj 1.9 -- using this roughly equivalent fn for 47 | backward-compatibility" 48 | [x] 49 | (if (sequential? x) 50 | true 51 | (try 52 | (seq x) 53 | #?(:clj (catch IllegalArgumentException e nil) 54 | :cljs (catch js/Error e nil))))) 55 | 56 | (defn to-string 57 | "Specialized pretty-printer for printing our sequences of things with numbers prepended" 58 | [data] 59 | ;; (println "data =" data) 60 | (cond (string? data) ; strings masquerade as seqs, so we handle separately 61 | , (str " 00. " (quote-strings data)) 62 | (is-seqable? data) 63 | , (map-indexed 64 | (fn [index item] 65 | ;; data is a seq of items... 66 | (cond 67 | (sequential? data) 68 | , (limitln (:max-line-length @config) 69 | (cl-format nil "~2,'0D. ~A" index (quote-strings item))) 70 | ;; ...or a map of items (so item is a [k v]) 71 | (map? data) 72 | , (let [;; _ (println "item =" item) 73 | ;; _ (println "type =" (type item)) 74 | [k v] item 75 | format-s (str "~2,'0D. ~" 76 | (longest-length (keys data)) 77 | "A: ~A")] 78 | (limitln (:max-line-length @config) 79 | (cl-format nil format-s 80 | index 81 | (limit-right (:max-key-length @config) k) 82 | (quote-strings v)))) 83 | (set? data) 84 | , (limitln (:max-line-length @config) 85 | (cl-format nil "~2,'0D. ~A" index (quote-strings item))) 86 | :else 87 | , (limitln (:max-line-length @config) 88 | (cl-format nil "~2,'0D. ~A" index (quote-strings item))))) 89 | (take (:max-items @config) data)) 90 | :else ; singular type 91 | (cond 92 | ;; TODO remember to handle derefables in the protocols branch too 93 | #?(:clj (instance? clojure.lang.IDeref data) 94 | :cljs (instance? cljs.core.Atom data)) 95 | ;; We number derefables, even though they're non-sequential, to help 96 | ;; indicate that we can drill into them. 97 | , (limitln (:max-line-length @config) 98 | (cl-format nil "00. ~A" (quote-strings data))) 99 | :else ; Nothing we handle specially -- just `str` it. 100 | , (str " " data)))) 101 | 102 | (defn to-debug-string [x] 103 | (if (and (is-seqable? x) (not (string? x))) 104 | (map (partial limitln (:max-line-length @config)) x) 105 | (limitln (:max-line-length @config) x))) 106 | 107 | (defn print-globals [path saved the-past the-future] 108 | (println "PATH:\n" (to-debug-string path)) 109 | (println "SAVED:\n" (to-debug-string saved)) 110 | (println "THE-PAST:\n" (to-debug-string the-past)) 111 | (println "THE-FUTURE:\n" (to-debug-string the-future)) 112 | (println)) 113 | 114 | (defn print-data [data] 115 | (println (to-string data))) 116 | -------------------------------------------------------------------------------- /src/datawalk/todo.org: -------------------------------------------------------------------------------- 1 | * TODO get tests working in cljs 2 | * TODO BUG path addition fails on sets 3 | ** (as it sorta-should, since get-in won't work) 4 | ** Decide on behavior for sets, lists, and derefables. 5 | * DONE automatically deref references? 6 | ** Ideal solution: 7 | 1) Works correctly with forward & backward 8 | 2) Works properly with up. 9 | - "Up" should lift back up out of the atom? 10 | 3) Works correctly with path 11 | - What does that even mean? A deref step means `get-in` will break anyway. 12 | - And can't be replaced with `->` because that doesn't work for eg ints 13 | 4) Seems natural to users 14 | ** Options: 15 | *** Deref is an ordinary `drill` step 16 | **** 1, 2, 4 - breaks 3 (by default) 17 | *** Deref is ordinary `drill` but breaks path 18 | **** 1, 2, 3, 4? 19 | **** Change root? 20 | ***** Might require a bunch of special handling 21 | **** Maybe...you're in a special mode/state when inside a derefable, with temp root & a bit of modified behavior? 22 | *** Deref is a special command 23 | **** 1, 2, 3?, 4 24 | *** Deref is implicit, you never see `atom` 25 | **** ? 26 | *** Deref is a printing affordance only, can't drill 27 | **** ? 28 | ** Should `drill` be a protocol? 29 | ** note that some derefables will cause problems, since they don't currently contain vals. 30 | *** But `deref` optionally takes a timeout & a timeout-val; that should solve. 31 | ** What's derefable? @ref/@agent/@var/@atom/@delay/@future/@promise 32 | ** Detecting derefability: 33 | *** (partial satisfies? IDeref) 34 | *** #(instance? Atom %) 35 | *** #(satisfies? IDeref %) 36 | ** Togglable setting for deref behavior? 37 | * DONE ensure lazy seqs are realized before stringifying! (only a problem w/ protocols?) 38 | * TODO change printing to protocol so that users can extend (see protocols branch) 39 | ** In progress, see `protocols` branch 40 | * TODO get-in doesn't always work -- it fails on lists (w/ numeric indices). Somehow I never noticed... 41 | ** Convert all strings to vectors? But useless when user wants to exit repl and call get-in :/ 42 | * TODO printing keys -- if namespaced keyword & longer than max-line-length, take `name` before limiting 43 | ** At (limit-right (:max-key-length @config) k) 44 | * TODO add example data (& instructions) - maybe with core/demo fn for ease 45 | * TODO need some easy way to access state from core so users don't need to require multiple nss 46 | * TODO handle java.lang.IndexOutOfBoundsException (& JS equivalent) when drilling 47 | * TODO THINK 48 | ** As I build protocols for other datatypes - json, datomic, etc - it'll probably entail extra dependencies. 49 | ** Consider creating 2 builds, one with minimal dependencies and one that's batteries-included & has protocols for a bunch of datatypes. 50 | * Commands: 51 | ** TODO commands for find-key, find-val? g)rep 52 | ** DONE split `c` into prn and pprint 53 | *** TODO Consider general variants of printing cmds, where capital means pprint 54 | **** Could handle it at the `parse` level, where capital letters are lowercased & a flag is set to indicate "special" or "variant" 55 | ** TODO change certain commands to mnemonic symbols? I'm thinking < and > for backward/forward, maybe ^ for up. 56 | ** TODO consider: `t` for type 57 | ** TODO consider: map and/or filter commands 58 | ** TODO do I possibly want a command to save current to a named var? (eg d)ef ) 59 | ** TODO commands to page through when > max-items? May be out of scope. 60 | * TODO Find or build a way to generalize `read-line` across environments. 61 | ** @mfikes ABIO lib might help. 62 | -------------------------------------------------------------------------------- /src/datawalk/util.cljc: -------------------------------------------------------------------------------- 1 | (ns datawalk.util 2 | "Util fns, especially shared ones") 3 | 4 | (defn not-set [x] 5 | (if (set? x) (vec x) x)) 6 | -------------------------------------------------------------------------------- /test/datawalk/core_test.cljc: -------------------------------------------------------------------------------- 1 | (ns datawalk.core-test 2 | "Integration tests, really. Does a series of walks through data & verifies 3 | that things are as they should be." 4 | (:require [clojure.string :as s] 5 | [clojure.test :refer [is deftest testing run-tests]] 6 | [datawalk.core :refer [look-at w datawalk]] 7 | [datawalk.datawalk :as dw])) 8 | 9 | 10 | ;; We're going to use the underlying `datawalk` fn rather than add complexity by 11 | ;; abstracting `read-line` just for test purposes. Unlike `repl`, `w` always 12 | ;; returns nil, so we have to check the global data to ensure the current data 13 | ;; (or other globals) are as expected. 14 | 15 | (defmacro gval 16 | "global-value. Return the value in one of the global-state atoms." 17 | [g-name#] 18 | `(deref (var-get (var ~g-name#)))) 19 | 20 | (defn dw-call 21 | "Walk through data with a series of steps, as though the steps 22 | were entered individually in the repl. Non-numeric steps should be 23 | quoted to prevent evaluation (eg steps could be [2 's 4])" 24 | [data steps] 25 | (reduce datawalk data (map str steps))) 26 | 27 | (defn expect 28 | "Verifies that when `steps` have been performed on `data`, the 29 | resulting value is expected-result" 30 | [data steps expected-result] 31 | (look-at data) ; init 32 | (is (= expected-result (dw-call data steps)))) 33 | 34 | (defn expect-saved 35 | "Verifies that when `steps` have been performed on `data`, the 36 | data in the saved-data map (dw/saved) matches expected-saved" 37 | [data steps expected-saved] 38 | (look-at data) ; init 39 | (dw-call data steps) 40 | (is (= expected-saved (gval dw/saved)))) 41 | 42 | (defn- whitespace-normalized [string] 43 | (s/trim (s/replace string #"\s+" ""))) 44 | 45 | (defn matches-ish 46 | "True if two strings differ by at most whitespace" 47 | [s1 s2] 48 | (= (whitespace-normalized s1) (whitespace-normalized s2))) 49 | 50 | (defn expect-str 51 | "Verifies that when `steps` have been performed on `data`, the 52 | printed session matches expected-result" 53 | [data steps expected-result] 54 | (look-at data) ; init 55 | (let [result (with-out-str (dw-call data steps))] 56 | (println (str "**" result "**")) 57 | (println "***") 58 | (prn result) 59 | (prn "whit-nor") 60 | (prn (whitespace-normalized result)) 61 | (println) 62 | (is (= (whitespace-normalized expected-result) 63 | (whitespace-normalized result)))) 64 | 65 | #_(is (= expected-result (with-out-str (dw-call data steps))))) 66 | 67 | (deftest vec-test 68 | (expect [1 2 [3 [[4] 5 6]]] 69 | [2 1 0 0] 70 | 4)) 71 | 72 | (deftest set'-test 73 | (expect #{1 2 3 [4 #{5 6 7}]} 74 | [3 1 2] 75 | 5)) 76 | 77 | (deftest list-test 78 | (expect '(1 2 (3 ((4) 5))) 79 | [2 1 0 0] 80 | 4)) 81 | 82 | (deftest map-test 83 | (expect {1 2 3 [4 {5 6}]} 84 | [1 1 0] 85 | 6)) 86 | 87 | (deftest saved-cur-test 88 | (expect-saved {1 2 3 [4 {5 6}]} 89 | [1 's 1 0 's] 90 | {1 [4 {5 6}], 2 6})) 91 | 92 | (deftest saved-path-test 93 | (expect-saved {1 2 3 [4 {5 6}]} 94 | [1 'v 'p 1 0 'p 'v] 95 | {1 [3], 2 [3 1 5]})) 96 | 97 | (deftest time-travel-test 98 | (expect {1 2 3 [4 {5 6}]} 99 | [1 1 'b 'f 'f 0 'b 'b 'b 'b 1 1] 100 | {5 6}) 101 | (expect {1 '(2 3) 7 [4 {5 6}]} 102 | [0 1 'b 'b] 103 | {1 '(2 3) 7 [4 {5 6}]}) 104 | (expect {1 '(2 3) 7 [4 {5 6}]} 105 | [1 1 0 'f 'f 'f 'b 'b 'b] 106 | {1 '(2 3) 7 [4 {5 6}]}) 107 | (expect {1 '(2 3) 7 [4 {5 6}]} 108 | ['b 'b 'b] 109 | {1 '(2 3) 7 [4 {5 6}]}) 110 | (expect {1 '(2 3) 7 [4 {5 6}]} 111 | [1 'b 'f 'b 'f 'b] 112 | {1 '(2 3) 7 [4 {5 6}]})) 113 | 114 | (deftest up-test 115 | (expect-saved {1 2 3 [4 {5 6}]} 116 | [1 1 0 'p 'v 'u 'u 'u 's 'p 'v] 117 | {1 [3 1 5], 2 {1 2, 3 [4 {5 6}]}, 3 []})) 118 | 119 | (deftest refable-test 120 | (expect [1 (atom {1 [2 (atom 3)]}) 4] 121 | [1 0 0 1 0] 122 | 3) 123 | #?(:clj ; blocking derefables are clj-only 124 | (let [blocking-refable (future (Thread/sleep 200) :done)] 125 | (look-at blocking-refable) 126 | (is (not= (datawalk blocking-refable "0") :done)) 127 | (Thread/sleep 200) 128 | (is (= (datawalk blocking-refable "0") :done))))) 129 | 130 | ;; Examine string output. Brittle, but necessary if we want to test the 131 | ;; print-centric tangential commands. 132 | (deftest map-str-test 133 | (expect-str {1 2 3 [4 {5 6}]} 134 | [1 1 'p] 135 | "(00. 4 01. {5 6} ) (00. 5: 6 ) PATH: [3 1] (00. 5: 6 )")) 136 | --------------------------------------------------------------------------------