Module: Ripl::Rocket::Shell

Defined in:
lib/ripl/rocket.rb

Instance Method Summary collapse

Instance Method Details

#loop_eval(input) ⇒ Object



59
60
61
62
# File 'lib/ripl/rocket.rb', line 59

def loop_eval(input)
  Ripl::Rocket.reset_output_height
  super
end


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
# File 'lib/ripl/rocket.rb', line 64

def print_result(result)
  return if @error_raised
  if config[:rocket_mode]
    # get important data
    screen_length = `tput cols`.to_i
    screen_height = `tput lines`.to_i
    last_line_without_prompt = @input.split("\n").last || ''
    offset = (last_line_without_prompt.display_size + prompt.display_size ) % screen_length.to_i
    output_length = result.inspect.display_size
    rocket_length = config[:rocket_prompt].display_size
    stdout_height  = Ripl::Rocket.output_height
    color_config = config[:rocket_color]
    color_code   = !color_config || color_config.to_s[/^[\d;]+$/] ?
      color_config : Ripl::Rocket::COLORS[color_config.to_sym]
    colored_rocket = color_code ? "\e[#{ color_code }m#{ config[:rocket_prompt] }\e[0;0m" : config[:rocket_prompt]

    # auto rocket mode:  only display rocket if line has enough space left
    line_too_long   = screen_length <= offset + rocket_length + output_length
    height_too_long = stdout_height >= screen_height
    if !(config[:rocket_mode] == :auto && ( line_too_long || height_too_long ) )
      if !height_too_long
        print TPUT[:sc] +                   # save cursor position
              TPUT[:cuu1]*stdout_height +   # move cursor upwards    to the original input line
              TPUT[:cuf1]*offset +          # move cursor rightwards to the original input offset
              colored_rocket +              # draw rocket prompt
              format_result(result).sub( /^#{result_prompt}/, '' ) +         # draw result (without prompt)
              TPUT[:rc] +                   # restore cursor position
              ( config[:rocket_mode] != :auto && line_too_long  ? "\n" : '') # add a line for always-rocket mode

      else # too much stdout, but in "always rocket mode"
        puts colored_rocket +              # draw rocket prompt
             format_result(result).sub( /^#{result_prompt}/, '' )            # draw result (without prompt)
      end
      return
    end
  end
  # else: no rocket ;)
  super
end