Module: Catch_output

Included in:
Frame_catch_output, RunTeX::Tool
Defined in:
lib/catch_output.rb

Overview

Catch screen output. Can be used to avoid messages from programms called by system

Constant Summary collapse

STDOUT_ORIG =
STDOUT.clone()
STDERR_ORIG =
STDERR.clone()

Instance Method Summary collapse

Instance Method Details

#catch_screen_output(catch_stdout = true, catch_stderr = true, stdout_orig = STDOUT.clone(), stderr_orig = STDERR.clone()) ⇒ Object

Catch the screen output (stdout and stderr) for the given block. You can set, which output you want to catch.

Returnvalue is an array with the result of stdout and stderr. If any output wasn’t catched, the return value in the array is nil.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/catch_output.rb', line 39

def catch_screen_output(
    catch_stdout = true,
    catch_stderr = true,
    stdout_orig = STDOUT.clone(),
    stderr_orig = STDERR.clone()
    )
    
  raise 'no block' unless block_given? 
     
  if catch_stdout
    #Create temporary file for stdout

    tmpstdout = Tempfile.new( 'stdout')
    #redirect stdout

    STDOUT.reopen( tmpstdout ) 
  end
  if catch_stderr
    #Create temporary file for stdout

    tmpstderr = Tempfile.new( 'stderr')
    #redirect stdout

    STDERR.reopen( tmpstderr ) 
  end
  

  yield #Execute the block

  
  if catch_stdout
    #stdout is coming again to the screen.

    tmpstdout.close()    
    STDOUT.reopen( stdout_orig)
    
    # Get the result of stdout

    tmpstdout.open()
    stdout = tmpstdout.readlines().join
    tmpstdout.close()
  end
  

  if catch_stderr
    #stderr is coming again to the screen.

    tmpstderr.close()    
    STDERR.reopen( stderr_orig)
    
    # Get the result of stderr

    tmpstderr.open()
    stderr = tmpstderr.readlines().join
    tmpstderr.close()
  end
  return [ stdout, stderr ]
end

#catch_stderr(&block) ⇒ Object

Catch stderr for the given block, but print stdout.



27
28
29
30
31
32
# File 'lib/catch_output.rb', line 27

def catch_stderr( &block )
  #~ raise 'no block' unless block_given? 

  raise 'no block' unless block.is_a?(Proc)
  stdout, stderr = catch_screen_output( false, true, &block )
  return stderr
end

#catch_stdout(&block) ⇒ Object

Catch stdout for the given block, but print stderr.



19
20
21
22
23
24
# File 'lib/catch_output.rb', line 19

def catch_stdout( &block )
  #~ raise 'no block' unless block_given? 

  raise 'no block' unless block.is_a?(Proc)
  stdout, stderr = catch_screen_output( true, false, &block )
  return stdout
end