/usr/lib/ruby/vendor_ruby/wirble.rb is in ruby-wirble 0.1.3-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 58 59 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 | require 'ostruct'
#
# Wirble: A collection of useful Irb features.
#
# To use, add the following to your ~/.irbrc:
#
# require 'rubygems'
# require 'wirble'
# Wirble.init
#
# If you want color in Irb, add this to your ~/.irbrc as well:
#
# Wirble.colorize
#
# Note: I spent a fair amount of time documenting this code in the
# README. If you've installed via RubyGems, root around your cache a
# little bit (or fire up gem_server) and read it before you tear your
# hair out sifting through the code below.
#
module Wirble
VERSION = '0.1.3'
#
# Load internal Ruby features, including pp, tab-completion,
# and a simple prompt.
#
module Internals
# list of internal libraries to automatically load
LIBRARIES = %w{pp irb/completion}
#
# load libraries
#
def self.init_libraries
LIBRARIES.each do |lib|
begin
require lib
rescue LoadError
nil
end
end
end
#
# Set a simple prompt, unless a custom one has been specified.
#
def self.init_prompt
# set the prompt
if IRB.conf[:PROMPT_MODE] == :DEFAULT
IRB.conf[:PROMPT_MODE] = :SIMPLE
end
end
#
# Load all Ruby internal features.
#
def self.init(opt = nil)
init_libraries unless opt && opt[:skip_libraries]
init_prompt unless opt && opt[:skip_prompt]
end
end
#
# Basic IRB history support. This is based on the tips from
# http://wiki.rubygarden.org/Ruby/page/show/Irb/TipsAndTricks
#
class History
DEFAULTS = {
:history_path => ENV['IRB_HISTORY_FILE'] || "~/.irb_history",
:history_size => (ENV['IRB_HISTORY_SIZE'] || 1000).to_i,
:history_perms => File::WRONLY | File::CREAT | File::TRUNC,
}
private
def say(*args)
puts(*args) if @verbose
end
def cfg(key)
@opt["history_#{key}".intern]
end
def save_history
path, max_size, perms = %w{path size perms}.map { |v| cfg(v) }
# read lines from history, and truncate the list (if necessary)
lines = Readline::HISTORY.to_a.uniq
lines = lines[-max_size, -1] if lines.size > max_size
# write the history file
real_path = File.expand_path(path)
File.open(real_path, perms) { |fh| fh.puts lines }
say 'Saved %d lines to history file %s.' % [lines.size, path]
end
def load_history
# expand history file and make sure it exists
real_path = File.expand_path(cfg('path'))
unless File.exist?(real_path)
say "History file #{real_path} doesn't exist."
return
end
# read lines from file and add them to history
lines = File.readlines(real_path).map { |line| line.chomp }
Readline::HISTORY.push(*lines)
say 'Read %d lines from history file %s' % [lines.size, cfg('path')]
end
public
def initialize(opt = nil)
@opt = DEFAULTS.merge(opt || {})
return unless defined? Readline::HISTORY
load_history
Kernel.at_exit { save_history }
end
end
#
# Add color support to IRB.
#
module Colorize
#
# Tokenize an inspection string.
#
module Tokenizer
def self.tokenize(str)
raise 'missing block' unless block_given?
chars = str.split(//)
# $stderr.puts "DEBUG: chars = #{chars.join(',')}"
state, val, i, lc = [], '', 0, nil
while i <= chars.size
repeat = false
c = chars[i]
# $stderr.puts "DEBUG: state = #{state}"
case state[-1]
when nil
case c
when ':'
state << :symbol
when '"'
state << :string
when '#'
state << :object
when /[a-z]/i
state << :keyword
repeat = true
when /[0-9-]/
state << :number
repeat = true
when '{'
yield :open_hash, '{'
when '['
yield :open_array, '['
when ']'
yield :close_array, ']'
when '}'
yield :close_hash, '}'
when /\s/
yield :whitespace, c
when ','
yield :comma, ','
when '>'
yield :refers, '=>' if lc == '='
when '.'
yield :range, '..' if lc == '.'
when '='
# ignore these, they're used elsewhere
nil
else
# $stderr.puts "DEBUG: ignoring char #{c}"
end
when :symbol
case c
# XXX: should have =, but that messes up foo=>bar
when /[a-z0-9_!?]/
val << c
else
yield :symbol_prefix, ':'
yield state[-1], val
state.pop; val = ''
repeat = true
end
when :string
case c
when '"'
if lc == "\\"
val[-1] = ?"
else
yield :open_string, '"'
yield state[-1], val
state.pop; val = ''
yield :close_string, '"'
end
else
val << c
end
when :keyword
case c
when /[a-z0-9_]/i
val << c
else
# is this a class?
st = val =~ /^[A-Z]/ ? :class : state[-1]
yield st, val
state.pop; val = ''
repeat = true
end
when :number
case c
when /[0-9e-]/
val << c
when '.'
if lc == '.'
val[/\.$/] = ''
yield state[-1], val
state.pop; val = ''
yield :range, '..'
else
val << c
end
else
yield state[-1], val
state.pop; val = ''
repeat = true
end
when :object
case c
when '<'
yield :open_object, '#<'
state << :object_class
when ':'
state << :object_addr
when '@'
state << :object_line
when '>'
yield :close_object, '>'
state.pop; val = ''
end
when :object_class
case c
when ':'
yield state[-1], val
state.pop; val = ''
repeat = true
else
val << c
end
when :object_addr
case c
when '>'
when '@'
yield :object_addr_prefix, ':'
yield state[-1], val
state.pop; val = ''
repeat = true
else
val << c
end
when :object_line
case c
when '>'
yield :object_line_prefix, '@'
yield state[-1], val
state.pop; val = ''
repeat = true
else
val << c
end
else
raise "unknown state #{state}"
end
unless repeat
i += 1
lc = c
end
end
end
end
#
# Terminal escape codes for colors.
#
module Color
COLORS = {
:nothing => '0;0',
:black => '0;30',
:red => '0;31',
:green => '0;32',
:brown => '0;33',
:blue => '0;34',
:cyan => '0;36',
:purple => '0;35',
:light_gray => '0;37',
:dark_gray => '1;30',
:light_red => '1;31',
:light_green => '1;32',
:yellow => '1;33',
:light_blue => '1;34',
:light_cyan => '1;36',
:light_purple => '1;35',
:white => '1;37',
}
#
# Return the escape code for a given color.
#
def self.escape(key)
COLORS.key?(key) && "\033[#{COLORS[key]}m"
end
end
#
# Default Wirble color scheme.
#
DEFAULT_COLORS = {
# delimiter colors
:comma => :blue,
:refers => :blue,
# container colors (hash and array)
:open_hash => :green,
:close_hash => :green,
:open_array => :green,
:close_array => :green,
# object colors
:open_object => :light_red,
:object_class => :white,
:object_addr_prefix => :blue,
:object_line_prefix => :blue,
:close_object => :light_red,
# symbol colors
:symbol => :yellow,
:symbol_prefix => :yellow,
# string colors
:open_string => :red,
:string => :cyan,
:close_string => :red,
# misc colors
:number => :cyan,
:keyword => :green,
:class => :light_green,
:range => :red,
}
#
# Fruity testing colors.
#
TESTING_COLORS = {
:comma => :red,
:refers => :red,
:open_hash => :blue,
:close_hash => :blue,
:open_array => :green,
:close_array => :green,
:open_object => :light_red,
:object_class => :light_green,
:object_addr => :purple,
:object_line => :light_purple,
:close_object => :light_red,
:symbol => :yellow,
:symbol_prefix => :yellow,
:number => :cyan,
:string => :cyan,
:keyword => :white,
:range => :light_blue,
}
#
# Set color map to hash
#
def self.colors=(hash)
@colors = hash
end
#
# Get current color map
#
def self.colors
@colors ||= {}.update(DEFAULT_COLORS)
end
#
# Return a string with the given color.
#
def self.colorize_string(str, color)
col, nocol = [color, :nothing].map { |key| Color.escape(key) }
col ? "#{col}#{str}#{nocol}" : str
end
#
# Colorize the results of inspect
#
def self.colorize(str)
begin
ret, nocol = '', Color.escape(:nothing)
Tokenizer.tokenize(str) do |tok, val|
# c = Color.escape(colors[tok])
ret << colorize_string(val, colors[tok])
end
ret
rescue
# catch any errors from the tokenizer (just in case)
str
end
end
#
# Enable colorized IRB results.
#
def self.enable(custom_colors = nil)
# if there's a better way to do this, I'm all ears.
::IRB::Irb.class_eval do
alias :non_color_output_value :output_value
def output_value
if @context.inspect?
val = Colorize.colorize(@context.last_value.inspect)
printf @context.return_format, val
else
printf @context.return_format, @context.last_value
end
end
end
colors = custom_colors if custom_colors
end
#
# Disable colorized IRB results.
#
def self.disable
::IRB::Irb.class_eval do
alias :output_value :non_color_output_value
end
end
end
#
# Convenient shortcut methods.
#
module Shortcuts
#
# Print object methods, sorted by name. (excluding methods that
# exist in the class Object) .
#
def po(o)
o.methods.sort - Object.methods
end
#
# Print object constants, sorted by name.
#
def poc(o)
o.constants.sort
end
end
#
# Convenient shortcut for ri
#
module RiShortcut
def self.init
Kernel.class_eval {
def ri(arg)
puts `ri '#{arg}'`
end
}
Module.instance_eval {
def ri(meth=nil)
if meth
if instance_methods(false).include? meth.to_s
puts `ri #{self}##{meth}`
else
super
end
else
puts `ri #{self}`
end
end
}
end
end
#
# Enable color results.
#
def self.colorize(custom_colors = nil)
Colorize.enable(custom_colors)
end
#
# Load everything except color.
#
def self.init(opt = nil)
# make sure opt isn't nil
opt ||= {}
# load internal irb/ruby features
Internals.init(opt) unless opt && opt[:skip_internals]
# load the history
History.new(opt) unless opt && opt[:skip_history]
# load shortcuts
unless opt && opt[:skip_shortcuts]
# load ri shortcuts
RiShortcut.init
# include common shortcuts
Object.class_eval { include Shortcuts }
end
colorize(opt[:colors]) if opt && opt[:init_colors]
end
end
|