diff --git a/src/table/core.clj b/src/table/core.cljc similarity index 88% rename from src/table/core.clj rename to src/table/core.cljc index 8f8f40e..a5ad5da 100644 --- a/src/table/core.clj +++ b/src/table/core.cljc @@ -1,6 +1,8 @@ (ns table.core - (:require table.width) - (:use [clojure.string :only [join]] )) + (:require table.width + #?@(:cljs [[goog.string] + [goog.string.format]])) + (:use [clojure.string :only [join]])) (declare style-for format-cell render-rows-with-fields escape-newline render-rows table-str) @@ -74,7 +76,9 @@ (let [[rows fields] (generate-rows-and-fields table options) rendered-rows (render-rows-with-fields rows fields options)] (if (:desc options) - (concat rendered-rows [(format "%s rows in set" (count rows))]) + (concat rendered-rows [(#?(:clj format + :cljs goog.string.format) + "%s rows in set" (count rows))]) rendered-rows))) (defn- render-rows-with-fields [rows fields options] @@ -91,7 +95,9 @@ (let [dash-key (if (style-for dash) dash :dash)] (wrap-row (map #(apply str (repeat - (.length (str %))(style-for dash-key))) headers) + (#?(:clj .length + :cljs .-length) (str %)) + (style-for dash-key))) headers) (style-for section)))) header (wrap-row headers (style-for :header-walls)) body (map #(wrap-row (fmt-row %) (style-for :body-walls)) rows) ] @@ -100,14 +106,15 @@ body [( border-for :bottom :bottom-dash)]))) (defn- escape-newline [string] - (clojure.string/replace string (str \newline) (char-escape-string \newline))) + (clojure.string/replace string "\n" "\\n")) (defn- style-for [k] (k *style*)) (defn format-cell [string width] (if (zero? width) "" - (format + (#?(:clj format + :cljs goog.string.format) (str "%-" width "." width "s") (if (> (count string) width) (str (.substring string 0 (- width 3)) "...") diff --git a/src/table/width.clj b/src/table/width.cljc similarity index 68% rename from src/table/width.clj rename to src/table/width.cljc index 6c30fe0..7601b31 100644 --- a/src/table/width.clj +++ b/src/table/width.cljc @@ -1,5 +1,7 @@ (ns table.width - (:require clojure.java.shell clojure.java.io clojure.string)) + (:require clojure.string + #?@(:clj [[clojure.java.shell] + [clojure.java.io]]))) (declare get-initial-widths max-width-per-field actual-width auto-resize-widths detect-terminal-width command-exists?) @@ -46,25 +48,31 @@ (if (> arg 0) arg 100) arg)) -(defn- stty-detect [] - (->> (clojure.java.shell/sh "/bin/sh" "-c" "stty -a < /dev/tty") - :out - (re-find #" (\d+) columns") - vec - second - ((fn [_ two] (if two (Integer. two))) :not-used))) - ; since Java doesn't recognize COLUMNS by default you need to `export COLUMNS` for it ; be recognized -(defn- detect-terminal-width [] - (ensure-valid-width - (cond - (System/getenv "COLUMNS") (Integer. (System/getenv "COLUMNS")) - (command-exists? "stty") (stty-detect)))) +#?(:clj + (defn- detect-terminal-width [] + (ensure-valid-width + (cond + (System/getenv "COLUMNS") (Integer. (System/getenv "COLUMNS")) + (command-exists? "stty") (stty-detect)))) + + :cljs + (defn- detect-terminal-width [] + (ensure-valid-width 100))) + +#?(:clj + (defn- stty-detect [] + (->> (clojure.java.shell/sh "/bin/sh" "-c" "stty -a < /dev/tty") + :out + (re-find #" (\d+) columns") + vec + second + ((fn [_ two] (if two (Integer. two))) :not-used)))) -(defn- command-exists? - "Determines if command exists in $PATH" - [cmd] - (some +#?(:clj (defn- command-exists? + "Determines if command exists in $PATH" + [cmd] + (some #(-> (str % "/" cmd) clojure.java.io/file .isFile) - (-> (System/getenv "PATH") (clojure.string/split #":")))) + (-> (System/getenv "PATH") (clojure.string/split #":")))))