-
-
Save usametov/c34b19dae7f13a4626c1549fddc1d319 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 [clojure.pprint :refer [pprint]]) | |
(: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)))) | |
; an alias for spit | |
(def str->clipboard spit-clipboard) | |
(defn pprint-data-to-clipbaord | |
"pretty prints a data structure into the clipboard" | |
[d] | |
(let [wr (java.io.StringWriter.)] | |
(pprint d wr) | |
(str->clipboard (.toString wr)))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment