Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[apps/cli] Support reading from stdin #249 #250

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

LuoZijun
Copy link

@LuoZijun LuoZijun commented Jan 9, 2019

Resolve #249


This change is Reviewable

@behnam behnam changed the title Support for stdin #249 [apps/cli] Support reading from stdin #249 Jan 9, 2019
Copy link
Member

@behnam behnam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, @LuoZijun!

Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @LuoZijun)


apps/cli/src/bin/unic-echo.rs, line 151 at r1 (raw file):

        let done_clone = done.clone();

        let handler = thread::spawn(move ||{

Could you elaborate on why using a separate thread to read from stdin here?

AFAIU, we should be able to do the same thing with non-blocking IO without spawning a thread. Or, if we want to go with blocking IO, still going with direct read in the main thread seems to be reasonable for such a small CLI tool.

What do you think?

@behnam behnam added enhancement Enhancements to existing features A: apps Applications & Examples labels Jan 9, 2019
@LuoZijun
Copy link
Author

@behnam

If we support accepting parameters from the basic input,
the main thread will still blocked when this $ ./unic-echo appears.

Of course, if everyone thinks this is not going to bother the user,
the block style approach will be very straightforward.
and i am happy to change to a synchronous way.

Use case:

  • ./unic-echo "STRING ..."
  • cat hello.txt | ./unic-echo or ./unic-echo < hello.txt
  • ./unic-echo

@behnam
Copy link
Member

behnam commented Jan 13, 2019

Oh, I think I see what you mean.

I think blocking reading from stdin should only happen if we explicitly want to read from stdin.

One common condition for reading from stdin is when there are no (non-optional) arguments passed to the CLI. (Like unix's echo without any file-names passed to it.)

Another common option is when the argument is one HYPHEN-MINUS ("-"). In this case, the position of the HYPHEN-MINUS can tell us if the stdin text should be placed before or after any other regular argument.

If none of them applies (there's at least one argument passed to the CLI and none of the is "-") the stdin is not even (blocking) opened.

So, all of these cases would result in "Hello world" string to be echoed/inspected:

  • unic-echo "Hello world" and
    unic-inspect "Hello world"

  • echo "Hello world" | unic-echo and
    echo "Hello world" | unic-inspect

  • echo "Hello world" | unic-echo - and
    echo "Hello world" | unic-inspect -

  • echo "ignored" | unic-echo "Hello world" and
    echo "ignored" | unic-inspect "Hello world"

  • echo "world" | unic-echo "Hello " - and
    echo "world" | unic-inspect "Hello " -

What do you think?

@LuoZijun
Copy link
Author

@behnam

I think that's great.

BTW: Actually is --, not - :)

@LuoZijun
Copy link
Author

LuoZijun commented Feb 8, 2019

@behnam ping ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A: apps Applications & Examples enhancement Enhancements to existing features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants