From db7a4be846453bc0143f61532d4061a9cad95639 Mon Sep 17 00:00:00 2001 From: tomoya ishida Date: Sat, 28 Oct 2023 23:47:40 +0900 Subject: [PATCH] [ruby/irb] Easter egg for autocomplete mode (https://github.com/ruby/irb/pull/737) * Show easter-egg inside document dialog in autocomplete mode * Show more easter_egg(dancing or logo) when Alt+d is pressed in autocomplete mode https://github.com/ruby/irb/commit/a8e5cc7354 --- lib/irb/easter-egg.rb | 16 ++++++++++----- lib/irb/input-method.rb | 22 ++++++++++++++++----- lib/irb/ruby_logo.aa | 43 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 10 deletions(-) diff --git a/lib/irb/easter-egg.rb b/lib/irb/easter-egg.rb index 3e79692de9..1d7460abcd 100644 --- a/lib/irb/easter-egg.rb +++ b/lib/irb/easter-egg.rb @@ -98,15 +98,21 @@ module IRB end end + private def easter_egg_logo(type) + @easter_egg_logos ||= File.read(File.join(__dir__, 'ruby_logo.aa'), encoding: 'UTF-8:UTF-8') + .split(/TYPE: ([A-Z]+)\n/)[1..] + .each_slice(2) + .to_h + @easter_egg_logos[type.to_s.upcase] + end + private def easter_egg(type = nil) type ||= [:logo, :dancing].sample case type when :logo - File.open(File.join(__dir__, 'ruby_logo.aa')) do |f| - require "rdoc" - RDoc::RI::Driver.new.page do |io| - IO.copy_stream(f, io) - end + require "rdoc" + RDoc::RI::Driver.new.page do |io| + io.write easter_egg_logo(:large) end when :dancing begin diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb index 454b6db00b..cef65b7162 100644 --- a/lib/irb/input-method.rb +++ b/lib/irb/input-method.rb @@ -303,15 +303,20 @@ module IRB return nil if result.nil? or pointer.nil? or pointer < 0 name = doc_namespace.call(result[pointer]) + show_easter_egg = name&.match?(/\ARubyVM/) && !ENV['RUBY_YES_I_AM_NOT_A_NORMAL_USER'] options = {} options[:extra_doc_dirs] = IRB.conf[:EXTRA_DOC_DIRS] unless IRB.conf[:EXTRA_DOC_DIRS].empty? driver = RDoc::RI::Driver.new(options) if key.match?(dialog.name) - begin - driver.display_names([name]) - rescue RDoc::RI::Driver::NotFoundError + if show_easter_egg + IRB.__send__(:easter_egg) + else + begin + driver.display_names([name]) + rescue RDoc::RI::Driver::NotFoundError + end end end @@ -374,8 +379,15 @@ module IRB formatter.width = width dialog.trap_key = alt_d mod_key = RUBY_PLATFORM.match?(/darwin/) ? "Option" : "Alt" - message = "Press #{mod_key}+d to read the full document" - contents = [message] + doc.accept(formatter).split("\n") + if show_easter_egg + type = STDOUT.external_encoding == Encoding::UTF_8 ? :unicode : :ascii + contents = IRB.send(:easter_egg_logo, type).split("\n") + message = "Press #{mod_key}+d to see more" + contents[0][0, message.size] = message + else + message = "Press #{mod_key}+d to read the full document" + contents = [message] + doc.accept(formatter).split("\n") + end contents = contents.take(preferred_dialog_height) y = cursor_pos_to_render.y diff --git a/lib/irb/ruby_logo.aa b/lib/irb/ruby_logo.aa index a34a3e2f28..61fe22c94a 100644 --- a/lib/irb/ruby_logo.aa +++ b/lib/irb/ruby_logo.aa @@ -1,3 +1,4 @@ +TYPE: LARGE -+smJYYN?mm- HB"BBYT TQg NggT @@ -35,3 +36,45 @@ m7 NW H N HSVO1z=?11- NgTH bB kH WBHWWHBHWmQgg&gggggNNN NNggggggNN +TYPE: ASCII + ,,,;;;;''''';;;'';, + ,,;'' ';;,;;; ', + ,,'' ;;'';'''';;;;;; + ,;' ;; ',, ; + ,;' ,;' ';, ; + ;' ,;; ',,,; + ,' ,;;,,,,,,,,,,,;;;; + ;' ;;';;;; ,;; + ;' ,;' ;; '',, ,;;; + ;; ,;' ; '';, ,; ;' +;; ,;;' ;; ;; ;; +;;, ,,;;' ; ;'; ;; +;';;,,,,;;;;;;;,,, ;; ,' ; ;; +; ;;''' ,;'; ''';,,, ; ,;' ;;;; +;;;;, ; '; ''';;;' ';;; +;'; ;, ;' '; ,;' ', ;;; +;;; ; ,; '; ,,' ',, ;; +;;; '; ;' ';,,'' ';,;; + '; ';,; ,,;''''''''';;;;;;,,;;; + ';,,;;,,;;;;;;;;;;'''''''''''''' +TYPE: UNICODE + ⣀⣤⣴⣾⣿⣿⣿⡛⠛⠛⠛⠛⣻⣿⠿⠛⠛⠶⣤⡀ + ⣀⣴⠾⠛⠉⠁ ⠙⣿⣶⣤⣶⣟⣉ ⠈⠻⣦ + ⣀⣴⠟⠋ ⢸⣿⠟⠻⣯⡙⠛⠛⠛⠶⠶⠶⢶⣽⣇ + ⣠⡾⠋⠁ ⣾⡿ ⠈⠛⢦⣄ ⣿ + ⣠⡾⠋ ⣰⣿⠃ ⠙⠷⣤⡀ ⣿ + ⢀⡾⠋ ⣰⣿⡏ ⠈⠻⣦⣄⢠⣿ + ⣰⠟⠁ ⣴⣿⣿⣁⣀⣠⣤⣤⣤⣤⣤⣤⣤⣴⠶⠿⣿⡏ + ⣼⠏ ⢀⣾⣿⠟⣿⠿⣯⣍⠁ ⣰⣿⡇ + ⢀⣼⠋ ⢀⣴⣿⠟⠁ ⢸⡇ ⠙⠻⢦⣄⡀ ⢠⡿⣿⡇ +⢀⣾⡏ ⢀⣴⣿⠟⠁ ⣿ ⠉⠻⢶⣄⡀⣰⡟ ⣿⠃ +⣾⣿⠁ ⣠⣶⡿⠋⠁ ⢹⡇ ⠈⣿⡏ ⢸⣿ +⣿⣿⡆ ⢀⣠⣴⣿⡿⠋ ⠈⣿ ⢀⡾⠋⣿ ⢸⣿ +⣿⠸⣿⣶⣤⣤⣤⣤⣶⣾⠿⠿⣿⣿⠶⣤⣤⣀⡀ ⢹⡇ ⣴⠟⠁ ⣿⡀⢸⣿ +⣿⢀⣿⣟⠛⠋⠉⠁ ⢰⡟⠹⣧ ⠈⠉⠛⠻⠶⢦⣤⣀⡀ ⠈⣿ ⣠⡾⠃ ⢸⡇⢸⡇ +⣿⣾⣿⢿⡄ ⣿⠁ ⠘⣧ ⠉⠙⠛⠷⣿⣿⡋ ⠸⣇⣸⡇ +⣿⠃⣿⠈⢿⡄ ⣸⠇ ⠘⣧ ⢀⣤⠾⠋⠈⠻⣦⡀ ⣿⣿⡇ +⣿⢸⡏ ⠈⣷⡀ ⢠⡿ ⠘⣧⡀ ⣠⡴⠟⠁ ⠈⠻⣦⣀ ⢿⣿⠁ +⢻⣾⡇ ⠘⣷ ⣼⠃ ⠘⣷⣠⣴⠟⠋ ⠙⢷⣄⢸⣿ + ⠻⣧⡀ ⠘⣧⣰⡏ ⢀⣠⣤⠶⠛⠉⠛⠛⠛⠛⠛⠛⠻⢶⣶⣶⣶⣶⣶⣤⣤⣽⣿⣿ + ⠈⠛⠷⢦⣤⣽⣿⣥⣤⣶⣶⡿⠿⠿⠶⠶⠶⠶⠾⠛⠛⠛⠛⠛⠛⠛⠋⠉⠉⠉⠉⠉⠉⠁