Class: Aio::Module::InputStyle::Console

Inherits:
Aio::Module::InputStyle show all
Includes:
Aio::Module, Ui::Verbose
Defined in:
lib/modules/input/style/console.rb

Defined Under Namespace

Classes: CmdContext, CmdState, ContextState, EmptyState, Machine, State

Instance Attribute Summary

Attributes inherited from Aio::Module::InputStyle

#ext_info, #input_file, #input_info

Instance Method Summary collapse

Methods included from Ui::Verbose

#clear_line, #print_error, #print_good, #progress_bar

Methods inherited from Aio::Module::InputStyle

#author, #check_file, #description, #file_suffix, #file_suffix?, #license, #pass_file, #pass_file?, #platform, #set_defaults, #type

Constructor Details

#initializeConsole

Returns a new instance of Console.



165
166
167
168
169
170
171
172
# File 'lib/modules/input/style/console.rb', line 165

def initialize
  super({
    :author				=> "Elin",
    :description 	=> "这个模块用于对命令行格式的输入处理",
    :platform 		=> "all",
  })
  @machine = Machine.new
end

Instance Method Details

#merge_regs(arr_regs) ⇒ Object



219
220
221
222
223
# File 'lib/modules/input/style/console.rb', line 219

def merge_regs(arr_regs)
  regs = Aio::Base::Toolkit::Regexp.merge(arr_regs).to_s
  regs.gsub!('(^', '(')
  ::Regexp.new(regs)
end

#parseObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/modules/input/style/console.rb', line 174

def parse
  dir = self.input_file
  dir_pn = Pathname.new(dir)

  # 加载所有的cmd正则表达式
  @machine.regs = merge_regs(ext_info[:cmds_reg])

  # 如果是单个文件的话,直接进入文件解析模式
  if dir_pn.file?
    parse_file(dir_pn)
  else

    # 如果是文件夹的话,那么就对每个文件进行解析
    Find.find(dir) do |file|
      fn = Pathname.new(file)
      parse_file(file) if fn.file?
    end
  end

  @machine.cmd_context.all_info.each do |info|
    yield info[0], info[1], info[2]
  end
end

#parse_file(file) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/modules/input/style/console.rb', line 198

def parse_file(file)
  fo = File.open(file, "r+", :encoding => "utf-8")

  fo.each_line do |line|
    begin 
      line = Aio::Base::Toolkit::String.safe(line)
      line = line.strip
    rescue => e
      print_error line + " : " + e.message
    end

    @machine.parse_line(line)
  end

  # 如果最后没有done结束,则手动结束
  if @machine.state == ContextState
    @machine.cmd_context.done 
    @machine.to_cmd_state
  end
end