-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move some stuff from planck.io to planck.core
- Loading branch information
Showing
5 changed files
with
105 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
hello stderr | ||
WARNING: planck.io/spit is deprecated. at line 1 | ||
WARNING: planck.io/spit is deprecated. at line 1 | ||
WARNING: planck.io/spit is deprecated. at line 1 | ||
WARNING: planck.io/spit is deprecated. at line 1 | ||
WARNING: planck.io/spit is deprecated. at line 1 | ||
WARNING: planck.io/spit is deprecated. at line 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,89 @@ | ||
(ns planck.core) | ||
|
||
(defprotocol IClosable | ||
(-close [this])) | ||
|
||
(defprotocol IReader | ||
(-read [this] "Returns available characters as a string or nil of EOF.")) | ||
|
||
(defrecord Reader [raw-read raw-close] | ||
IReader | ||
(-read [_] | ||
(raw-read)) | ||
IClosable | ||
(-close [_] | ||
(raw-close))) | ||
|
||
(defrecord Writer [raw-write raw-flush raw-close] | ||
IWriter | ||
(-write [_ s] | ||
(raw-write s)) | ||
(-flush [_] | ||
(raw-flush)) | ||
IClosable | ||
(-close [_] | ||
(raw-close))) | ||
|
||
(defonce | ||
^{:doc "A planck.io/IReader representing standard input for read operations." | ||
:dynamic true} | ||
*in* | ||
(Reader. js/PLANCK_RAW_READ_STDIN nil)) | ||
|
||
(set! cljs.core/*out* (Writer. js/PLANCK_RAW_WRITE_STDOUT js/PLANCK_RAW_FLUSH_STDOUT nil)) | ||
|
||
(defonce | ||
^{:doc "A cljs.core/IWriter representing standard error for print operations." | ||
:dynamic true} | ||
*err* | ||
(Writer. js/PLANCK_RAW_WRITE_STDERR js/PLANCK_RAW_FLUSH_STDERR nil)) | ||
|
||
(defn file-seq | ||
"A tree seq on PLKFiles" | ||
[dir] | ||
(js/PLANCK_IO_FILESEQ dir)) | ||
|
||
(defn- fission! | ||
"Breaks an atom's value into two parts. The supplied function should | ||
return a pair. The first element will be set to be the atom's new | ||
value and the second element will be returned." | ||
[atom f & args] | ||
(loop [] | ||
(let [old @atom | ||
[new-in new-out] (apply f old args)] | ||
(if (compare-and-set! atom old new-in) | ||
new-out | ||
(recur))))) | ||
|
||
(defonce ^:private buffer (atom nil)) | ||
|
||
(defn read-line | ||
"Reads the next line from the current value of planck.io/*in*" | ||
[] | ||
(if-let [buffered @buffer] | ||
(let [n (.indexOf buffered "\n")] | ||
(if (neg? n) | ||
(if-let [next-characters (-read *in*)] | ||
(do | ||
(swap! buffer (fn [s] (str s next-characters))) | ||
(recur)) | ||
(fission! buffer (fn [s] [nil s]))) | ||
(fission! buffer (fn [s] [(let [residual (subs s (inc n))] | ||
(if (= "" residual) | ||
nil | ||
residual)) | ||
(subs s 0 n)])))) | ||
(when (reset! buffer (-read *in*)) | ||
(recur)))) | ||
|
||
(defn slurp | ||
"Slurps a file" | ||
[filename] | ||
(or (js/PLANCK_READ_FILE filename) | ||
(throw (js/Error. filename)))) | ||
|
||
(defn spit | ||
"Spits a file" | ||
[filename content] | ||
(js/PLANCK_WRITE_FILE filename content) | ||
nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters