./bin/cli/console.rb in lux-fw-0.5.37 vs ./bin/cli/console.rb in lux-fw-0.6.2

- old
+ new

@@ -1,61 +1,103 @@ -require 'pry' - class Object def cp data data = JSON.pretty_generate(data.to_hash) if data.respond_to?(:to_hash) Clipboard.copy data 'copied' end + # reload code changes + def reload! + Lux.config.on_reload_code.call :cli + end + + # prettify last sql command + def sql! sql=nil + require 'niceql' + puts Niceql::Prettifier.prettify_sql sql || $last_sql_command + end + + def c + Lux.error.clear_screen + end + # show method info - # show User, :secure_hash - def show klass, m - klass = klass.class unless klass.respond_to?(:new) - el = klass.instance_method(m) - puts el.source_location.or([]).join(':').yellow - puts '-' - puts el.source - nil + # m User, :secure_hash + def m object, mtd = nil + if mtd + info = object.method(mtd) + puts info.source_location.or([]).join(':').yellow + puts '-' + puts info.source + nil + else + if object.respond_to?(:superclass) + object.methods - object.superclass.methods + else + object.methods - object.class.superclass.methods + end + end end end - ARGV[0] = 'console' if ARGV[0] == 'c' LuxCli.class_eval do desc :console, 'Start console' - def console - $lux_start_time = Time.now + def console *args + ENV['LUX_ENV'] = 'clre' - require 'awesome_print' - require 'clipboard' - require './config/application' + require 'amazing_print' + require './config/app' - Lux.config.dump_errors = true - Lux.config.log_to_stdout = true + # create mock session + Lux::Current.new '/' if File.exist?('./config/console.rb') puts '* loading ./config/console.rb' require './config/console' else puts '* ./config/console.rb not found' end - # AwesomePrint.pry! - # nice object dump in console - Pry.print = proc { |output, data| - out = if data.is_a?(Hash) - data.class.to_s+"\n"+JSON.pretty_generate(data).gsub(/"(\w+)":/) { '"%s":' % $1.yellow } + Pry.pager = false + + Pry.config.print = proc do |output, value| + if value.is_a?(Method) + output.puts value.inspect + elsif value.is_a?(String) + output.puts value else - data.ai + ap value end + end - output.puts out - } + if args.first + command = args.join(' ') - # create mock session - Lux::Current.new '/' + if command.ends_with?('.rb') + puts 'Load : %s' % command.light_blue + load command + else + puts 'Command : %s' % command.light_blue + data = eval command + puts '-' + Pry.config.print.call $stdout, data + end + else + # custom history loader + history = Pathname.new Lux.root.join('./.pry_history') - Pry.start + Thread.new do + # load in started pry session + sleep 0.5 + if history.exist? + lines = history.read.split($/).uniq - ['exit'] + lines.each {|l| Pry.history.push(l) } + end + end + Pry.start + + history.write Pry.history.to_a.uniq.last(100).join($/) + end end -end \ No newline at end of file +end