Module: Ripl::Fresh

Defined in:
lib/ripl/fresh.rb,
lib/ripl/fresh/commands.rb

Defined Under Namespace

Modules: Commands

Constant Summary collapse

VERSION =
'0.2.1'

Instance Method Summary collapse

Instance Method Details

#before_loopObject



9
10
11
12
13
14
15
16
# File 'lib/ripl/fresh.rb', line 9

def before_loop
  @command_mode = :ruby
  @real_buffer  = nil
  super
  # register bond completion
  Ripl.config[:completion][:gems] ||= []
  Ripl.config[:completion][:gems] << 'ripl-fresh'
end

#get_inputObject

determine @command_mode



19
20
21
22
23
24
25
26
27
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
# File 'lib/ripl/fresh.rb', line 19

def get_input
  command_line = super

  # This case statement decides the command mode,
  #  and which part of the input should be used for what...
  #  Note: Regexp match groups are used!
  @result_storage = @result_operator = nil

  @command_mode = case command_line
  # force ruby with a space
  when /^ /
    :ruby
  # regexp match shell commands
  when *Array( Ripl.config[:fresh_patterns] ).compact
    command_line     = $~[:command_line]     if $~.names.include? 'command_line'
    command          = $~[:command]          if $~.names.include? 'command'
    @result_operator = $~[:result_operator]  if $~.names.include? 'result_operator'
    @result_storage  = $~[:result_storage]   if $~.names.include? 'result_storage'
    forced           = !! $~[:force]         if $~.names.include? 'force'

    if forced
      :system
    elsif Ripl.config[:fresh_ruby_commands].include?( command )
      :ruby
    elsif Ripl.config[:fresh_mixed_commands].include?( command )
      :mixed
    elsif Ripl.config[:fresh_system_commands].include?( command )
      :system
    elsif Kernel.respond_to? command.to_sym
      :ruby
    else
      Ripl.config[:fresh_unknown_command_mode]
    end
  else
    Ripl.config[:fresh_default_mode]
  end

  command_line
end

#loop_eval(input) ⇒ Object

get result (depending on @command_mode)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
# File 'lib/ripl/fresh.rb', line 60

def loop_eval(input)
  if input == ''
    @command_mode = :system and return
  end

  case @command_mode
  when :system # generate ruby code to execute the command
    if !@result_storage
      ruby_command_code = "_ = system '#{ input }'\n"
    else
      temp_file = "/tmp/ripl-fresh_#{ rand 12345678901234567890 }"
      ruby_command_code = "_ = system '#{ input } 2>&1', :out => '#{ temp_file }'\n"
      
      # assign result to result storage variable
      case @result_operator
      when '=>', '=>>'
        result_literal   = "[]"
        formatted_result = "File.read('#{ temp_file }').split($/)"
        operator = @result_operator == '=>>' ? '+=' : '='
      when '~>', '~>>'
        result_literal = "''"
        formatted_result = "File.read('#{ temp_file }')"
        operator = @result_operator == '~>>' ? '<<' : '='
      end
      ruby_command_code << %Q%
        #{ @result_storage } ||= #{ result_literal }
        #{ @result_storage } #{ operator } #{ formatted_result }
        FileUtils.rm '#{ temp_file }'
      %
    end

    # ruby_command_code << "raise( SystemCallError.new $?.exitstatus ) if !_\n" # easy auto indent
    ruby_command_code << "if !_
                            raise( SystemCallError.new $?.exitstatus )
                          end;"
    
    super @input = ruby_command_code

  when :mixed # call the ruby method, but with shell style arguments TODO more shell like (e.g. "")
    method_name, *args = *input.split
    super @input = "#{ method_name }(*#{ args })"

  else # good old :ruby
    super
  end
end

system commands don’t have output values and Ruby is displayed normally



108
109
110
111
112
113
114
115
116
# File 'lib/ripl/fresh.rb', line 108

def print_result(result)
  if @error_raised ||
       @command_mode == :system ||
       @command_mode == :mixed && (!result || result == '')
    # don't display anything
  else
    super # puts(format_result(result))
  end
end