Module 0x1 | Basic Ruby Kung Fu

    Here are some different ways to get terminal size from ruby:

    • By IO/console standard library
    • By readline standard library
    1. Readline.get_screen_size
    • By environment like IRB or Pry
    • By tput command line
    1. [`tput cols`.to_i , `tput lines`.to_i]

    Console with tab completion

    We can’t stop being jealous of Metasploit console (msfconsole), where we take a rest from command line switches. Fortunately, here is the main idea of console tab completion in ruby:

    • Readline

    console-basic1.rb

    Now run it and try out the tab completion!

    console-basic2.rb

    1. require 'readline'
    2. # Prevent Ctrl+C for exiting
    3. # List of commands
    4. CMDS = [ 'help', 'rubyfu', 'ls', 'exit' ].sort
    5. proc do |str|
    6. case
    7. when Readline.line_buffer =~ /help.*/i
    8. puts "Available commands:\n" + "#{CMDS.join("\t")}"
    9. when Readline.line_buffer =~ /rubyfu.*/i
    10. puts "Rubyfu, where Ruby goes evil!"
    11. when Readline.line_buffer =~ /ls.*/i
    12. puts `ls`
    13. puts 'Exiting..'
    14. exit 0
    15. CMDS.grep( /^#{Regexp.escape(str)}/i ) unless str.nil?
    16. end
    17. end
    18. Readline.completion_proc = completion # Set completion process
    19. Readline.completion_append_character = ' ' # Make sure to add a space after completion
    20. while line = Readline.readline('-> ', true) # Start console with character -> and make add_hist = true
    21. puts completion.call
    22. break if line =~ /^quit.*/i or line =~ /^exit.*/i

    Things can go much farther, like msfconsole, maybe?