1

When I try to correct input for my crystal program (in a zsh terminal on my Mac), a ^? character is printed to the screen for each press of backspace. It's disorienting and does not delete any characters from the screen, but it technically functions just fine as I discovered playing around with this little testing snippet.

a = gets.as(String).chomp
puts a
a = gets   # alright then^?^?^?^?^?
puts a     # alright

What is going on here? How can I make my input behave as a user would expect, is there something special I can do with STDIN?

Giant Frog
  • 13
  • 1
  • 5
  • 1
    Can't confirm this behaviour for `zsh-5.8.1` on linux, so this issue might be osx related? –  May 20 '22 at 09:11
  • That's useful to know, thank you js-on! – Giant Frog May 20 '22 at 17:45
  • I don't see how this is related to zsh, in particular since I don't see any zsh code in your question. It seems that the library which processes user input, does not know what to do with a backspace. Maybe you can use `rlwrap`. This was written originally with Linux in mind, but if you google for _rlwrap macos_, a few hits will come up. – user1934428 May 24 '22 at 06:03
  • I wasn't sure if this was happening because I was using zsh. It turns out, no, it's not related, so I removed the tag from the post. – Giant Frog May 24 '22 at 20:49

1 Answers1

1

I think, it will depend on the terminal that you are using and it is largely independent of the programming language (e.g. it has been reported in Python).

Some terminals send ^H or ^? when you type a backslash. I can also reproduce it in xterm (on Linux) when calling cat, which is roughly similar to getting a line and printing it (in a loop):

$ echo $TERM
xterm

$ cat
abc^H^H^Hdef
def

... while it works with other terminals (same test: typing abc, deleting three characters, then typing def):

$ echo $TERM
xterm-256color

$ cat
def
def

You can use libraries like readline to workaround around it. I have not tried it myself, but this library implements bindings for Crystal: crystal-readline

Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
  • 1
    That's super interesting! I wasn't aware of that library, so I took it for a spin and it seems to handle backspaces on my system perfectly, as well as not print ctrl + key symbols. Thank you for the recommendation! – Giant Frog May 20 '22 at 17:58