Copy and Copy JSON: essential Listener hacks.
Something I’ve really wanted in the Listener for a while is the ability to right-click and copy a string to the clipboard. I’ve been working around that by having my own “>clipboard” word that places a single string argument on the clipboard — extremely handy for debugging long HTTP responses which get truncated at the right-side of the window.
: >clipboard ( str — )
clipboard get set-clipboard-contents ;
This is from Factor’s useful ui.clipboards
vocab, which is the most concise and least ugly way I’ve seen to handle clipboards in a cross-platform GUI. clipboard
is a global reference to the current clipboard object whose methods are overridden to handle the dirty details of the current platform’s clipboard.
Along came this useful post about adding an ‘open url’ command for URLs in the listener, and I saw a path to my goal: ui.operations
and the define-operation
word.
Combined with unparse
to get a string representation of an object, this adds a “Copy” command to the Listener’s context menu for all objects:
: copy ( obj — )
unparse >clipboard ;
[ drop t ] \ copy H{ } define-operation
In this brave new Web 2.0 world, it’s dangerous to go alone! Take this JSON with you*:
: copy-json ( obj — )
>json >clipboard ;
[ string? not ] \ copy-json H{ } define-operation
Load up the vocab (or paste those definitions in the Listener), and voilá — the GUI is instantly extended with new context menu options:
As usual, this is all available on GitHub
*(Sorry for the Zelda reference, just spent a happy weekend playing a big chunk of the awesome LD20 48-hour game compo entries)