Wednesday, February 8, 2023
HomeiOS DevelopmentPiping stdout and stderr to Preview — Erica Sadun

Piping stdout and stderr to Preview — Erica Sadun


Some time again, I wrote about how useful it was to redirect a person web page into Preview. This lets you preserve the person web page open, search it, and customarily have a greater person expertise than fighting extra (or much less) to navigate by way of the knowledge supplied there.

man -t apropos | open -fa Preview

Not too long ago, somebody requested me about extra trendy command line interplay, particularly, instructions that use --help or comparable to supply their documentation. Might that info be opened in Preview as nicely.

So I placed on my pondering hat and set to work. The primary command line utility I made a decision to work with was screencapture as a result of I’ve been utilizing it pretty closely over the previous couple of days. Nevertheless, it seems that Apple did not construct in an precise assist system past man. It was a poor option to attempt to use to render however I made a decision to maintain plugging away at it as a result of I needed to have the ability to pipe each stdoutand stderr to Preview.

What I got here up with appeared one thing like this, multi function line after all:

bash -c "screencapture -? &> 
    $TMPDIR/previewrendertext.txt; 
    /usr/sbin/cupsfilter -i textual content/plain 
        -D $TMPDIR/previewrendertext.txt 
        2> /dev/null | 
    open -fa Preview"

This all depends on cupsfilter, which may convert a file of textual content to a printable kind, which simply occurs to be readable by Preview as a PDF.

I’m doing fairly a little bit of conglomeration, becoming a member of the stderr and stdout streams utilizing &> and saving them into my Mac’s $TMPDIR. That file is cleaned up by the -D possibility from cupsfilter.

I additionally am eradicating the incessant debug messages from cupsfilter by redirecting them to /dev/null earlier than opening the print output in Preview.

Please observe that I’m nonetheless utilizing tcsh/zsh over bash on my fundamental system, in order that definitely impacts issues. Since I wanted a bit of of the bash nuance, I made a decision to run all of it squished as a single -c command. (I’m positive if I spent sufficient time, I might do all of it in csh however I actually didn’t wish to spend that point.)

As you’ll be able to see within the earlier screenshot, an older utility meant for man output doesn’t actually look all that sizzling shoved into Preview by way of cupsfilter, particularly with line lengths. There’s additionally no good groffing and troffing to make every part fairly, the way in which you get with man:

So how might would this kludge work with a contemporary command-line app, corresponding to these produced utilizing the Swift Argument Parser (https://github.com/apple/swift-argument-parser)? First, I constructed a utility that may let me run any command (nicely, as long as it was correctly quoted) with out having to sort all the small print out every time I ran it:

#! /bin/bash

$@ &> $TMPDIR/previewrendertext.txt ; /usr/sbin/cupsfilter -i textual content/plain -D $TMPDIR/previewrendertext.txt 2> /dev/null | open -fa Preview

This allowed me to name preview "now --help" to redirect the usual assist message from my now utility (https://github.com/erica/now)  to Preview. Yeah, initially I needed to only pipe stuff into it however I couldn’t determine how one can get the stderr and the stdout piped collectively right into a single stream, not to mention convert them right into a file kind as a result of cupsfilter doesn’t know or do pipes.

It’s fairly readable and well-formatted as a result of computerized configuration that the Swift Argument Parser supplies from my code however it simply feels, , very very plain.

So I went forward and tried to see what would occur if I groffed it up a bit of by passing it by way of /usr/bin/groff -Tps -mandoc -c as a substitute of utilizing cupsfilter:

bash -c "now --help &> 
    $TMPDIR/previewrendertext.txt; 
    /usr/bin/groff -Tps -mandoc -c 
    $TMPDIR/previewrendertext.txt" | 
    open -fa preview

And it’s…fairly meh. I attempted mandoc, mdoc, me, mm, ms, and www codecs. All of them got here out the identical, and not one of the SAP tabs actually labored. I believe it appears much more “manny” than the straight printout however the indentation actually bugged:

I made a decision to cease at about this level as there’s actually a time when additional effort simply isn’t value additional funding — so I might throw it on the market and see if this was of curiosity to anybody else.

Let me know.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments