-
-
Save baskeboler/7d226374582246d28b25801e28e18216 to your computer and use it in GitHub Desktop.
Write pretty printed Clojure data structures to the clipboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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