Class: Resedit::App
- Inherits:
-
Object
- Object
- Resedit::App
- Defined in:
- lib/resedit/app/app.rb
Constant Summary collapse
- HIST_FILE =
"~/.resedithist"
Instance Attribute Summary collapse
-
#cmdInterface ⇒ Object
readonly
Returns the value of attribute cmdInterface.
-
#cmds ⇒ Object
readonly
Returns the value of attribute cmds.
-
#col ⇒ Object
readonly
Returns the value of attribute col.
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#copyright ⇒ Object
readonly
Returns the value of attribute copyright.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#shell ⇒ Object
readonly
Returns the value of attribute shell.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
- #commandInterface ⇒ Object
-
#initialize(name, version, commands, cmdInterface = true, copyright = "(c) old-games.ru") ⇒ App
constructor
A new instance of App.
- #log(fmt, *args) ⇒ Object
- #logd(fmt, *args) ⇒ Object
- #loge(fmt, *args) ⇒ Object
- #parseCommand(string) ⇒ Object
- #quit ⇒ Object
- #run ⇒ Object
- #runCommand(string) ⇒ Object
- #setShell(sname) ⇒ Object
Constructor Details
#initialize(name, version, commands, cmdInterface = true, copyright = "(c) old-games.ru") ⇒ App
Returns a new instance of App.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/resedit/app/app.rb', line 17 def initialize(name, version, commands, cmdInterface=true, copyright="(c) old-games.ru") @@instance = self @name, @version, @copyright = name, version, copyright @cmdInterface = cmdInterface @logger = Logger.new(STDOUT) @logger.level= Logger::INFO logger.formatter = proc { |severity, datetime, progname, msg| msg } @shell = nil; @commands = [] @commands += commands if commands @commands += [HelpCommand.new(), VersionCommand.new(), ExitCommand.new(), ScriptCommand.new(), MZCommand.new(), ShellCommand.new()] @cmds={} @commands.each{|c| c.names.each{|n| @cmds[n] = c; } } @col = Colorizer.instance() end |
Instance Attribute Details
#cmdInterface ⇒ Object (readonly)
Returns the value of attribute cmdInterface.
11 12 13 |
# File 'lib/resedit/app/app.rb', line 11 def cmdInterface @cmdInterface end |
#cmds ⇒ Object (readonly)
Returns the value of attribute cmds.
11 12 13 |
# File 'lib/resedit/app/app.rb', line 11 def cmds @cmds end |
#col ⇒ Object (readonly)
Returns the value of attribute col.
11 12 13 |
# File 'lib/resedit/app/app.rb', line 11 def col @col end |
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
11 12 13 |
# File 'lib/resedit/app/app.rb', line 11 def commands @commands end |
#copyright ⇒ Object (readonly)
Returns the value of attribute copyright.
11 12 13 |
# File 'lib/resedit/app/app.rb', line 11 def copyright @copyright end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
11 12 13 |
# File 'lib/resedit/app/app.rb', line 11 def logger @logger end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/resedit/app/app.rb', line 11 def name @name end |
#shell ⇒ Object (readonly)
Returns the value of attribute shell.
11 12 13 |
# File 'lib/resedit/app/app.rb', line 11 def shell @shell end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
11 12 13 |
# File 'lib/resedit/app/app.rb', line 11 def version @version end |
Class Method Details
.get ⇒ Object
13 14 15 |
# File 'lib/resedit/app/app.rb', line 13 def self.get() return @@instance end |
Instance Method Details
#commandInterface ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/resedit/app/app.rb', line 104 def commandInterface() @cmds['version'].run('') begin open(File.(HIST_FILE),"r").each_line {|ln| ln.chomp! Readline::HISTORY.push(ln) if ln.length>0 } rescue end @stop=false while(!@stop) sh=@shell ? " "+@shell : "" begin cmd = Readline.readline("#{@name}#{sh}>", true) Readline::HISTORY.pop if cmd=='' || (Readline::HISTORY.length>1 && cmd==Readline::HISTORY[-2]) runCommand(cmd) rescue StandardError => e puts @col.red("Error: #{e.to_s()}") puts e.backtrace if App::get().logger.level == Logger::DEBUG end end return 0 end |
#log(fmt, *args) ⇒ Object
44 45 46 |
# File 'lib/resedit/app/app.rb', line 44 def log(fmt, *args) @logger.info(sprintf(fmt+"\n",*args)) end |
#logd(fmt, *args) ⇒ Object
41 42 43 |
# File 'lib/resedit/app/app.rb', line 41 def logd(fmt, *args) @logger.debug(@col.gray(sprintf(fmt+"\n",*args))) end |
#loge(fmt, *args) ⇒ Object
47 48 49 |
# File 'lib/resedit/app/app.rb', line 47 def loge(fmt, *args) @logger.error(@col.red(sprintf(fmt+"\n",*args))) end |
#parseCommand(string) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/resedit/app/app.rb', line 71 def parseCommand(string) cmd = [] string.split().each {|w| if w[0]=='-' && w.length()>2 && w[1]!='-' w[1..-1].each_char{|c| cmd+=["-#{c}"] } else cmd+=[w] end } logd("parsing command #{cmd.to_s}") return nil if cmd.length()==0 || cmd[0][0]=='#' c = @cmds[cmd[0]] raise "Unknown command: #{cmd[0]}" if !c prms = c.parseParams(cmd[1..-1]) return c,prms end |
#quit ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/resedit/app/app.rb', line 52 def quit begin strt = Readline::HISTORY.length-64 open(File.(HIST_FILE),"w"){|f| Readline::HISTORY.each.with_index{|ln,i| f.write(ln+"\n") if i > strt } } rescue end @stop=true end |
#run ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/resedit/app/app.rb', line 129 def run() begin if ARGV.length()==0 commandInterface() if @cmdInterface if !@cmdInterface puts "Command not specified. Known commands are:" runCommand('help') end else if (@cmds[ARGV[0]]) #check command runCommand(ARGV.join(' ')) commandInterface() if @shell elsif ARGV.length()==1 && File.exists?(ARGV[0]) #check script runCommand('script '+ARGV[0]) else raise "unknown command #{ARGV[0]}" end end exit(0) rescue StandardError => e puts "Error: #{e.to_s()}" puts e.backtrace exit(1) end end |
#runCommand(string) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/resedit/app/app.rb', line 91 def runCommand(string) if @shell cmd=string.split()[0] if !@cmds[cmd] || !['Resedit::ExitCommand','Resedit::ShellCommand'].include?(@cmds[cmd].class.name) string = @shell+" "+string end end logd("running command %s", string) cmd = parseCommand(string) cmd[0].run(cmd[1]) end |
#setShell(sname) ⇒ Object
65 66 67 68 69 |
# File 'lib/resedit/app/app.rb', line 65 def setShell(sname) sname=nil if sname.length==0 raise "Unknown shell: "+sname if sname && !@cmds[sname] @shell = sname end |