Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/table/core.clj → src/table/core.cljc
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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]
Expand All @@ -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) ]
Expand All @@ -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)) "...")
Expand Down
46 changes: 27 additions & 19 deletions src/table/width.clj → src/table/width.cljc
Original file line number Diff line number Diff line change
@@ -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?)
Expand Down Expand Up @@ -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 #":")))))