Class: Dispatches

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

Instance Method Summary collapse

Constructor Details

#initializeDispatches

Returns a new instance of Dispatches.



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
# File 'lib/itrp.rb', line 46

def initialize()

	# load handlers
	reload

	# root proc 
	@cmd_roots = { :any => ITRP::Cmd_root.new(Appenv) }
	@cmd_roots[:any].set_time_window 


	# build command tree 
	(ITRP::constants-[:Cmd, :Cmd_root]).each do |k|
		kls = ITRP::const_get(k)
		ins = kls.new(Appenv)
		@cmd_roots[ins.enabled_in_state] ||= ITRP::Cmd_root.new(Appenv) 
		@cmd_roots[ins.enabled_in_state].place_node(ins)
	end
	
	# hook up autocompletion 
	Readline.completion_proc = proc do |s| 
		buff = Readline.line_buffer()

		[Appenv.context, :any].uniq.collect do |ctx|
			node  = @cmd_roots[ctx].find_node(buff.split(' '))
			(node or @cmd_roots[:any]).completions(s) 
		end.flatten  
	end

	# history load 
       if File.exist? HISTFILE
           File.readlines(HISTFILE).each do |l|
               Readline::HISTORY.push(l.chop)
           end
       end

end

Instance Method Details

#helpObject



113
114
115
116
117
118
# File 'lib/itrp.rb', line 113

def help
	@cmd_roots.each do |k,v|
		print "Context :#{k}\n"
		v.treeprint 
	end 
end

#invoke(cmdline) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/itrp.rb', line 82

def invoke(cmdline)
	# system commands
	case cmdline.scan(/(\w+)/).flatten.first 
		when 'reload' ; reload 
		when 'quit'   ; quit 
		when 'help'   ; help
		when 'clear'  ; system("clear") 
		else;
	end 

	# dispatch to correct plugin w/ context
	node=nil
	[Appenv.context, :any].uniq.collect do |ctx|
		node  = @cmd_roots[ctx].find_node(cmdline.strip.split(' '))
		if not node.is_root?
			node.enter(cmdline.strip) 
			return
		end
	end
	node.notfound(cmdline) if node.is_root?
end

#quitObject



120
121
122
123
# File 'lib/itrp.rb', line 120

def quit
	savehistory
	exit 0
end

#reloadObject



104
105
106
107
108
109
110
111
# File 'lib/itrp.rb', line 104

def reload
	print("Loading command handlers ")
	Dir.glob("handlers/*rb") do |f|
		load(f)
		print('.')
	end
	print("done\n\n")
end

#savehistoryObject



125
126
127
128
129
130
131
# File 'lib/itrp.rb', line 125

def savehistory
       File.open( HISTFILE, "w") do |h|
           Readline::HISTORY.to_a.uniq.each do |l|
               h.write(l + "\n" ) 
           end
       end
end