Skip to content

Instantly share code, notes, and snippets.

@tribela
Created February 20, 2014 02:48
Show Gist options
  • Save tribela/9106195 to your computer and use it in GitHub Desktop.
Save tribela/9106195 to your computer and use it in GitHub Desktop.
ruby readline while printing
# encoding: UTF-8
require "EventMachine"
require "readline"
prompt = '⚡ '
def mutex
@mutex || Mutex.new
end
def sync(&block)
mutex.synchronize do
block.call
end
end
def output
@num ||= 0
@num = (@num+1) % 5
STDOUT.print "\e[0G\e[K"
puts @num
Readline.refresh_line
end
def stop
EM.stop_event_loop
end
EM.run do
Thread.start do
while buf = Readline.readline(prompt, true)
unless Readline::HISTORY.count == 1
Readline::HISTORY.pop if buf.empty? || Readline::HISTORY[-1] == Readline::HISTORY[-2]
end
sync {
puts buf
}
end
stop
end
EM.add_periodic_timer(3) do
if Readline.line_buffer.nil? || Readline.line_buffer.empty?
sync {
output
}
end
end
trap('INT') { stop }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment