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

weird interaction between repl and console input #3442

Closed
scabug opened this issue May 16, 2010 · 10 comments
Closed

weird interaction between repl and console input #3442

scabug opened this issue May 16, 2010 · 10 comments

Comments

@scabug
Copy link

scabug commented May 16, 2010

Attempting to use java.io.Console to read a password throws the repl into weirdness and only reads in a single character:

Welcome to Scala version 2.8.0.RC1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_17).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val console = System.console
console: java.io.Console = java.io.Console@12781046

scala> console.readPassword("Enter pw: ")
Enter pw: 
res0: Array[Char] = Array(p)

scala> rreess00((00))

res1: Char = p

scala> ::qquuiitt

Also, when calls to the regular readLine method are made, the characters are not echoed to the screen:

Welcome to Scala version 2.8.0.RC1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_17).
Type in expressions to have them evaluated.
Type :help for more information.

scala> Console.readLine("Enter stuff: ")
Enter stuff: res0: String = stuff

scala> :quit

Both of these work as expected if run as a script instead of from the repl. Scripts are attached.

@scabug
Copy link
Author

scabug commented May 16, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3442?orig=1
Reporter: @eengbrec
Attachments:

  • test.scala (created on May 16, 2010 3:16:00 PM UTC, 107 bytes)
  • test2.scala (created on May 16, 2010 3:16:19 PM UTC, 53 bytes)

@scabug
Copy link
Author

scabug commented May 16, 2010

@eengbrec said:
reads a password from the console

@scabug
Copy link
Author

scabug commented May 16, 2010

@eengbrec said:
regular console input via readLine

@scabug
Copy link
Author

scabug commented May 16, 2010

@paulp said:
This works, and as it does, realistically I'm unlikely to pursue this without a very specific diagnosis as to how it should be changed.

Console.withIn(System.in) { Console.readLine("Enter stuff:") }

@scabug
Copy link
Author

scabug commented May 16, 2010

@eengbrec said:
Ok, I did a little research... The Jython repl essentially behaves like the Scala repl. Using java.io.Console essentially won't work, and input is not echoed when reading from stdin using a BufferedReader.

JRuby's repl succeeds at echoing characters that are going into a BufferedReader, but fails the same as Jython and Scala with java.io.Console.

I don't think this is high priority, Merely mildly annoying, and mostly annoying because I didn't know of the limitation. I won't get any heartburn if it's marked as wontfix or given a very low priority.

@scabug
Copy link
Author

scabug commented Aug 28, 2010

Steven Bethard (bethard) said:
As far as I can tell, the Console.withIn thing doesn't solve the java.io.Console problem at all - you can still only type a single character:

scala> Console.withIn(System.in) {System.console.readLine("username: ")}
username: res10: java.lang.String = a

scala> bcde

This matters less for System.console.readLine since you can always use Console.readLine instead, but what are you supposed to use instead of System.console.readPassword?

@scabug
Copy link
Author

scabug commented Aug 28, 2010

@paulp said:
Replying to [comment:4 bethard]:

As far as I can tell, the Console.withIn thing doesn't solve the java.io.Console problem at all - you can still only type a single character:
{code}
scala> Console.withIn(System.in) {System.console.readLine("username: ")}

That's because that's not the code which was given.

This matters less for System.console.readLine since you can always use Console.readLine instead

That's the code which was given.

but what are you supposed to use instead of System.console.readPassword?

What you're supposed to do, if that is not a rhetorical question, is fix it and submit a patch. It's not like the ticket was closed "everything's fine".

@scabug
Copy link
Author

scabug commented Aug 28, 2010

Steven Bethard (bethard) said:
I guess I misunderstood the comment "this works". I thought the ticket was moved to low priority because the Console.withIn thing was being proffered as a workaround (but it's not). Sorry for the confusion.

Anyway, here's a workaround (using jline which appears to be included by scala already):

scala> new jline.ConsoleReader().readLine('*')
******
res0: java.lang.String = abdefg

@scabug
Copy link
Author

scabug commented Jun 25, 2013

@adriaanm said:
Closing since there's a workaround. We're moving to vanilla jline, btw: #7604

@scabug scabug closed this as completed Jun 25, 2013
@scabug
Copy link
Author

scabug commented Aug 31, 2013

@gourlaysama said:
In 2.10+, this workaround doesn't seem to work, but you can just get into power mode and then

scala> repl.in.readLine("enter something: ")
enter something: hello world
res0: String = hello world

If only that readLine method was used as the default readLine in the REPL...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant