Skip to content

bbin ls fails outside of interative terminal #109

@SevereOverfl0w

Description

@SevereOverfl0w
(defn terminal-dimensions
  "Yields e.g. `{:cols 30 :rows 120}`"
  []
  (->
   (p/process ["stty" "size"] {:inherit true :out :string})
   deref
   :out
   str/trim
   (str/split #" ")
   (->> (map #(Integer/parseInt %))
        (zipmap [:rows :cols]))))

fails because the call to it is guarded on whether the stdout is a tty:

(defn plain-mode? [{:keys [plain] :as _cli-opts}]
  (or (fs/windows?) plain (not (tty-out?))))

But stty works on the stdin. So this blows up:


/tmp/foo
❯ bbin ls </dev/null 

stty: 'standard input': Inappropriate ioctl for device
----- Error --------------------------------------------------------------------
Type:     java.lang.NumberFormatException
Message:  For input string: ""
Location: /home/overfl0w/.local/bin/bbin:461:9

----- Context ------------------------------------------------------------------
457:    :out
458:    str/trim
459:    (str/split #" ")
460:    (->> (map #(Integer/parseInt %))
461:         (zipmap [:rows :cols]))))
             ^--- For input string: ""
462: 
463: (defn sh [cmd & {:as opts}]
464:   (doto (p/sh cmd (merge {:err :inherit} opts))
465:     p/check))
466: 

----- Stack trace --------------------------------------------------------------
clojure.core/zipmap                    - <built-in>
babashka.bbin.util/terminal-dimensions - /home/overfl0w/.local/bin/bbin:461:9
babashka.bbin.util/terminal-dimensions - /home/overfl0w/.local/bin/bbin:451:1
babashka.bbin.util/print-scripts       - /home/overfl0w/.local/bin/bbin:672:36
babashka.bbin.util/print-scripts       - /home/overfl0w/.local/bin/bbin:663:1
... (run with --debug to see elided elements)
babashka.bbin.cli/bbin                 - /home/overfl0w/.local/bin/bbin:1930:1
babashka.bbin.cli                      - /home/overfl0w/.local/bin/bbin:1936:3
clojure.core/apply                     - <built-in>
babashka.bbin.cli                      - /home/overfl0w/.local/bin/bbin:1940:3
babashka.bbin.c

but bbin ls </dev/null >/tmp/ba works just fine

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions