Eugleo / magic-racket

The best coding experience for Racket in VS Code
https://marketplace.visualstudio.com/items?itemName=evzen-wybitul.magic-racket
GNU General Public License v3.0
202 stars 28 forks source link

User Input not working #35

Closed bevo009 closed 3 years ago

bevo009 commented 3 years ago

Environment

Error message

Insert a number: ; >: contract violation ; expected: real? ; given: #f ; argument position: 2nd ; [,bt for context]

Additional context

User Input seems to not work in the Magic extension This is on Windows, and also on WSL 2 Ubuntu The code below runs correctly in Dr Racket and Emacs

#lang racket

(define (inquire-user number)
  (display "Insert a number: ")
  (define guess (string->number (read-line)))
  (cond [(> number guess) (displayln "Too low") (inquire-user number)]
        [(< number guess) (displayln "Too high") (inquire-user number)]
        [else (displayln "Correct!")]))

(displayln "Guess a number between 1 and 100")
(inquire-user (random 1 101))

Additional problems:

[Win only], the Load File in REPL button only opens the repl, you have to press it again to actually load the file [works fine in ubuntu]

[Win only] Ctrl-L doesn't clear the screen (works fine in ubuntu)

[Win & Linux] The prompt appends the filename when loading a file,, which is kinda messy Not ideal with long filenames, and not how Emacs and Dr Racket do it I prefer the simple >

Runi-c commented 3 years ago

The error is because read-line does not read CRLF line endings properly by default (default mode is 'linefeed))), so it's including the CR in the number, hence causing string->number to return false. I don't think this is fixable on the extension side, it's up to the editor sending CRLFs (which of course it will, it's Windows).

You can fix it by specifying 'mode of read-line:

(define guess (string->number (read-line (current-input-port) 'any)))
Eugleo commented 3 years ago

I agree with Runi. As per your other issues:

  1. the Load File in REPL button only opens the repl: We'retracking that in #40.
  2. Ctrl-L doesn't clear the screen: I'm not too familiar with Windows, but I think it's up to your shell to interpret the Ctrl+L command. Try switching to git bash, or googling how to clear screen in your particular shell. Can't be fixed on Magic Racket side, unfortunately.
  3. The prompt appends the filename when loading a file: I looked at the racket executable documentation, but I don't see nay flags that would hide the module name from the prompt. If you find something, let me know, and we can implement it.

Seeing that all the issues you mentioned are either not fixable by Magic Racket, or tracked already, I'm closing this issue. Let me know if you have any further problems, @bevo009 .