Module: Ripl::AutoIndent

Defined in:
lib/ripl/auto_indent.rb

Constant Summary collapse

VERSION =
'0.2.0'
TPUT =
{
  :cuu1 => "\e[A", # up
  :sc   => "\e7",  # save
  :rc   => "\e8",  # load
}

Instance Method Summary collapse

Instance Method Details

#before_loopObject



14
15
16
17
# File 'lib/ripl/auto_indent.rb', line 14

def before_loop
  @current_indent = 0
  super
end

#loop_eval(input) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/ripl/auto_indent.rb', line 34

def loop_eval(input)
  last_indent     = ( @current_indent ||= 0 )
  @current_indent = RubyIndentation[ @buffer ? @buffer*";"+";"+input : input ]

  if config[:auto_indent_rewrite] && @current_indent < last_indent
    rewrite_line last_indent - @current_indent
  end

  super # (@buffer ? @buffer + input : input)
end

#promptObject



19
20
21
# File 'lib/ripl/auto_indent.rb', line 19

def prompt
  @buffer ? super + config[:auto_indent_space]*@current_indent : super
end

#rewrite_line(append_indents = 0) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/ripl/auto_indent.rb', line 23

def rewrite_line(append_indents = 0)
  print_method = defined?(Ripl::ColorStreams) ? :real_write : :write
  $stdout.send print_method,
    TPUT[:sc] +
    TPUT[:cuu1] +
    prompt + 
    @input +
    config[:auto_indent_space]*append_indents +
    TPUT[:rc]
end