Class: Cmdr

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

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}, app_mgr = nil) ⇒ Cmdr

Returns a new instance of Cmdr.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cmdr.rb', line 12

def initialize(opt={}, app_mgr=nil)
  
  opt[:public].merge!({
    user: 
    {'-1' => 
      {
        history: 
        {list: [], index: []}
      }
    }
  })        
  
  @v = opt[:public]
  @@app = app_mgr

  super()
end

Instance Method Details

#run_cmd(raw_command, user_id = '-1', bottom_up_display = true) ⇒ Object



31
32
33
34
35
36
37
38
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cmdr.rb', line 31

def run_cmd(raw_command, user_id='-1', bottom_up_display=true)

  input_string = raw_command.clone

  command, options_string = raw_command[/\w+/], ($').strip

  
  if @v[:alias].has_key? command then
    command, options_string2 = @v[:alias][command][/\w+/], ($').strip 
    options_string = options_string2 + ' '+ options_string
  elsif @v[:user][user_id][:alias].has_key? command then
    command, options_string2 = @v[:user][user_id][:alias][command][/\w+/], ($').strip 
    options_string = options_string2 + ' '+ options_string      
  end

  # save the command to history
  unless raw_command[/arrowup|arrowdown/] then
    @v[:user][user_id][:history][:list] << raw_command 
    @v[:user][user_id][:history][:index] = -1
  end

  raw_args = options_string.gsub(/\".[^\|\'"]+["']/) {|x| x.gsub(/\s/,'%20')}.split(/\s/)\
    .map {|x| x.gsub(/%20/,' ').sub(/\"(.*)\"/,'\1')}
  rs = RScript.new()
  code, args = rs.read(raw_args)

  @user_id = user_id unless user_id == '-1'
  doc = Document.new eval(code)

  node = XPath.first(doc.root, 'summary/javascript')
  job_with_js = ''

  if node then
    job_with_js = node.text.to_s
    node.parent.delete node
  end

  if XPath.first(doc.root, "records/*") then
    r = XPath.match(doc.root, 'records/*').map do |node|
      nodes = XPath.match(node, '*')
      if nodes.length > 1 then
        item_details = nodes.map do |x|
          t = x.cdatas.length > 0 ? x.cdatas.join.strip : x.text.to_s.strip
          "<strong>%s</strong>: %s" % [x.name, t]
        end
      else
        item_details = nodes.map do |x|
          x.cdatas.length > 0 ? x.cdatas.join.strip : x.text.to_s.strip
        end              
      end
      "<ul><li>%s</li></ul>" % item_details.join("</li><li>")  
    end
    out = "<ul><li>%s</li></ul>" % r.join("</li><li>")
  else
    text1 = XPath.first(doc.root, 'summary/title/text() | summary/to_s/text()')
    out = REXML::Text::unnormalize(text1.to_s).gsub(/"/,'&#34;')
  end
  
  javascript = []
  javascript << (bottom_up_display == true ? listmite(out) : listite(out))
  javascript << job_with_js

  xml = Builder::XmlMarkup.new( :target => buffer='', :indent => 2 )
  xml.instruct! :xml, :version => "1.0", :encoding => "UTF-8"
  xml.result do
    xml.summary do
      xml.status 'success'
      xml.script {xml.cdata!(javascript.join("\n"))}
      xml.output r.to_s
    end
    xml.records
  end

  buffer

end