Class: TreeRepl::Repl
- Inherits:
-
Object
- Object
- TreeRepl::Repl
- Defined in:
- lib/treerepl/repl.rb
Instance Attribute Summary collapse
-
#commands ⇒ Object
Returns the value of attribute commands.
-
#cur ⇒ Object
Returns the value of attribute cur.
-
#finder ⇒ Object
String -> TreeNode.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#root_class ⇒ Object
Returns the value of attribute root_class.
Instance Method Summary collapse
- #add_command(command_class) ⇒ Object
-
#cwd ⇒ Object
void -> string.
-
#eval_path(node, path) ⇒ Object
TreeNode string -> TreeNode.
-
#initialize(root_class) ⇒ Repl
constructor
A new instance of Repl.
- #loop(strings2cmds) ⇒ Object
-
#main(root) ⇒ Object
TreeNode -> Void.
-
#node_relative_to(node, name) ⇒ Object
TreeNode string -> TreeNode.
-
#path(from, dest) ⇒ Object
TreeNode TreeNode -> TreeNode[].
-
#use(user) ⇒ Object
———————————————————————- ‘Private’ ———————————————————————-.
Constructor Details
Instance Attribute Details
#commands ⇒ Object
Returns the value of attribute commands.
6 7 8 |
# File 'lib/treerepl/repl.rb', line 6 def commands @commands end |
#cur ⇒ Object
Returns the value of attribute cur.
7 8 9 |
# File 'lib/treerepl/repl.rb', line 7 def cur @cur end |
#finder ⇒ Object
String -> TreeNode
11 12 13 |
# File 'lib/treerepl/repl.rb', line 11 def finder @finder end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
5 6 7 |
# File 'lib/treerepl/repl.rb', line 5 def root @root end |
#root_class ⇒ Object
Returns the value of attribute root_class.
8 9 10 |
# File 'lib/treerepl/repl.rb', line 8 def root_class @root_class end |
Instance Method Details
#add_command(command_class) ⇒ Object
19 20 21 |
# File 'lib/treerepl/repl.rb', line 19 def add_command(command_class) @commands << command_class.new(self) end |
#cwd ⇒ Object
void -> string
113 114 115 |
# File 'lib/treerepl/repl.rb', line 113 def cwd path(root,cur).map {|n| n.name}.join('/') end |
#eval_path(node, path) ⇒ Object
TreeNode string -> TreeNode
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 107 108 109 110 |
# File 'lib/treerepl/repl.rb', line 78 def eval_path(node,path) path = '.' if not path or path == '' if path =~ /^\// args = ['/'] + path.gsub(/^\/+/,'').split(/\//) else args = path.split /\// end trav = node args.each do |arg| case arg when '.' trav = trav when '..' trav = trav.parent when '/' trav = root else found = false trav.children.each do |n| if n.name == arg trav = n found = true break end end if not found STDERR.puts 'Invalid path: ' + path return nil end end end return trav end |
#loop(strings2cmds) ⇒ Object
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 |
# File 'lib/treerepl/repl.rb', line 34 def loop(strings2cmds) print cwd + '> ' orig_line = STDIN.readline.strip line = orig_line.split(/ /) return false if line.empty? str = line.shift.downcase return true if str == 'quit' if not cur if str != 'use' and str != 'help' STDERR.puts 'You must set a user with \'use\' before anything else.' return end end args = line cmd = strings2cmds[str] if cmd begin cmd.exec args rescue Exception => e STDERR.puts 'Trouble executing: ' + orig_line raise e end else STDERR.puts 'Unknown command: ' + str end return false end |
#main(root) ⇒ Object
TreeNode -> Void
28 29 30 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 |
# File 'lib/treerepl/repl.rb', line 28 def main(root) use root strings2cmds = {} commands.each do |cmd| strings2cmds[cmd.name] = cmd end def loop(strings2cmds) print cwd + '> ' orig_line = STDIN.readline.strip line = orig_line.split(/ /) return false if line.empty? str = line.shift.downcase return true if str == 'quit' if not cur if str != 'use' and str != 'help' STDERR.puts 'You must set a user with \'use\' before anything else.' return end end args = line cmd = strings2cmds[str] if cmd begin cmd.exec args rescue Exception => e STDERR.puts 'Trouble executing: ' + orig_line raise e end else STDERR.puts 'Unknown command: ' + str end return false end while true break if loop strings2cmds end end |
#node_relative_to(node, name) ⇒ Object
TreeNode string -> TreeNode
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/treerepl/repl.rb', line 118 def node_relative_to(node,name) if name == '.' return node elsif name == '..' return node.parent elsif name == '/' return root else node.children.each do |n| if name == n.name return n end end end return nil end |
#path(from, dest) ⇒ Object
TreeNode TreeNode -> TreeNode[]
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/treerepl/repl.rb', line 136 def path(from,dest) res = [] trav = dest while trav res.push trav break if trav == from trav = trav.parent end return res.reverse end |
#use(user) ⇒ Object
‘Private’
70 71 72 73 74 75 |
# File 'lib/treerepl/repl.rb', line 70 def use(user) if user @cur = user @root = user end end |