Skip to content

Instantly share code, notes, and snippets.

@baskeboler
Forked from brake/clipboard.clj
Last active April 11, 2021 01:21
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save baskeboler/7d226374582246d28b25801e28e18216 to your computer and use it in GitHub Desktop.
Write pretty printed Clojure data structures to the clipboard
(ns clipboard.core
(:require [fipp.edn :as fipp])
(:import (java.awt.datatransfer DataFlavor Transferable StringSelection)
(java.awt Toolkit)
(java.io StringWriter))
(defn get-clipboard
"get system clipboard"
[]
(-> (Toolkit/getDefaultToolkit)
(.getSystemClipboard)))
(defn slurp-clipboard
"get latest string from clipboard"
[]
(when-let [^Transferable clip-text (some-> (get-clipboard)
(.getContents nil))]
(when (.isDataFlavorSupported clip-text DataFlavor/stringFlavor)
(->> clip-text
(#(.getTransferData % DataFlavor/stringFlavor))
(cast String)))))
(defn spit-clipboard
"write string s to clipboard"
[s]
(let [sel (StringSelection. s)]
(some-> (get-clipboard)
(.setContents sel sel))))
(defn with-out-clipbaord
"pretty prints a data structure into the clipboard using fipp library"
[d]
(let [wr (java.io.StringWriter.)]
(fipp/pprint d {:writer wr})
(spit-clipboard (.toString wr))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment