-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.clj
38 lines (32 loc) · 946 Bytes
/
terminal.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(ns crockery.terminal
(:require [clojure.string :as str]
[clojure.java.io :as io]
[clojure.java.shell :refer [sh]]))
(defn- positive-int [s]
(when (not-empty s)
(let [n (Integer. (str/trim s))]
(when (pos-int? n) n))))
(defn- get-paths []
(-> (System/getenv "PATH") (str/split #":")))
(defn- command-exists? [cmd paths]
(some
#(-> (str % "/" cmd) io/file .isFile)
paths))
(defn- stty-detect [paths]
(when (command-exists? "stty" paths)
(-> (sh "/bin/sh" "-c" "stty size < /dev/tty")
:out
(str/split #" +")
last
positive-int)))
(defn- tput-detect [paths]
(when (command-exists? "tput" paths)
(-> (sh "/bin/sh" "-c" "tput cols 2> /dev/tty")
:out
positive-int)))
(defn detect-terminal-width []
(let [paths (get-paths)]
(or
(some-> (System/getenv "COLUMNS") positive-int)
(stty-detect paths)
(tput-detect paths))))