Fork me on GitHub
#babashka
<
2022-09-14
>
teodorlu13:09:50

good afternoon 🙂 I have a tiny bookmarks-as-json system built with json, python and rofi. I'm considering rewriting the Python in Clojure. Have anyone of you shelled out to rofi from a Babashka script before? --- A quick search gave me ralphie.rofi[1], which was kind of interesting. I'm also including the Python source. It's a quick and dirty script, but it works. [1]: https://github.com/russmatney/ralphie/blob/master/src/ralphie/rofi.clj

🎉 1
teodorlu19:09:18

Hi again 🙂 I'm trying to shell out to fzf with babashka.process/process. Would appreciate some help, details in thread.

teodorlu19:09:24

;; I'm able to shell out to grep with both babashka.process and bash.

;; bash:
;;
;;   $ grep no <<EOF
;;   no u
;;   lol u
;;   EOF
;;
;; Press enter, and watch:
;;
;;   no u

;; translated to babashka.process:
(let [in-str
"no u
lol u"]
  (-> @(process/process ["grep" "no"]
                        {:in in-str
                         :out :string})
      :out))
;; ... works as expected.

;; I'm able to shell out to fzf with bash:
;;
;;   $ fzf <<EOF
;;   no u
;;   lol u
;;   EOF
;;
;; ... here we can select the right candidate interactively ...
;;
;;   lol u

;; But when I try with babashka.process, I'm not able to select anything interactively:
(let [in-str
"no u
lol u"]
  (-> @(process/process ["fzf"]
                        {:in in-str
                         :out :string})
      :out))

borkdude19:09:51

I think you need to do :err :inherit

1
teodorlu19:09:29

You're right!

teodorlu19:09:11

Any idea why that did the trick? I didn't expect that "inheriting stderr" would fix fzf interactivity.

borkdude19:09:04

this is because fzf prints its menu to stderr

👍 1
teodorlu19:09:03

Never knew that before now. But it makes sense, considering how you can control both stdin and stdout. Thanks!

teodorlu19:09:02

TIL tiny fzf wrapper for babashka scripts (thanks @borkdude)

(defn fzf [options]
  (-> @(process/process ["fzf"] {:in (str/join "\n" options)
                                 :out :string
                                 :err :inherit})
      :out
      str/trim))

(let [choice (fzf ["option A" "option B"])]
  (println "You picked:" choice))

;; Prints
;;
;;   You picked: option B

1
🎉 1
🆒 1
nice 2
👀 1
Alys Brooks20:09:40

Is there a way to have universal babashka tasks? I'm thinking of creating an alias for bb -f ~/bb.edn or similar, but am I missing a better option?

borkdude20:09:44

@actuallyalys_slack Maybe you're looking for #babashka-bbin which allows you to install scripts in your system so you can call them from anywhere

1
Alys Brooks20:09:51

Ahh, I had missed that babashka project. 🙂 That might work well, if I create a little babashka project to house my little babaska snippets.

🎉 1
borkdude20:09:27

Another option is to use bb --config ~/bb.edn

borkdude20:09:28

btw, I don't recommend to put a bb.edn file in ~ : clojure-lsp might mistake it for a project root :)

borkdude20:09:45

so maybe: bb --config ~/.babashka/bb.edn or so

teodorlu20:09:47

I wrote a small thing. Bink: Bookmarks are just data idea: bookmarks are just data, and should be treated as such. To get started, we need a tool to open bookmarks from files. That's bink. https://play.teod.eu/bink/ Powered by babashka, babashka.process, fzf and EDN (all great tools) Comments/questions welcome 🙂 --- You can install bink with #babashka-bbin (by @rads):

$ bbin install 
$ bink

clj 1
teodorlu20:09:53

here's the whole source:

(ns bink
  (:require [babashka.fs :as fs]
            [clojure.edn :as edn]
            [babashka.process :as process]
            [clojure.string :as str]))

(defn providers []
  ;; Per 2022-09-14, we only support EDN links.
  (let [support-json false]
    (if support-json
      (fs/glob (fs/expand-home "~/.config/bink/provider") "*.{edn,json}")
      (fs/glob (fs/expand-home "~/.config/bink/provider") "*.edn"))))

(defn all-links []
  (mapcat (fn [p]
            (:links (edn/read-string (slurp (str p)))))
          (providers)))

(defn fzf [options]
  (-> @(process/process ["fzf"] {:in (str/join "\n" options)
                                 :out :string
                                 :err :inherit})
      :out
      str/trim))

(defn firefox [url]
  (process/process ["firefox" url]))

;; get links
(let [links (all-links)
      links-by-title (into {}
                           (for [l links]
                             [(:title l) l]))
      _ (assert (= (count links) (count links-by-title)) "Duplicate titles are not allowed")
      choice-title (fzf (map :title links))
      choice (get links-by-title choice-title)
      _ (firefox (:href choice))
      ])

borkdude20:09:00

Can we install it with bbin?

teodorlu20:09:55

Not sure, haven't had the chance to provide a bbin-installable script before. I can have a look.

borkdude20:09:34

This will be a good test :)

teodorlu21:09:36

Quick status: 1. Got bbin installed. I'm using GRML ZSH config (https://grml.org/zsh/), so I had to read install scripts to understand what they were doing in order to understand how to install bbin. 2. Had to wait a while for a full system update. bbin required a later babashka version than the one I had installed 3. Looked for docs on how to make my script available for install with bbin, coudn't find any 4. Just tried bbin install , which worked! So to install with bbin:

$ bbin install 

borkdude21:09:21

^ @rads some feedback

borkdude21:09:52

@U3X7174KS I got:

Visiting /Users/borkdude/.config/bink/provider failed

borkdude21:09:05

probably I should create .config/bink myself?

teodorlu21:09:25

yeah, you populate the EDN files yourself.

teodorlu21:09:40

Gimme a sec

borkdude21:09:46

it works :)

borkdude21:09:10

except that shelling out to firefox didn't work ;)

borkdude21:09:22

perhaps you should just use browse-url?

borkdude21:09:56

anyway, the bbin install worked, but the docs should probably be more clear than just give brief examples without any comments

borkdude21:09:08

(I was referring to the bbin docs)

👍 1
teodorlu21:09:54

Yeah, super rough edges so far. I tried browse-url first, but I couldn't get it working. I assumed it wasn't included in babashka, but perhaps it is?

borkdude21:09:35

it is

👀 1
borkdude21:09:54

but with a different implementation

borkdude21:09:27

Try this:

bb -e '(clojure.java.browse/browse-url "")'

teodorlu21:09:43

works :thumbsup:

teodorlu21:09:41

Not sure how updates / reinstallation works with bbin. But if you get the latest bink on master now, you'll get browse-url rather than firefox.

borkdude21:09:24

I think installing again should overwrite the old one

borkdude21:09:42

yep, worked :)

borkdude21:09:08

@U3X7174KS This is super cool.

🙌 1
borkdude21:09:23

And @rads: the installation via bbin worked super smoothly. Great job both.

teodorlu21:09:48

@borkdude thanks a lot for the feedback 👍 Since your first test run, I've improved the getting started experience. bink should now give some helpful output if there are no links.