Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 245 lines (205 sloc) 4.741 kb
a045712 first commit
Andrew Filer authored
1 #!/usr/bin/ruby -Ku
2
3 def require_gem gem
4 require 'rubygems'
5 require gem
6 end
7
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
8 def error(*args)
9 args = args.find_all { |x| x }
10 args << "\n"
11 STDERR.print "ERROR: ", *args
a045712 first commit
Andrew Filer authored
12 end
13
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
14 class NilClass
15 attr_accessor :sym, :args
16 def method_missing(sym, *args)
17 @sym = sym
18 @args = args
19 end
a045712 first commit
Andrew Filer authored
20 end
21
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
22 class Array
23 def | arg
24 if arg.respond_to? :sym and arg.sym
25 self.map { |x| x.send arg.sym, *arg.args }
a045712 first commit
Andrew Filer authored
26 end
27 end
28 end
29
d3a6be7 Made find() return enumerables, added col() to String
Andrew Filer authored
30
31 class Enumerable::Enumerator
32 def | arg
33 if arg.respond_to? :sym and arg.sym
34 self.map { |x| x.send arg.sym, *arg.args }
35 end
36 end
37
38 def inspect
39 self.map { |x| x.inspect }
40 end
41
42 def to_s
43 self.map { |x| x.to_s }.join "\n"
44 end
45 end
46
47
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
48 class String
49 def | arg
50 if arg.respond_to? :sym and arg.sym
51 self.send arg.sym, *arg.args
a045712 first commit
Andrew Filer authored
52 end
53 end
54
55 def curl(*args)
56 unless $curl
57 require_gem 'curb'
58 $curl = Curl::Easy.new
59 end
60 $curl.url = self
61 if args.length == 0
62 $curl.http_get
63 else
64 $curl.http_post *args
65 end
66 $curl.body_str
67 end
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
68
69 def append_to(file)
70 open(file, 'w') { |f| f << self }
71 end
72
73 def directory?; FileTest.directory? self; end
74 def file?; FileTest.file? self; end
d3a6be7 Made find() return enumerables, added col() to String
Andrew Filer authored
75 def chmod(mode); File.chmod(mode, self); end
76
77 def hexify
78 self.unpack("H*")[0]
79 end
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
80
d3a6be7 Made find() return enumerables, added col() to String
Andrew Filer authored
81 def unhexify
82 [self].pack("H*")
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
83 end
d3a6be7 Made find() return enumerables, added col() to String
Andrew Filer authored
84
85 def clump
86 self.split("\n\n")
87 end
88
89 def col(name)
90 if $col_headers
91 if i = $col_headers.index(name)
92 self.strip.split(/\s+/, $col_headers.length)[i]
93 end
94 else
95 $col_headers = self.strip.split(/\s+/)
96 nil
97 end
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
98 end
d3a6be7 Made find() return enumerables, added col() to String
Andrew Filer authored
99
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
100 end
101
102 class SubshellBase
103 alias_method :old_method_missing, :method_missing
104 def method_missing(sym, *args)
105 if ''.respond_to? sym
106 self.exec "$_.send #{sym.inspect}, *#{args.inspect}"
107 elsif true
108 `#{sym.to_s} #{args.join ' '}`
109 else
110 error "Bad command or filename: #{sym}", (" #{args.inspect}" unless args.empty?)
111 raise NameError
112 end
113 end
114
115 def get_line
116 #eval 'line = STDIN.gets; line ? line.chomp! : nil', self.pure_binding
117 eval 'STDIN.gets', self.pure_binding
118 end
119
120 def pure_binding
121 @pure_binding ||= self.instance_eval "binding"
122 #eval('import SubshellStuff', @pure_binding)
123 end
124
125 def exec(command)
126 begin
127 result = eval(command, self.pure_binding)
128 puts result unless result == nil
129 rescue
130 end
131 end
132 end
133
134 class Subshell < SubshellBase
135 def ÷(a,b)
136 a/b
137 end
138
139 def ifconfig
140 require_gem 'ifconfig'
141 IfconfigWrapper.new.parse
142 end
143
144 def term_cols
145 $TERM_COLS = $TERM_COLS or `tput cols`.to_i
146 end
147
148 def stripe(array)
149 col_width = 12
150 num_cols = term_cols / col_width
151 num_rows = (1.0 * array.length / num_cols).ceil
152 (0..num_rows-1).each do |x|
153 (0..num_cols-1).each do |y|
154 printf "%-#{col_width-1}.#{col_width-1}s ", array[x*num_cols+y]
155 end
156 puts
157 end
158 return nil
159 end
160
161 def help(arg)
162 type = arg.is_a?(Class) ? arg : arg.class
163 stripe type.instance_methods(false).sort
164 return nil
165 end
166
d3a6be7 Made find() return enumerables, added col() to String
Andrew Filer authored
167 def find(path, &block)
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
168 require 'find'
d3a6be7 Made find() return enumerables, added col() to String
Andrew Filer authored
169 if block_given?
170 Find.find(path, &block)
171 else
172 Enumerable::Enumerator.new(lambda { |&blk| Find.find(path) { |x| blk.call x } }, :call)
173 end
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
174 end
175 end
176
177 def get_line
178 line = STDIN.gets
179 line ? CommandString.new(line.chomp!) : nil
a045712 first commit
Andrew Filer authored
180 end
181
182 class CommandString < String
183 def initialize(*args)
184 super args.join(' ').gsub '÷', '/'
185 end
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
186 end
187 def CommandString(*args); CommandString.new(*args); end
188
189 def exec(command)
190 command = CommandString command
191 subshell = Subshell.new
192
193 if STDIN.stat.pipe?
194 while line = subshell.get_line
195 subshell.exec command
a045712 first commit
Andrew Filer authored
196 end
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
197 else
198 subshell.exec command
a045712 first commit
Andrew Filer authored
199 end
200 end
201
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
202 def ps1
203 display_pwd = ENV['PWD'].sub /^#{ENV['HOME']}/, '~'
204 "#{ENV['USER']}@#{ENV['HOST']}:#{display_pwd} $ "
205 end
206
207 def shell
208 require 'readline'
a045712 first commit
Andrew Filer authored
209
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
210 stty_save = `stty -g`.chomp
211 trap('INT') { puts; puts; system('stty', stty_save); exit }
212
213 subshell = Subshell.new
214
215 while line = Readline.readline(ps1)
216 finish if line.nil? or line == 'exit'
217 next if line == ""
218 Readline::HISTORY.push line
219
d3a6be7 Made find() return enumerables, added col() to String
Andrew Filer authored
220 #commands = line.split /(\s*[;|&]\s*)/
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
221 #command.each_slice(2) do |i|
222 # cmd = commands[i]
223 # delim = commands[i+1]
224 # #######
225 #end
226 subshell.exec line
a045712 first commit
Andrew Filer authored
227 end
30b96f0 Lots o changes, created "pure" binding, added rudimentary interactive sh...
Andrew Filer authored
228 end
229
d3a6be7 Made find() return enumerables, added col() to String
Andrew Filer authored
230 if __FILE__ == $0
231 # Allows for this bash magic: alias crush='crush "#" "`history 1`"'
232 # x='634 cr pwd ### puts "hi" ### ls'; re='###(.+)###'; if [[ $x =~ $re ]]; then echo ${BASH_REMATCH[1]}; fi
233 cmd = if ARGV.length > 1 and ARGV[0] == '#'
234 #ARGV[1].split(/\s+/, 4)[3..-1]
235 ARGV[1].split(/#/, 2)[1]
236 else
237 ARGV
238 end
239
240 if cmd.empty? or cmd.first.empty?
241 shell
242 else
243 exec cmd
244 end
a045712 first commit
Andrew Filer authored
245 end
Something went wrong with that request. Please try again.