如何在Ruby控制台实用程序中清除终端?

时间:2021-05-01 20:51:28

I'm trying to write a little console script in Ruby that will run my tests for me. I'm basing it on this Rake task by Peter Cooper.

我正在尝试在Ruby中编写一个小程序脚本,它将为我运行我的测试。我是以Peter Cooper的Rake任务为基础的。

It works, but it would be nice if it would clear the console before each run.

它可以工作,但如果它在每次运行之前清除控制台会很好。

Can someone tell me how to make it do that?

有人能告诉我如何做到这一点吗?

Here's what I have so far:

这是我到目前为止所拥有的:

require 'find'
files = {}

session_count = 0

puts "Watching #{File.expand_path(File.dirname(__FILE__))}."

loop do
  changed = false
  Find.find(File.dirname(__FILE__)) do |file|
    next unless file =~ /\.rb$/
    ctime = File.ctime(file).to_i

    if ctime != files[file]
      files[file] = ctime
      changed = true
    end
  end

  if changed

    command = 'ruby -Itest test/**/*_test.rb'

    puts '---------------------------------------------------------------------------------------------------------'
    puts "[#{session_count}] #### Running:\n      #{command}"
    puts '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'

    puts `#{command}`

    puts '---------------------------------------------------------------------------------------------------------'
    puts "[#{session_count}] #### Done"
    puts '---------------------------------------------------------------------------------------------------------'

    session_count += 1

  end

  sleep 1
end

I'm on Mac OS 10.7.5 and using Zsh 4.3.11.

我使用的是Mac OS 10.7.5并使用Zsh 4.3.11。

1 个解决方案

#1


3  

You could do:

你可以这样做:

 if changed
    system('clear') 
...

#1


3  

You could do:

你可以这样做:

 if changed
    system('clear') 
...