So, the pipe "|" and redirect ">" are shell concepts, so your first and
second one fail because those aren't understood by the Factor process
launcher.

You can, however, run this as a pipeline:

    :: man-page>txt ( cmd -- result )
        { { "man" cmd } { "col" "-bx" } [ contents ] } run-pipeline
        last utf8 decode ;

If you want to write that to a file, you can either do that in a second
step:

    : write-man-page ( cmd path -- )
        [ man-page>text ] [ utf8 set-file-contents ] bi* ;

Or, you can make that the last element of the pipeline, ignoring the exit
status of the pipeline (which might be useful for error checking):

    :: man-page>txt ( cmd path -- )
        {
            { "man" cmd }
            { "col" "-bx" }
            [ path utf8 [ [ write ] each-block ] with-file-writer t ]
        } run-pipeline drop ;

Some other ideas for you.  You could write words for each step, which is
maybe less performant than piping one into the next, making sure to dispose
of the output stream in ``col`` to trigger an EOF:

    : man ( cmd -- text )
         { "man" } swap suffix utf8 [ contents ] with-process-reader ;

    : col-bx ( text -- text' )
        { "col" "-bx" } utf8 [
           write flush output-stream get dispose contents
        ] with-process-stream ;

    : man-page>txt ( cmd path -- )
        [ man col-bx ] dip utf8 set-file-contents ;

If anyone feels like working on a project, making pipelines and io.launcher
in general more friendly or better documented would be great. :-)

Best,
John.



On Wed, Nov 30, 2016 at 6:37 AM, CW Alston <cwalsto...@gmail.com> wrote:

> Hi -
> I'm trying to whip up a Factor utility to export unix manpages to
> plain text files, with proper formatting. I found this command-line
> snippet which works nicely in the macOS terminal:
>
>     man dyld | col -bx > /Users/cwalston/factor/man_dyld.txt
>
> -But I'm having no luck invoking this in Factor code. The following attempt
> fails with error code 1, & nothing written to the file:
>
> :: man-page>txt ( cmd path -- )
>     [  "man" , cmd , "|" , "col" , "-bx" , ">" , path , ]
>     { } make " " join clone try-process ;
>
> -Trying with run-pipeline doesn't do, with or without "|"; result is { 0 1
> }:
>
> ! "dyld" "/Users/cwalston/factor/man_dyld.txt" man-page>txt
> :: man-page>txt ( cmd path -- result )
>      [ "man" , cmd , ] { } make
>      [ "col" , "-bx" , ">" , path , ] { } make
>      [ " " join ] bi@ 2array clone
>      run-pipeline ;
>
> -A third approach without 'col -bx' retains the troff formatting
> gobbledy-gook,
> (N\bNA\bAM\bME\bE for NAME, etc):
>
> ! "dyld" get-man-lines
> :: get-man-lines ( cmd -- results )
>     V{ } clone :> man-lines
>     "man" cmd " " glue    ! ( -- string )
>
>     V{ } clone [ push ] keep     ! ( -- seq )
>        [ [ [ readln dup ]        ! ( -- str/f str/f )
>            [ >string
>              man-lines push
>            ] while  ! ( -- f )
>          ] loop man-lines
>        ] swap [ push ] keep clone    ! ( -- { string quot } )
>     run-pipeline second ;            ! ( -- vector ) file lines, w/ garbage
>
> I've tried several variations of these 3 approaches, to no avail.
> Any suggestions?
>
> ~cw
>
> --
> *~ Memento Amori*
>
> ------------------------------------------------------------
> ------------------
>
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
>
------------------------------------------------------------------------------
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to