Fork me on GitHub
#babashka
<
2022-12-19
>
Dumch13:12:54

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?

Dumch13:12:57

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

borkdude13:12:20

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

🙌 1
1
1
Dumch13:12:44

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?

borkdude13:12:35

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

borkdude13:12:07

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. 🙂

borkdude13:12:34

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 ?

borkdude14:12:52

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

borkdude14:12:29

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

borkdude14:12:53

shelling out to file seems best

borkdude14:12:47

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:). (https://github.com/helins/binf.cljc)

borkdude14:12:55

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

borkdude14:12:47

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

Adam Helins14:12:50

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.

👍 1
Dumch14:12:26

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

Dumch14:12:30

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.

lispyclouds14:12:37

I think this should do the same?

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

1
borkdude14:12:59

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

Dumch15:12:21

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

Dumch15:12:02

This works:

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

👍 1
alpox22:12:49

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?

borkdude22:12:45

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

borkdude22:12:04

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

alpox22:12:51

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

borkdude22:12:18

For example this library allows you to do colored printing: https://ioavisopretty.readthedocs.io/en/stable/ansi.html

alpox23:12:43

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

borkdude23:12:49

It depends on the dialog command being available on your system

alpox23:12:33

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
lispyclouds07:12:37

@U6JS7B99S the closest thing i can think of is https://github.com/escherize/bask maybe that's good enough?

d._.b23:12:57

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

borkdude23:12:43

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

borkdude23:12:09

yes, this is possible with add-deps: https://book.babashka.org/#_add_deps

d._.b23:12:41

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

d._.b00:12:37

it would be sweet if included libs completed

d._.b01:12:38

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

borkdude07:12:33

Any editor with nREPL support