Way of handling CLI interface for chat program.

M

Michael

Hello, folks.

I'm trying to write simple CLI program which will be some kind of
chat, but i have faced with some output problem.
Is it possible to output over current line in stdout?

User types something in prompt> and at this moment message from
another user arrives. Currently, I'm using readline, and if i just
<puts "\n#{user}: #{message}\n"> I'll end with following

screen 2:
 
T

Tim Pease

Hello, folks.

I'm trying to write simple CLI program which will be some kind of
chat, but i have faced with some output problem.
Is it possible to output over current line in stdout?


STDOUT.write "\rThis will overwrite the current line"

The "\r" is the carriage return character, and it will return the
insert point to the beginning of the current line. The only caveat is
that the new line you are writing needs to be at least as long as the
line you wish to overwrite. If it is not, you will have remnants of
the previous line at the end of your output.

Another option would be to use the "highline" gem.

Blessings,
TwP
 
M

Michael

STDOUT.write "\rThis will overwrite the current line"

The "\r" is the carriage return character, and it will return the
insert point to the beginning of the current line. The only caveat is
that the new line you are writing needs to be at least as long as the
line you wish to overwrite. If it is not, you will have remnants of
the previous line at the end of your output.

You can issue STDOUT.write("\e[K"), it will erase whole line and you
won't bother about it length, but it's not what i'm asking for :(
sorry for my English, I'll try be more specific.

Consider the following example:
---------------------------------------------------
th = Thread.new { loop do puts "Hello, Ruby!"; sleep(10) end }
while (line = Readline.readline("prompt> ", true))
puts "#{line}: processed"
end
---------------------------------------------------

This code will produce such results:
---------------------------------------------------
$ ./cli.rb
Hello, Ruby!
prompt> I WAS TYPING HERE FOR 10 SECONDSHello, Ruby!
---------------------------------------------------
And what I want, it's ALL lines be put above my input prompt, and not
interrupting me while I'm typing.
Another option would be to use the "highline" gem.

Yeah, thank you and Robert for that pointing (really good ideads
inside!), but brief looking shows that it is not suitable for my
purpose of non-standard outputting.

One way to solve this problem which i see is safe current line of
input, including "prompt> ", erase it, output event, and output saved
line again, hopefully it will be fast enough and user won't see
anything, but how can i get current line, anyway?
 
J

James Gray

Consider the following example:
---------------------------------------------------
th = Thread.new { loop do puts "Hello, Ruby!"; sleep(10) end }
while (line = Readline.readline("prompt> ", true))
puts "#{line}: processed"
end

This doesn't clear the lines as you were asking for, but hopefully it
will give you new ideas:

#!/usr/bin/env ruby -wKU

require "thread"
require "timeout"

require "rubygems"
require "highline/system_extensions"

incoming_messages = Queue.new
output_buffer = String.new

# simulate incoming server messages
Thread.new do
loop do
incoming_messages << "AnnoyingUser: Hi, I'm still here!"
sleep 5
end
end

# main event loop
prompt = true
loop do
# handle output
unless incoming_messages.empty?
puts unless prompt
puts incoming_messages.shift until incoming_messages.empty?
prompt = true
end
puts incoming_messages.shift until incoming_messages.empty?

# prompt
if prompt
print ">> #{output_buffer}"
$stdout.flush
prompt = false
end

# fetch input
begin
Timeout.timeout(0.1) do
if char = HighLine::SystemExtensions.get_character.chr
output_buffer << char
print char
$stdout.flush
end
end
rescue Timeout::Error
# do nothing -- we'll try again later
end

# handle full lines of input
if output_buffer[-1, 1] == "\n"
incoming_messages << "Sending '#{output_buffer.chomp}' to
server..."
output_buffer = String.new
prompt = true
end
end

__END__

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,137
Messages
2,570,797
Members
47,342
Latest member
eixataze

Latest Threads

Top