4

Is it possible to edit content already written to the command prompt using Ruby?

So for example, lets say I've written 10 lines to STDOUT, can I move the cursor to, say, lime five and overwrite just that line?

Thanks.

Jean-Luc
  • 3,563
  • 8
  • 40
  • 78
  • You mean IRB ? In the command prompt console of your OS only one series of commands can be executed at one time, editing and executing again would just rerun the changed code – peter Dec 03 '12 at 19:18
  • I don't mean in the IRB, I mean in the windows cmd.exe program that I run my ruby program in. I'm thinking I need cursed... – Jean-Luc Dec 03 '12 at 23:14

1 Answers1

2

Yes you can, on a Windows Vista, 7 and probably 8 and also in some third-party extended command-interpreters like 4NT and Take Command you can recall previous commands by using the up-key, edit the line and re-execute the line. I don't see what Ruby has got to do with this. If you want to let Ruby type keystrokes in a console that is possible using the auto-it Active-X control.

EDIT: here a sample using Autoit to edit the console, downlaod and install it first and then run the following script. To make sure the script doesn't interact with other open consoles i copied mu cmd.exe to a cmd2.exe which is started up first.

require 'win32ole' 

title = "C:\\Windows\\System32\\cmd2.exe"
STDOUT.sync = true 
ai = WIN32OLE.new("AutoItX3.Control") 
ai.winwait(title)
ai.WinActivate(title, "")
ai.Send "cls{ENTER}"
1.upto(4) do |i|
  ai.Send "line#{i}{ENTER}"
end
1.upto(4) do |i|
  ai.Send "{UP}"
  sleep 1
end
ai.Send "line one {ENTER}"
peter
  • 41,770
  • 5
  • 64
  • 108
  • Perhaps I didn't write it to well, although I am interested in what you've written, my question was more: If I have text already written on the command prompt, is it possible for me to overwrite it with different text? (using Ruby) – Jean-Luc Dec 03 '12 at 23:57
  • That is what i answered yes, using Auto-it by simulating keystrokes, that is the only way, but why do that if you can execute any command in Ruby at any time ? Perhaps you could better explain what you really want to achieve – peter Dec 04 '12 at 00:07
  • I am reading data from the serial port to the command prompt, and the data being read needs to overwrite the data already there. So, I read 5 lines of data from the serial port and display is on the command prompt, then, some amount of time later, I read 5 lines again from the serial port, and those 5 lines need to overwrite the previous 5 lines that are already on the command-prompt (not display them underneath). – Jean-Luc Dec 04 '12 at 00:15
  • 1
    you use the console as a view, i have no experience with reading data from the serial port but you could capture this data and use a control you have more control over, like a GUI window or in the browser again using Watir but if you really want to do it this way you can use the technique i proposed. I can give you an example wednesday if you want. another way would be to print a controle char which moves the cursor but you would need an ansi gem to do that, but iaf you can't move up, see my question http://stackoverflow.com/questions/10262235/printing-an-ascii-spinning-cursor-in-the-console – peter Dec 04 '12 at 00:36
  • How are you supposed to move the cursor to the previous line? Like how can I move the cursor three lines up? – Jean-Luc Dec 04 '12 at 03:45
  • i'll make you a working sample and add it to my answer if i have a 5 minute break tomorrow – peter Dec 04 '12 at 17:12