class Intar
intar/version.rb – Version number
Constants
- DEFAULTS
- Metacmds
- OLDSET
- VERSION
Attributes
metacmds[R]
Public Class Methods
new(obj = nil, **params)
click to toggle source
# File lib/intar.rb, line 77 def initialize obj = nil, **params @obj = obj.nil? ? (eval "self", TOPLEVEL_BINDING) : obj @params = DEFAULTS.dup.update params @binding = @obj.intar_binding @n = 0 @prompt = Prompt.new end
open(obj = nil, **params) { |i| ... }
click to toggle source
# File lib/intar.rb, line 53 def open obj = nil, **params i = new obj, **params yield i end
run(obj = nil, **params)
click to toggle source
# File lib/intar.rb, line 58 def run obj = nil, **params open obj, **params do |i| i.run end end
Private Class Methods
metacmd(names, summary, desc)
click to toggle source
# File lib/intar.rb, line 298 def metacmd names, summary, desc @mcd = [ names, summary, desc] end
method_added(sym)
click to toggle source
# File lib/intar.rb, line 288 def method_added sym if @mcd then names, summary, desc = *@mcd m = Metacmds[ sym, summary, desc] names.each { |n| @metacmds[ n] = m } @mcd = nil end end
Public Instance Methods
execute(code)
click to toggle source
# File lib/intar.rb, line 126 def execute code eval code, @binding, "#{self.class}/execute" end
run()
click to toggle source
# File lib/intar.rb, line 89 def run prompt_load_history oldset = eval OLDSET, @binding while l = readline do begin @redir = find_redirect l r = if l =~ /\A\\(\w+|.)\s*(.*?)\s*\Z/ then m = get_metacommand $1 send m.method, (eval_param $2) else begin @redir.redirect_output do eval l, @binding, @file end rescue SyntaxError raise if l.end_with? $/ @previous = l next end end rescue Quit break rescue Failed switchcolor 31, 1 puts $! switchcolor r = $! rescue Exception break if SystemExit === $! and not @params[ :catch_exit] show_exception r = $! else display r end oldset.call r, @n end prompt_save_history end
Private Instance Methods
cmd_cd(x)
click to toggle source
# File lib/intar.rb, line 364 def cmd_cd x @wds ||= [] y = Dir.getwd if x =~ /\A([%=-])?(\d+)?\z/ then x = $2 ? (@wds.delete_at -$2.to_i) : @wds.pop x or raise Failed, ($2 ? "No directory ##$2." : "No last directory.") case $1 when "-" then x = nil when "=" then y = nil end end if x then x = File.expand_path x Dir.chdir x rescue raise Failed, $!.to_s @wds.push y if y y = Dir.getwd @wds.delete y end @redir.redirect_output do i = @wds.length @wds.each { |d| puts "%2d %s" % [ i, d] i -= 1 } end y end
cmd_edit(x)
click to toggle source
# File lib/intar.rb, line 487 def cmd_edit x fn = tempname x = Regexp.new x if x p = prompt_scan_history { |l| break l if not x or l =~ x } File.open fn, "w" do |f| f.write p end begin system ENV[ "EDITOR"]||ENV[ "VISUAL"]||"vi", fn or raise Failed, "Executing editor failed: #{$?.exitstatus}" p = File.read fn p.strip! @prompt.push p @redir.redirect_output do eval p, @binding, @file end ensure File.unlink fn end end
cmd_env(x)
click to toggle source
# File lib/intar.rb, line 395 def cmd_env x if x then cmds_split_assign x do |n,v| if v then v =~ /\A"((?:[^\\"]|\\.)*)"\z/ and v = ($1.gsub /\\(.)/, "\\1") ENV[ n] = v else ENV[ n] end end else @redir.redirect_output do ENV.keys.sort.each { |n| puts "#{n}=#{ENV[ n]}" } nil end end end
cmd_help(x)
click to toggle source
# File lib/intar.rb, line 310 def cmd_help x @redir.redirect_output do if x then mc = get_metacommand x names = cmds_list[ mc] puts "Metacommand: #{names.join ' '}" puts "Summary: #{mc.summary}" puts "Description:" puts mc.description else l = cmds_list.map { |k,v| [v,k] } puts "Metacommands:" l.each { |names,mc| puts " %-20s %s" % [ (names.join " "), mc.summary] } end end nil end
cmd_history(x)
click to toggle source
# File lib/intar.rb, line 514 def cmd_history x case x when "l", "load" then prompt_load_history when "s", "save" then prompt_save_history when %r(\A(\d+)?/\s*) then search_history $1, $' when %r(\A(\d+)(?:\s+(\d+))?\s*\z) then show_history $1.to_i, $2.to_i when nil then show_history 5, 0 else raise Failed, "Unknown history command: #{x}" end end
cmd_input(x)
click to toggle source
# File lib/intar.rb, line 465 def cmd_input x x or raise Failed, "No input file given." l = File.read x rescue raise Failed, $!.to_s @redir.redirect_output do eval l, @binding, x end end
cmd_output(x)
click to toggle source
# File lib/intar.rb, line 476 def cmd_output x if x then File.open x, "w" do end rescue raise Failed, "File error: #$!" end @params[ :output] = x end
cmd_param(x)
click to toggle source
# File lib/intar.rb, line 433 def cmd_param x if x then cmds_split_assign x do |n,v| if v then @params[ n.to_sym] = parse_param v else @params[ n.to_sym] end end else @redir.redirect_output do @params.keys.sort.each { |n| puts "#{n}=#{@params[n].inspect}" } nil end end end
cmd_quit(x)
click to toggle source
# File lib/intar.rb, line 350 def cmd_quit x raise Quit end
cmd_shell(x)
click to toggle source
# File lib/intar.rb, line 422 def cmd_shell x @redir.redirect_output do system x||ENV[ "SHELL"]||"/bin/sh" or raise Failed, "Exit code: #{$?.exitstatus}" end nil end
cmd_version(x)
click to toggle source
# File lib/intar.rb, line 340 def cmd_version x @redir.redirect_output do puts "#{self.class} #{VERSION}" VERSION end end
cmds_list()
click to toggle source
# File lib/intar.rb, line 329 def cmds_list l = Hash.new { |h,k| h[k] = [] } self.class.metacmds.each_pair { |k,v| l[ v].push k } l end
cmds_split_assign(x) { |n, notempty?| ... }
click to toggle source
# File lib/intar.rb, line 414 def cmds_split_assign x n, v = x.split /\s*=\s*|\s+/, 2 yield n, v.notempty? end
color(*c)
click to toggle source
# File lib/intar.rb, line 180 def color *c if @params[ :color] then s = c.map { |i| "%d" % i }.join ";" "\e[#{s}m" end end
cur_prompt(prev)
click to toggle source
# File lib/intar.rb, line 156 def cur_prompt prev p = @params[ :prompt].to_s p.gsub /%(?: \(([^\)]+)?\) | ([+-]?[0-9]+(?:\.[0-9]+)?) )?(.)/nx do case $3 when "s" then @obj.to_s when "i" then $1 ? (@obj.send $1) : @obj.inspect when "n" then "%#$2d" % @n when "t" then Time.now.strftime $1||"%X" when "u" then Etc.getpwuid.name when "h" then Socket.gethostname when "w" then cwd_short when "W" then File.basename cwd_short when "c" then color *($1 || $2 || "").split.map { |x| x.to_i } when ">" then prev ? "." : Process.uid == 0 ? "#" : ">" when "%" then $3 else $& end end end
cwd_short()
click to toggle source
# File lib/intar.rb, line 192 def cwd_short r = Dir.pwd h = Etc.getpwuid.dir r[ 0, h.length] == h and r[ 0, h.length] = "~" r end
display(r)
click to toggle source
# File lib/intar.rb, line 225 def display r return if r.nil? and not @params[ :shownil] s = @params[ :show] s or return s = s.to_i rescue 0 i = ARROW.dup i << r.inspect if s > 0 then siz, = $stdout.wingeom siz *= s if i.length > siz then i.cut! siz-ELLIPSIS.length i << ELLIPSIS end end puts i end
eval_param(l)
click to toggle source
# File lib/intar.rb, line 260 def eval_param l eot = "EOT0001" eot.succ! while l[ eot] l = eval "<<#{eot}\n#{l}\n#{eot}", @binding, @file l.strip! l.notempty? end
extract_from_history() { |l| ... }
click to toggle source
# File lib/intar.rb, line 549 def extract_from_history a = [] prompt_scan_history do |l| n = yield l a.push n if n end ensure @redir.redirect_output do while (p = a.pop) do puts p end end end
find_redirect(line)
click to toggle source
# File lib/intar.rb, line 133 def find_redirect line RedirectPipe.detect line, @params[ :pager] or RedirectFile.detect line, @params[ :output] or RedirectNone.new end
get_metacommand(name)
click to toggle source
# File lib/intar.rb, line 303 def get_metacommand name self.class.metacmds[ name] or raise Failed, "Unknown Metacommand: #{name}" end
parse_param(v)
click to toggle source
# File lib/intar.rb, line 451 def parse_param v case v when /\At(?:rue)?\z/i, /\Ayes\z/i, /\Aon\z/i then true when /\Af(?:alse)?\z/i, /\Ano\z/i, /\Aoff\z/i then false when /\A(?:nil|none|-)\z/i then nil when /\A(?:[+-])?\d+\z/i then $&.to_i when /\A"((?:[^\\"]|\\.)*)"\z/ then $1.gsub /\\(.)/, "\\1" else v end end
prompt_load_history()
click to toggle source
# File lib/intar.rb, line 268 def prompt_load_history @prompt.load_history @params[ :histfile] end
prompt_save_history()
click to toggle source
# File lib/intar.rb, line 271 def prompt_save_history @prompt.save_history @params[ :histfile], @params[ :histmax] end
prompt_scan_history() { |l| ... }
click to toggle source
# File lib/intar.rb, line 274 def prompt_scan_history @prompt.scan_history do |l| next if l =~ /\A\\/ yield l end end
readline()
click to toggle source
# File lib/intar.rb, line 199 def readline r, @previous = @previous, nil r or @n += 1 begin cp = cur_prompt r l = @prompt.ask cp return if l.nil? @prompt.push l unless !r and @params[ :histhid] and l =~ /\A[ \t]+/ if r then r << $/ << l else r = l unless l.empty? end cp.strip! cp.gsub! /\e\[[0-9]*(;[0-9]*)*m/, "" @file = "#{self.class}/#{cp}" end until r switchcolor r end
search_history(num, pat)
click to toggle source
# File lib/intar.rb, line 524 def search_history num, pat r = Regexp.new pat num ||= 1 num = num.to_i.nonzero? extract_from_history { |l| if l =~ r then if num then break unless num > 0 num -= 1 end next l end } end
show_exception()
click to toggle source
# File lib/intar.rb, line 243 def show_exception unless $!.to_s.empty? then switchcolor 31, 1 print $! print " " unless $!.to_s =~ /\s\z/ end switchcolor 31, 22 puts "(#{$!.class})" switchcolor 33 $@.each { |b| r = b.starts_with? __FILE__ break if r and (b.rest r) =~ /\A:\d+:/ puts b } switchcolor end
show_history(n, m)
click to toggle source
# File lib/intar.rb, line 538 def show_history n, m i, j = 0, 0 extract_from_history { |l| i += 1 if i > m then j += 1 break if j > n next l end } end
switchcolor(*c)
click to toggle source
# File lib/intar.rb, line 187 def switchcolor *c s = color *c print s end
tempname()
click to toggle source
# File lib/intar.rb, line 503 def tempname t = Time.now.strftime "%Y%m%d-%H%M%S" File.expand_path "intar-#{t}-#$$.rb", ENV[ "TMPDR"]||"/tmp" end