Created
April 11, 2011 19:47
-
-
Save atoulme/914171 to your computer and use it in GitHub Desktop.
A script to escape a String into unicode characters for double bytes strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
puts "Enter content and press Ctrl+D when ready" | |
old_stty = `stty -g` | |
# Set up the terminal in non-canonical mode input processing | |
# This causes the terminal to process one character at a time | |
system "stty -icanon min 1 time 0 -isig" | |
answer = "" | |
while true | |
char = STDIN.getc | |
break if char == ?\C-d # break on Ctrl-d | |
answer += char.chr | |
end | |
puts "\n\nOutput:" | |
puts answer.unpack('U*').map{ |i| i < 128 ? i.chr : '\u' + i.to_s(16).rjust(4, '0') }.join | |
system "stty #{old_stty}" # restore stty settings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/3727466/multi-line-input-problems-when-using-stdin-gets was the key to get the multi line input correctly.