cmdr 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cmdr.rb +125 -0
- metadata +74 -0
data/lib/cmdr.rb
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
#!/uar/bin/ruby
|
2
|
+
|
3
|
+
#file: cmdr.rb
|
4
|
+
|
5
|
+
require 'builder'
|
6
|
+
require 'rscript'
|
7
|
+
require 'rexml/document'
|
8
|
+
include REXML
|
9
|
+
|
10
|
+
class Cmdr
|
11
|
+
|
12
|
+
def initialize(opt={})
|
13
|
+
@v = opt[:public]
|
14
|
+
o = {bottom_up_display: true}.merge(opt[:config])
|
15
|
+
bottom_up_display = o[:bottom_up_display]
|
16
|
+
super()
|
17
|
+
end
|
18
|
+
|
19
|
+
def run_cmd(raw_command, user_id='-1')
|
20
|
+
|
21
|
+
input_string = raw_command.clone
|
22
|
+
|
23
|
+
command, options_string = raw_command[/\w+/], ($').strip
|
24
|
+
|
25
|
+
aliasx = @v[:alias]
|
26
|
+
if aliasx.has_key? command then
|
27
|
+
command, options_string2 = aliasx[command][/\w+/], ($').strip
|
28
|
+
options_string = options_string2 + ' '+ options_string
|
29
|
+
end
|
30
|
+
|
31
|
+
# save the command to history
|
32
|
+
unless raw_command[/arrowup|arrowdown/] then
|
33
|
+
@v[:user][user_id][:history][:list] << raw_command
|
34
|
+
@v[:user][user_id][:history][:index] = -1
|
35
|
+
end
|
36
|
+
|
37
|
+
raw_args = options_string.gsub(/\".[^\|\'"]+["']/) {|x| x.gsub(/\s/,'%20')}.split(/\s/)\
|
38
|
+
.map {|x| x.gsub(/%20/,' ').sub(/\"(.*)\"/,'\1')}
|
39
|
+
rs = RScript.new()
|
40
|
+
code, args = rs.read(raw_args)
|
41
|
+
|
42
|
+
@user_id = user_id unless user_id == '-1'
|
43
|
+
doc = Document.new eval(code)
|
44
|
+
|
45
|
+
node = XPath.first(doc.root, 'summary/javascript')
|
46
|
+
job_with_js = ''
|
47
|
+
|
48
|
+
if node then
|
49
|
+
job_with_js = node.text.to_s
|
50
|
+
node.parent.delete node
|
51
|
+
end
|
52
|
+
|
53
|
+
if XPath.first(doc.root, "records/*") then
|
54
|
+
r = XPath.match(doc.root, 'records/*').map do |node|
|
55
|
+
nodes = XPath.match(node, '*')
|
56
|
+
if nodes.length > 1 then
|
57
|
+
item_details = nodes.map do |x|
|
58
|
+
t = x.cdatas.length > 0 ? x.cdatas.join.strip : x.text.to_s.strip
|
59
|
+
"<strong>%s</strong>: %s" % [x.name, t]
|
60
|
+
end
|
61
|
+
else
|
62
|
+
item_details = nodes.map do |x|
|
63
|
+
x.cdatas.length > 0 ? x.cdatas.join.strip : x.text.to_s.strip
|
64
|
+
end
|
65
|
+
end
|
66
|
+
"<ul><li>%s</li></ul>" % item_details.join("</li><li>")
|
67
|
+
end
|
68
|
+
out = "<ul><li>%s</li></ul>" % r.join("</li><li>")
|
69
|
+
else
|
70
|
+
text1 = XPath.first(doc.root, 'summary/title/text() | summary/to_s/text()')
|
71
|
+
out = REXML::Text::unnormalize(text1.to_s).gsub(/"/,'"')
|
72
|
+
end
|
73
|
+
|
74
|
+
javascript = []
|
75
|
+
javascript << bottom_up_display == true ? listmite(out) : listite(out)
|
76
|
+
javascript << job_with_js
|
77
|
+
|
78
|
+
xml = Builder::XmlMarkup.new( :target => buffer='', :indent => 2 )
|
79
|
+
xml.instruct! :xml, :version => "1.0", :encoding => "UTF-8"
|
80
|
+
xml.result do
|
81
|
+
xml.summary do
|
82
|
+
xml.status 'success'
|
83
|
+
xml.script {xml.cdata!(javascript.join("\n"))}
|
84
|
+
xml.output r.to_s
|
85
|
+
end
|
86
|
+
xml.records
|
87
|
+
end
|
88
|
+
|
89
|
+
buffer
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
protected
|
94
|
+
|
95
|
+
def listite(out)
|
96
|
+
"
|
97
|
+
olist = document.getElementById('list');
|
98
|
+
ocontent = document.getElementById('content');
|
99
|
+
oli = document.createElement('li');
|
100
|
+
|
101
|
+
oli.innerHTML = \"#{out.gsub(/\r|\n/,"").gsub(/"/,'"')}\";
|
102
|
+
ocontent.insertBefore(oli, ocontent.firstChild);
|
103
|
+
|
104
|
+
olist.scrollTop = olist.scrollHeight;
|
105
|
+
|
106
|
+
ocommandInput = document.getElementById('user_input');
|
107
|
+
ocommandInput.value = '';
|
108
|
+
"
|
109
|
+
end
|
110
|
+
|
111
|
+
def listmite(out)
|
112
|
+
"
|
113
|
+
ocontent = document.getElementById('content');
|
114
|
+
oli = document.createElement('li');
|
115
|
+
|
116
|
+
oli.innerHTML = \"#{out.gsub(/\r|\n/,"").gsub(/"/,'"')}\";
|
117
|
+
ocontent.appendChild(oli);
|
118
|
+
olist.scrollTop = olist.scrollHeight;
|
119
|
+
|
120
|
+
ocommandInput = document.getElementById('user_input');
|
121
|
+
ocommandInput.value = '';
|
122
|
+
"
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cmdr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors: []
|
7
|
+
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-08-10 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: builder
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rscript
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description:
|
36
|
+
email:
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- lib/cmdr.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage:
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: cmdr
|
73
|
+
test_files: []
|
74
|
+
|