Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactor to use modules

  • Loading branch information...
commit 17893bf134d7f3cae8db3a030bf530d35cb71dc4 1 parent e27e6ae
Mat Trudel authored

Showing 2 changed files with 88 additions and 72 deletions. Show diff stats Hide diff stats

  1. +77 72 lib/base.rb
  2. +11 0 lib/menu.rb
149 lib/base.rb
... ... @@ -1,86 +1,91 @@
1 1 require 'readline'
2 2 require 'active_support'
  3 +require 'menu'
3 4
4   -class MenuTree
5   - attr_accessor :directory
6   - attr_accessor :parent
7   - attr_accessor :prompt
8   -
9   - def initialize(directory, parent = "")
10   - @directory = directory
11   - @parent = parent
12   - if @parent.is_a? self.class
13   - @prompt = @parent.prompt + "/" + File.basename(@directory)
14   - else
15   - @prompt = @parent
16   - end
17   - end
18   -
19   - def run_command(cmds)
20   - Readline.completion_append_character = " "
21   - Readline.completion_proc = Proc.new do |str|
22   - commands(str)
  5 +module MenuTree
  6 + class Base
  7 + attr_accessor :directory
  8 + attr_accessor :parent
  9 + attr_accessor :prompt
  10 + attr_accessor :instance
  11 +
  12 + def initialize(directory, parent = "")
  13 + @directory = directory
  14 + @parent = parent
  15 + if @parent.is_a? self.class
  16 + @prompt = @parent.prompt + "/" + File.basename(@directory)
  17 + else
  18 + @prompt = @parent
  19 + end
  20 + file = File.join(@directory, "menu.rb")
  21 + @instance = MenuTree::Menu.new(self)
  22 + @instance.instance_eval(File.read(file), file) if File.exists? file
23 23 end
24 24
25   - one_shot = true unless cmds.empty?
26   - to_run = cmds.shift
27   - while true do
28   - if (to_run.nil?)
29   - cmds = Readline.readline("#{@prompt}> ", true).split
30   - to_run = cmds.shift
  25 + def repl(cmds)
  26 + Readline.completion_append_character = " "
  27 + Readline.completion_proc = Proc.new do |str|
  28 + commands(str)
31 29 end
32   -
33   - # If it's a builtin, run it
34   - if (to_run.nil?)
35   - next
36   - elsif (to_run == "help" or to_run == "?")
37   - display_help
38   - elsif (to_run == "quit")
39   - exit
40   - elsif (to_run == "..")
41   - return
42   - elsif File.exists?(File.join(@directory, "#{to_run}.rb"))
43   - # If we have a .rb file, run it with the rest of cmd as arguments
44   - # TODO -- use .extend?
45   - require File.join(@directory, "#{to_run}")
46   - eval(to_run.camelize).new.run(cmds)
47   - elsif File.directory?(File.join(@directory, to_run))
48   - menu = MenuTree.new(File.join(@directory, to_run), self)
49   - menu.run_command(cmds)
50   - elsif File.exists?(File.join(@directory, "default.rb"))
51   - # TODO let default handle it, allowing us to have proxy menus
52   - else
53   - puts "Unknown command #{to_run}"
  30 +
  31 + one_shot = true unless cmds.empty?
  32 + to_run = cmds.shift
  33 + while true do
  34 + if (to_run.nil?)
  35 + cmds = Readline.readline("#{@prompt}> ", true).split
  36 + to_run = cmds.shift
  37 + end
  38 +
  39 + # If it's a builtin, run it
  40 + if (to_run.nil?)
  41 + next
  42 + elsif (to_run == "help" or to_run == "?")
  43 + display_help
  44 + elsif (to_run == "quit")
  45 + exit
  46 + elsif (to_run == "..")
  47 + return
  48 + elsif File.directory?(File.join(@directory, to_run))
  49 + menu = MenuTree::Base.new(File.join(@directory, to_run), self)
  50 + menu.repl(cmds)
  51 + else
  52 + @instance.send to_run, cmds rescue puts "Unknown command #{to_run}"
  53 + end
  54 + to_run = nil
  55 + return if one_shot
54 56 end
55   - to_run = nil
56   - return if one_shot
57 57 end
58   - end
59   -
60   - def commands(prefix='')
61   - completions = []
62   - Dir.new(@directory).each do |command|
63   - if command =~ /^[^\.]/ and command =~ /^#{Regexp.escape(prefix)}/ and File.file?(File.join(@directory, command))
64   - completions << File.basename(command, '.rb')
65   - end
  58 +
  59 + def commands(prefix='')
  60 + (instance_commands + submenus).select { |command| command =~ /^#{Regexp.escape(prefix)}/ }
66 61 end
67   - completions
68   - end
69 62
70   - def display_help
71   - puts <<EOF
72   -Builtin commands:
73   - help:\t\t\t\tDisplays this message
74   - quit:\t\t\t\tQuits this session
75   - ..: \t\t\t\tGoes up a level in the menu (or quits if at the root)
76   -
77   -#{@parent} commands:
78   -EOF
  63 + def instance_commands
  64 + @instance.methods - MenuTree::Menu.instance_methods
  65 + end
  66 +
  67 + def submenus
  68 + Dir.new(@directory).select { |entry| entry =~ /^[^\.]/ and File.directory?(entry) }
  69 + end
  70 +
  71 + def display_help
  72 + # TODO
  73 + puts <<EOF
  74 + Builtin commands:
  75 + help:\t\t\t\tDisplays this message
  76 + quit:\t\t\t\tQuits this session
  77 + ..: \t\t\t\tGoes up a level in the menu (or quits if at the root)
79 78
80   - # TODO display some help for each submenu
81   - commands.each do |cmd|
82   - require File.join(@directory, "#{cmd}")
83   - puts " #{cmd}:\t\t\t\t#{eval(cmd.camelize).new.description}"
  79 + #{@prompt} commands:
  80 +EOF
  81 + instance_commands.each do |cmd|
  82 + puts "#{cmd}"
  83 + end
  84 +
  85 + puts "Submenus:"
  86 + submenus.each do |submenu|
  87 + puts "#{submenu}"
  88 + end
84 89 end
85 90 end
86 91 end
11 lib/menu.rb
... ... @@ -0,0 +1,11 @@
  1 +module MenuTree
  2 + class Menu
  3 + def initialize(parent_tree)
  4 + @parent_tree = parent_tree
  5 + end
  6 +
  7 + def menutree(&block)
  8 + self.instance_exec(@parent_tree, &block) if block_given?
  9 + end
  10 + end
  11 +end

0 comments on commit 17893bf

Please sign in to comment.
Something went wrong with that request. Please try again.