Ruby - Hide "^C" on Interrupt
Asked Answered
C

3

10

In Ruby I have the following:

# Trap Interrupts
trap("INT") do
    puts "Shutting down..."
    exit
end

When I interrupt the program, the following is printed (Mac OSX Lion):

^CShutting down...

Is there any way to hide ^C from within Ruby?

Caustic answered 20/3, 2012 at 21:10 Comment(1)
Begin the exit message with a 'C' (something like Closing link...) and then omitting the 'C' :) (Still would have the ^ though)Arching
T
10

Whether control characters are echoed is a property of the tty you're using. stty -echoctl is the Unix way to disable echoing of control characters. You can run this command from within your Ruby script and achieve the same effect if you're using a Unix-ish system.

Tarshatarshish answered 20/3, 2012 at 21:18 Comment(0)
D
4

I've found that in my Python programs on Linux and Mac OS X terminals I can hide the ^C by starting the message with a carriage return (\r). It feels like a hack but it works just fine.

Detrusion answered 29/5, 2012 at 19:24 Comment(0)
V
0

You can also use backspace characters to erase the ^C:

trap("INT") do
    puts "\b"*2 + "Shutting down..."
    exit
end
Ventail answered 30/10, 2019 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.