Skip to content

Commit

Permalink
Add some sh-like buffering.
Browse files Browse the repository at this point in the history
  • Loading branch information
Raynes committed Oct 16, 2012
1 parent c5ac6dd commit 43eb5f9
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/conch/sh.clj
@@ -1,19 +1,30 @@
(ns conch.sh
(require [conch.core :as conch]
[clojure.java.io :as io]))
[clojure.java.io :as io]
[clojure.string :as string]))

(defn char-seq [reader]
(map char (take-while #(not= % -1) (repeatedly #(.read reader)))))

(defn buffer-stream [stream buffer]
(let [reader (io/reader stream)]
(cond
(= :none buffer) (char-seq reader)
(number? buffer) (map string/join (partition buffer (char-seq reader)))
:else (line-seq reader))))

(defn run-command [name args options]
(let [proc (apply conch/proc name args)]
(when-let [in (:in options)] (conch/feed-from-string proc in))
(if-let [callback (:out options)]
(doseq [line (line-seq (io/reader (:out proc)))]
(callback line proc))
(doseq [buffer (buffer-stream (:out proc) (:buffer options))]
(callback buffer proc))
(conch/stream-to-string proc :out))))

(defn execute [name & args]
(let [end (last args)
options (and (map? end) end)
args (if options (butlast args) args)]
args (if options (drop-last args) args)]
(if (or (:out options) (:background options))
(future (run-command name args options))
(run-command name args options))))
Expand Down

0 comments on commit 43eb5f9

Please sign in to comment.