This is still a work in progress; testing needed and very welcome.
Why
This helps with items 4 and 5 on this list:
We will combine the two, because inline code blocks should go away when they have fulfilled their duty, lest they obstruct the conversational context surrounding them.
How to do better
Let’s build us a little helper script that runs a command, collects its output, wraps it in BBCode for better readability and copies it to the clipboard, all in one fell swoop. After running it, all that’s left to do is to paste the text in the Discourse editor on an empty line.
Installation
Since there is nowhere to download this (yet?), here’s how to install it.
- We need some helper package(s) to make the script talk to the clipboard:
sudo apt install wl-clipboard xsel - Open the following details section, entitled `cat cmd2bb`, and copy the contained code block (note the clipboard icon in the top right corner of code blocks):
cat cmd2bb
What's a clipboard?
It’s the invisible copy/paste buffer, where data goes when you “copy” something, e.g. by pressing the the keyboard shortcut (
Ctrl-C) or by clicking the little icon in the top right corner of code blocks, which looks like a sheet of paper on a clipboard ;). It’s a small buffer to transfer data from one program to another. You’ve most probably been using it for a long time, but didn’t know it by that name.For the Pros: Fast Forward to 5)
dest=/usr/local/bin/cmd2bb wl-paste | sudo tee "$dest" sudo chmod +x "$dest" unset destExplanation
And here is the script, demonstrating itself, for it was used to produce this output with the command in the “Details” summary. As you can see, it created a “Hide Details” block with a “Preformatted text” (code) block inside it, and the “Summary” of the former will always be
`<command> <all> <arguments>`.For transparency, it also echos the raw command output to the terminal (stderr); just as an additional safety measure, so one can check if there’s any sensitive info in it.
CAUTION: Do not mark, select and/or copy that text manually, otherwise you will undo all the hard work ofcmd2bb.#!/bin/sh -e if [ "$XDG_SESSION_TYPE" = wayland ]; then cb_copy () { wl-copy; } else cb_copy () { xsel -b; } fi pr_cmd () { # restore quotes around args with spaces for arg in "$@"; do read -r _ rem <<-EOT $arg EOT q=${rem:+\'} printf '%s' "$q$arg$q" i=$((i+1)) [ $i -lt $# ] && printf ' ' done unset arg i rem q } cmd=$(pr_cmd "$@") cb_copy <<EOBB [details=$cmd] [code] $("$@" 2>&1 | tee /dev/stderr) [/code] [/details] EOBB - Open a text editor, e.g.
sudo nano /usr/local/bin/cmd2bb - Paste the code there, save the file (requires root privileges; note the `sudo` in step 2) and exit the editor.
- Make file executable:
sudo chmod +x /usr/local/bin/cmd2bb - Done.
Demo
My first command, when something goes wrong, is always journalctl --no-hostname --boot --priority=err. So now we can just run cmd2bb journalctl --no-hostname --boot --priority=err, which puts the output into the clipboard, go back to Discourse and paste it on a new line:
journalctl --no-hostname --boot --priority=err
<redacted>
Apr 07 09:19:12 sudo[724142]: pam_unix(sudo:auth): conversation failed
Apr 07 09:19:12 sudo[724142]: pam_unix(sudo:auth): auth could not identify password for [peter]
It even works with pipelines; just run them as command argument of sh:
sh -c 'shellcheck cmd2bb && echo PASSED | cat'
PASSED
![]()
For reference, here is the discussion thread which motivated this.