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
Readline.get_screen_size
- By environment like IRB or Pry
- By tput command line
[`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
require 'readline'
# Prevent Ctrl+C for exiting
# List of commands
CMDS = [ 'help', 'rubyfu', 'ls', 'exit' ].sort
proc do |str|
case
when Readline.line_buffer =~ /help.*/i
puts "Available commands:\n" + "#{CMDS.join("\t")}"
when Readline.line_buffer =~ /rubyfu.*/i
puts "Rubyfu, where Ruby goes evil!"
when Readline.line_buffer =~ /ls.*/i
puts `ls`
puts 'Exiting..'
exit 0
CMDS.grep( /^#{Regexp.escape(str)}/i ) unless str.nil?
end
end
Readline.completion_proc = completion # Set completion process
Readline.completion_append_character = ' ' # Make sure to add a space after completion
while line = Readline.readline('-> ', true) # Start console with character -> and make add_hist = true
puts completion.call
break if line =~ /^quit.*/i or line =~ /^exit.*/i
Things can go much farther, like msfconsole, maybe?