This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-12-19
Channels
- # adventofcode (52)
- # babashka (47)
- # beginners (13)
- # clojure (36)
- # clojure-belgium (1)
- # clojure-europe (14)
- # clojure-nl (1)
- # clojure-norway (14)
- # clojurescript (2)
- # clojutre (9)
- # cursive (12)
- # datomic (3)
- # deps-new (3)
- # emacs (12)
- # fulcro (5)
- # guix (1)
- # honeysql (7)
- # introduce-yourself (1)
- # jobs (1)
- # kaocha (8)
- # lsp (5)
- # membrane (5)
- # mount (7)
- # nbb (5)
- # nrepl (2)
- # off-topic (60)
- # polylith (9)
- # reclojure (2)
- # reitit (8)
- # ring (17)
- # shadow-cljs (4)
- # spacemacs (31)
- # sql (7)
- # timbre (3)
- # xtdb (15)
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
(:require
[babashka.process :as p]
[clojure.string :as str]
[clojure.java.shell :refer [sh]]))
(println *input*)
Without ns form it works@UL05W6AEM Use (slurp *in*)
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?
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 ;)
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
?
Perhaps it's something you could do in user space but I don't know how difficult this is
Hmm, perhaps @UCFG3SDFV knows if this can be done easily using binf?
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:). (https://github.com/helins/binf.cljc)
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: https://github.com/joeky888/fil/blob/master/main.go EDIT: hmm, this one seems really limited
One way would be to open the file as a memory mapped byte buffer (e.g. https://github.com/helins/binf.cljc#creating-a-view-over-a-memory-mapped-file-jvm ) 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.
I took https://clojurians.slack.com/archives/CLX41ASCS/p1595878111084500 script as an example. And this works:
(ns sshbb
(:import java.lang.ProcessBuilder$Redirect)
(:require
[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 completescmd
is a shell forwarding; I think it was termination because of the :inhetit :true
flag
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 https://github.com/terkelg/prompts . I have been searching for a while but were unable to find anything. Can you give me a hint? Does that even exist?
If you want terminal coloring, there are various libraries for this that are compatible with bb
For example this library allows you to do colored printing: https://ioavisopretty.readthedocs.io/en/stable/ansi.html
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
Maybe this one also helps: https://www.pixelated-noise.com/blog/2022/12/09/dialog-and-babashka/index.html
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
@U6JS7B99S the closest thing i can think of is https://github.com/escherize/bask maybe that's good enough?
yes, this is possible with add-deps
:
https://book.babashka.org/#_add_deps