Skip to content

Instantly share code, notes, and snippets.

@justone
Last active November 2, 2020 18:16
Show Gist options
  • Save justone/d1fad9a325ba60c816f7960919f98c4b to your computer and use it in GitHub Desktop.
Save justone/d1fad9a325ba60c816f7960919f98c4b to your computer and use it in GitHub Desktop.
Script to print out all the visible URLS in your tmux pane.
#!/usr/bin/env bb
(ns urls
(:require
[clojure.tools.cli :refer [parse-opts]]
[clojure.java.shell :as sh]
[clojure.string :as string]
))
(def cli-options
[["-l" "--last"]])
(def url-re
"From: https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url"
#"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)")
(defn get-tmux-pane
[]
(let [{:keys [out exit]} (sh/sh "tmux" "capture-pane" "-p")]
(when (zero? exit)
out)))
(defn find-urls
[text]
(when text
(->> (string/split text #"\s")
(filter #(re-matches url-re %)))))
(defn -main [& args]
(let [parsed (parse-opts args cli-options)
{:keys [options]} parsed
data (get-tmux-pane)]
(cond->> (find-urls data)
(:last options) (take 1)
:always (run! println))))
(ns user (:require [urls])) (apply urls/-main *command-line-args*)
@justone
Copy link
Author

justone commented May 12, 2020

Can be used with fzf to filter, and then piped to a url opener (like open on MacOS).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment