Class: Redcar::FilterCommand::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/filter_command/evaluator.rb

Overview

The command parser and runner for FilterCommand

Class Method Summary collapse

Class Method Details

.evaluate(command, input, output) ⇒ Object

Command processing before execution. If successful, the given command is run.

Parameters:

  • command (String)

    the command to run

  • input (String)

    the selected input format

  • output (String)

    the selected output format



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/filter_command/evaluator.rb', line 17

def self.evaluate(command,input,output)
  win = Redcar.app.focussed_window
  tab = win.focussed_notebook_tab
  original_command = command.clone
  if tab and tab.is_a?(Redcar::EditTab) and doc = tab.edit_view.document
    case input
    when "None" # do nothing
    when "Selection"
      if doc.selection?
        selection = doc.selected_text
      else
        selection = doc.get_line_without_end_of_line(doc.cursor_line)
      end
      command = pipe_text(selection, command)
    when "Document"
      command  = pipe_text(doc.get_all_text, command)
    when "Cursor Line"
      command  = pipe_text(doc.get_line_without_end_of_line(doc.cursor_line), command)
    when "Cursor Word"
      command  = pipe_text(doc.current_word, command)
    else
      raise "Unknown input type found: #{input}"
    end
    result  = FilterCommand::CommandParser.substitute_variables(doc,command)
    command = result['command']
    temp    = result['temp']
  elsif input != "None"
    tab_type_error input
    return
  end
  execute_command win, command, input, output
  if temp and File.exist?(temp)
    Redcar::Project::Manager.open_file(temp)
  end
  FilterCommand::History.update_commands original_command, input, output
end

.execute_command(window, command, input, output) ⇒ Object

The actual running of a command filter speedbar. If the current window has a project open, the process working directory will be the project directory.

Parameters:

  • window (Redcar::Window)

    a Redcar window, generally the window containing the

  • command (String)

    the command to be executed

  • input (String)

    the selected input format



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/filter_command/evaluator.rb', line 60

def self.execute_command window, command, input, output
  if project = Project::Manager.in_window(window)
    command = set_path command, project.path
  end
  pid, io_input, io_output, io_error = IO.popen4(command)
  if error = io_error.read and error.empty?
    format_text(io_output.read, output)
  else
    Redcar::Application::Dialog.message_box(error, :type => :error)
  end
end

.format_text(text, format) ⇒ Object

Command post-processing.

Parameters:

  • text (String)

    the text output of the the command process

  • output (String)

    the selected output format



75
76
77
78
79
80
81
# File 'lib/filter_command/evaluator.rb', line 75

def self.format_text text, format
  if format_class = OutputFormat.formats[format]
    format_class.new.format(text,format)
  else
    raise "Unknown output type found: #{format}"
  end
end

.input_typesObject

List of supported input types for command execution



8
9
10
# File 'lib/filter_command/evaluator.rb', line 8

def self.input_types
  ['None','Selection','Document','Cursor Line','Cursor Word']
end

.set_path(command, path) ⇒ Object

Sets the working directory for a given command to a given path, and adds proper escaping (platform-specific).

Parameters:

  • command (String)

    the command to be executed

  • path (String)

    the desired running directory



87
88
89
90
91
92
93
94
95
# File 'lib/filter_command/evaluator.rb', line 87

def self.set_path command, path
  case Redcar.platform
  when :osx, :linux
    command = "sh -c \"cd \\\"#{path}\\\" && #{command}\""
  when :windows
    command = "cd \"#{path.gsub('/', '\\')}\" & #{command}"
  end
  command
end