Fork me on GitHub

Can't "google" how to support pipe to a file script. The file is ~/sshbb.clj:

#!/usr/bin/env bb
; some clojure code here; can't get *input*  or *command-line-args*
; (println "*input*:" *input*)
(println "*command-line-args*:" *command-line-args*)
And I want to use it like: echo 1 | ~/sshbb.clj Is it possible?


It seems the problem was that I was using ns:

#!/usr/bin/env bb
(ns sshbb
    [babashka.process :as p]
    [clojure.string :as str]
    [ :refer [sh]]))

(println *input*)
Without ns form it works


*input* is only for short one-liners on the command line

🙌 1

Works! Thank you. Sorry I wasn't able to get it from the babashka book

Björn Ebbinghaus13:12:17

Hey, I have a script that reads multiple CSV files from different people with different OSs. Now the files have different char encodings and slurp doesn't detect all of them correctly. Special symbols like (äöü) get lost. Can someone point me to a robust way to read those files?


@U4VT24ZM3 slurp accepts an :encoding option, but I guess you should first detect the encoding in the first place?


but if they are UTF-8 you would do it with (slurp "file.csv" :encoding "UTF-8")

Björn Ebbinghaus13:12:26

I don't know the encodings beforehand. file -I returns charset=unknown-8bit for a file which is Latin-1. I wanted to ask if there is already a known, tested way before I cobble something together myself. 🙂


AFAIK there is no easy solution for this in bb today. Defaulting to UTF-8 surely would make things easier ;)

Björn Ebbinghaus14:12:17

Does bb have a tool like file -I where I can try to get a file encoding, or do I have to use babashka/process ?


bb doesn't have anything for this, unless it's built into clojure or the JVM


Perhaps it's something you could do in user space but I don't know how difficult this is


shelling out to file seems best


Hmm, perhaps @UCFG3SDFV knows if this can be done easily using binf?

Adam Helins14:12:54

If the file embeds a "mime" then one could use BinF to read those raw bytes and interpret the encoding. However, there is no automatic detection (probably outside the scope of a low-level bin library :thinking_face:). (


Yes, what I sort of was getting at: could one implement file using binf? Probably not trivial


It sounds like it's doable to port this to a Clojure library: EDIT: hmm, this one seems really limited

Adam Helins14:12:50

One way would be to open the file as a memory mapped byte buffer (e.g. ) Then you can use the BinF API to read from it but you have to now where to look at and how to interpret those bytes.

👍 1

Please, help to rewrite the script. Details inside the thread.


I took script as an example. And this works:

(ns sshbb
  (:import java.lang.ProcessBuilder$Redirect)
    [babashka.process :as p]
    [clojure.string :as str]))
(defn connect [cmd]
  (let [pb (doto (ProcessBuilder. cmd)
             (.redirectOutput ProcessBuilder$Redirect/INHERIT)
             (.redirectError ProcessBuilder$Redirect/INHERIT))
        proc (.start pb)]
    (-> (Runtime/getRuntime)
        (.addShutdownHook (Thread. #(.destroy proc))))
    (.waitFor proc)))

(-> args input->map build-cmd connect)
I want to rewrite the same script without interop, like:
(defn connect [cmd]
  @(p/process cmd {:inherit true
                   :out :inherit
                   :shutdown p/destroy-tree}))
But can't prevent it from termination.


I think this should do the same?

@(p/process {:err :inherit :out :inherit} "cmd to run")
the deref should block til the process completes


Use (babashka.process/shell "cmd to run")


cmd is a shell forwarding; I think it was termination because of the :inhetit :true flag


This works:

(defn connect [cmd]
  @(p/process cmd {:out :inherit}))

👍 1

Hi all! I was trying to find a way to make nice looking command prompts with clojure within a babashka script - Similar in functionality to . I have been searching for a while but were unable to find anything. Can you give me a hint? Does that even exist?


I'm not aware of any but note that you can use Node.js libraries with #C029PTWD3HR


If you want terminal coloring, there are various libraries for this that are compatible with bb


Thanks! I was thinking about making the hop over to nbb. I guess it won’t hurt 😄


For example this library allows you to do colored printing:


Ah yes, thanks. Coloring was not so much the problem. I wanted to find something to give an interactive user selection usable by arrow keys. When there is nothing like that compatible with bb then I better change over to nbb


It depends on the dialog command being available on your system


Thanks for the input. I rather go with nbb as I don’t see any downsides of the switch and such popup dialogs are in my opinion rather interrupting to the user

👍 1

@U6JS7B99S the closest thing i can think of is maybe that's good enough?


Is there a way to include deps information inside the executable babashka file itself?


do you mean to load dependencies dynamically in a standalone script?


yes, this is possible with add-deps:


another noob question, but any suggestions on editor support/completion?


it would be sweet if included libs completed


inf-clojure gives me a little bit, but my config is probably not set up for completion


Any editor with nREPL support