Skip to content

Instantly share code, notes, and snippets.

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)
( StringWriter))
(defn get-clipboard
"get system clipboard"
(-> (Toolkit/getDefaultToolkit)
(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"
(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"
(let [wr (]
(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