Class: IRB::Irb

Inherits:
Object
  • Object
show all
Defined in:
lib/fancy_irb/irb_ext.rb

Constant Summary collapse

TPUT =
{
  :sc   => `tput sc`,
  :rc   => `tput rc`,
  :cuu1 => `tput cuu1`,
  :cuf1 => `tput cuf1`,
}

Instance Method Summary collapse

Instance Method Details

#colorize(string, color) ⇒ Object



27
28
29
# File 'lib/fancy_irb/irb_ext.rb', line 27

def colorize(string, color)
  Paint[string, *Array(color)]
end

#output_valueObject



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

def output_value
  # prepare prompts
  rocket    = colorize FancyIrb[:rocket_prompt], FancyIrb[:colorize, :rocket_prompt]
  no_rocket = colorize FancyIrb[:result_prompt], FancyIrb[:colorize, :result_prompt]
  
  # get_result and pass it into every format_output_proc
  result = FancyIrb[:result_proc][ @context ]

  output = Array( FancyIrb[:output_procs] ).
    inject( result.to_s ){ |output, formatter|
      formatter[ output ].to_s
    }

  # reset color
  print Paint::NOTHING

  # try to output in rocket mode (depending on rocket_mode setting)
  if FancyIrb[:rocket_mode] && !FancyIrb.skip_next_rocket
    # get lengths
    last_input               = @scanner.instance_variable_get( :@line )
    last_line_without_prompt = last_input.split("\n").last
    offset =  + FancyIrb.real_lengths[:input_prompt] + 1
    if last_line_without_prompt
      offset += last_line_without_prompt.display_size
    end
    screen_length = FancyIrb.current_length
    screen_lines  = FancyIrb.current_lines
    output_length = FancyIrb.real_lengths[:output]
    rocket_length = FancyIrb[:rocket_prompt].size
    stdout_lines  = FancyIrb.get_height

    # auto rocket mode
    if  screen_length > offset + rocket_length + output_length &&
        stdout_lines < screen_lines
      print TPUT[:sc] +                # save current cursor position
            TPUT[:cuu1]*stdout_lines + # move cursor upwards    to the original input line
            TPUT[:cuf1]*offset +       # move cursor rightwards to the original input offset
            rocket +                   # draw rocket prompt
            output +                   # draw output
            TPUT[:rc]                  # return to normal cursor position
      return
    end
  end
  # normal output mode
  FancyIrb.skip_next_rocket = false
  puts no_rocket + output
end

#prompt(*args, &block) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fancy_irb/irb_ext.rb', line 81

def prompt(*args, &block)
  print Paint::NOTHING
  prompt = prompt_non_fancy(*args, &block)

  # this is kinda hacky... but that's irb °_°
  indents = @scanner.indent*2
  FancyIrb.continue = true if args[0] == IRB.conf[:PROMPT][IRB.conf[:PROMPT_MODE]][:PROMPT_C]
  indents += 2 if FancyIrb.continue
  FancyIrb.real_lengths[:input_prompt] = prompt.size + indents
 
  colorized_prompt = colorize prompt, FancyIrb[:colorize, :input_prompt]
  if input_color = FancyIrb[:colorize, :input]
    colorized_prompt + Paint.color(*Array(input_color)) # NOTE: No reset, relies on next one
  else
    colorized_prompt
  end
end

#prompt_non_fancyObject

colorize prompt & input



80
# File 'lib/fancy_irb/irb_ext.rb', line 80

alias prompt_non_fancy prompt

#signal_status(name, *args, &block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/fancy_irb/irb_ext.rb', line 101

def signal_status(name, *args, &block)
  FancyIrb.continue = false
  FancyIrb.reset_height
  signal_status_non_fancy(name, *args, &block)
ensure
  if name == :IN_EVAL
    if FancyIrb.capture_irb_errors
      errors = FancyIrb.capture_irb_errors.string
      
      $stdout = FancyIrb.original_stdout
      FancyIrb.capture_irb_errors = nil
      FancyIrb.original_stdout    = nil
      
      unless errors.empty?
        warn colorize( errors.chomp, FancyIrb[:colorize, :irb_errors] )
      end
    end
  end#if
end

#signal_status_non_fancyObject

track height and capture irb errors (part 2)



100
# File 'lib/fancy_irb/irb_ext.rb', line 100

alias signal_status_non_fancy signal_status