From 43eb5f9c3c8b451086027707275691c55d50e05a Mon Sep 17 00:00:00 2001 From: Anthony Grimes Date: Sat, 22 Sep 2012 21:52:06 -0500 Subject: [PATCH] Add some sh-like buffering. --- src/conch/sh.clj | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/conch/sh.clj b/src/conch/sh.clj index 7471b86..f02c26b 100644 --- a/src/conch/sh.clj +++ b/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))))