initial color support. will add ability to change colors from console soon. see #344

git-svn-id: file:///home/svn/framework3/trunk@7440 4d416f70-5f16-0410-b530-b9f4589650da
unstable
James Lee 2009-11-10 03:27:48 +00:00
parent 2620ad3a3c
commit a8d9da0d3d
12 changed files with 106 additions and 89 deletions

View File

@ -160,11 +160,11 @@ class Module
end
def print_status(msg='')
print_line(print_prefix + "[*] " + msg)
super(print_prefix + msg)
end
def print_error(msg='')
print_line(print_prefix + "[-] " + msg)
super(print_prefix + msg)
end
#

View File

@ -171,8 +171,8 @@ class Core
# Display one of the fabulous banners.
#
def cmd_banner(*args)
banner = Banner.to_s + "\n\n"
banner << " =[ metasploit v#{Msf::Framework::Version} [core:#{Msf::Framework::VersionCore} api:#{Msf::Framework::VersionAPI}]\n"
banner = "%cya" + Banner.to_s + "%c\n\n"
banner << " =[ %yelmetasploit v#{Msf::Framework::Version} [core:#{Msf::Framework::VersionCore} api:#{Msf::Framework::VersionAPI}]%c\n"
banner << "+ -- --=[ "
banner << "#{framework.stats.num_exploits} exploits - #{framework.stats.num_auxiliary} auxiliary\n"
banner << "+ -- --=[ "
@ -190,10 +190,9 @@ class Core
oldwarn << ""
end
end
banner << "\n"
# Display the banner
print(banner)
print_line(banner)
if(oldwarn)
oldwarn.map{|line| print_line(line) }
@ -1475,7 +1474,7 @@ class Core
mod.init_ui(driver.input, driver.output)
# Update the command prompt
driver.update_prompt("#{mod.type}(#{mod.shortname}) ")
driver.update_prompt("#{mod.type}(%red#{mod.shortname}%c) ")
end
#

View File

@ -21,8 +21,8 @@ class Driver < Msf::Ui::Driver
ConfigCore = "framework/core"
ConfigGroup = "framework/ui/console"
DefaultPrompt = "%umsf"
DefaultPromptChar = ">%c"
DefaultPrompt = "%umsf%c"
DefaultPromptChar = "%c>"
#
# The console driver processes various framework notified events.
@ -122,12 +122,6 @@ class Driver < Msf::Ui::Driver
# Load additional modules as necessary
self.framework.modules.add_module_path(opts['ModulePath'], false) if opts['ModulePath']
# Process things before we actually display the prompt and get rocking
on_startup
# Process the resource script
load_resource(opts['Resource'])
# Whether or not command passthru should be allowed
self.command_passthru = (opts['AllowCommandPassthru'] == false) ? false : true
@ -138,6 +132,12 @@ class Driver < Msf::Ui::Driver
if @defanged
self.command_passthru = false
end
# Process things before we actually display the prompt and get rocking
on_startup
# Process the resource script
load_resource(opts['Resource'])
end
#

View File

@ -72,10 +72,14 @@ class SVN
"today"
when 1.0 .. 2.0
"yesterday"
else
if (diff.to_i > 7)
"%red#{diff.to_i} days ago%c"
else
"#{diff.to_i} days ago"
end
end
end
end

View File

@ -36,12 +36,56 @@ module Color
# Return a string with ANSI codes substituted. Derived from code
# written by The FaerieMUD Consortium.
#
def self.ansi(*attrs)
def ansi(*attrs)
attr = attrs.collect {|a| AnsiAttributes[a] ? AnsiAttributes[a] : nil}.compact.join(';')
attr = "\e[%sm" % attr if (attr.empty? == false)
return attr
end
#
# Colorize if this shell supports it
#
def colorize(*color)
supports_color?() ? ansi(*color) : ''
end
def substitute_colors(str)
str.gsub!(/%cya/, colorize('cyan'))
str.gsub!(/%red/, colorize('red'))
str.gsub!(/%grn/, colorize('green'))
str.gsub!(/%blu/, colorize('blue'))
str.gsub!(/%yel/, colorize('yellow'))
str.gsub!(/%whi/, colorize('white'))
str.gsub!(/%mag/, colorize('magenta'))
str.gsub!(/%blk/, colorize('black'))
str.gsub!(/%dred/, colorize('dark', 'red'))
str.gsub!(/%dgrn/, colorize('dark', 'green'))
str.gsub!(/%dblu/, colorize('dark', 'blue'))
str.gsub!(/%dyel/, colorize('dark', 'yellow'))
str.gsub!(/%dcya/, colorize('dark', 'cyan'))
str.gsub!(/%dwhi/, colorize('dark', 'white'))
str.gsub!(/%dmag/, colorize('dark', 'magenta'))
str.gsub!(/%u/, colorize('underline'))
str.gsub!(/%b/, colorize('bold'))
str.gsub!(/%c/, colorize('clear'))
str
end
#
# Resets coloring so that it's back to normal.
#
def reset_color
return if not supports_color?
print(colorize('clear'))
end
#
# Colorize if this shell supports it
#
def do_colorize(*color)
supports_color?() ? ansi(*color) : ''
end
end
end end end

View File

@ -8,9 +8,9 @@ require 'rex/ui/text/color'
class Rex::Ui::Text::Color::UnitTest < Test::Unit::TestCase
def test_color
color = Rex::Ui::Text::Color.ansi('bold', 'red')
color = Rex::Ui::Text::Color.new.ansi('bold', 'red')
color += 'hey sup'
color += Rex::Ui::Text::Color.ansi('clear')
color += Rex::Ui::Text::Color.new.ansi('clear')
assert_equal("\e[1;31mhey sup\e[0m", color)
end

View File

@ -183,7 +183,7 @@ module DispatcherShell
found = false
error = false
reset_color if (supports_color?)
output.reset_color
if (method)
entries = dispatcher_stack.length

View File

@ -15,23 +15,31 @@ class Output < Rex::Ui::Output
require 'rex/ui/text/output/stdio'
require 'rex/ui/text/output/socket'
require 'rex/ui/text/output/buffer'
require 'rex/ui/text/color'
include Rex::Ui::Text::Color
def print_error(msg = '')
print_line("[-] #{msg}")
print_line("%red[-]%c #{msg}")
end
def print_good(msg = '')
print_line("[+] #{msg}")
print_line("%grn[+]%c #{msg}")
end
def print_status(msg = '')
print_line("[*] #{msg}")
print_line("%blu[*]%c #{msg}")
end
def print_line(msg = '')
print(msg + "\n")
end
def print(msg = '')
substitute_colors(msg)
print_raw(msg)
end
def reset
end

View File

@ -18,10 +18,14 @@ class Output::Buffer < Rex::Ui::Text::Output
self.buf = ''
end
def supports_color?
false
end
#
# Appends the supplied message to the output buffer.
#
def print(msg = '')
def print_raw(msg = '')
self.buf += msg || ''
if self.on_print_proc

View File

@ -15,10 +15,14 @@ class Output::Socket < Rex::Ui::Text::Output
@sock = sock
end
def supports_color?
false
end
#
# Prints the supplied message to the socket.
#
def print(msg = '')
def print_raw(msg = '')
@sock.write(msg)
@sock.flush

View File

@ -11,10 +11,18 @@ module Text
###
class Output::Stdio < Rex::Ui::Text::Output
def supports_color?
# Color is disabled until we resolve some bugs
#return false
term = Rex::Compat.getenv('TERM')
(term and term.match(/(?:vt10[03]|xterm(?:-color)?|linux|screen)/i) != nil)
end
#
# Prints the supplied message to standard output.
#
def print(msg = '')
def print_raw(msg = '')
$stdout.print(msg)
$stdout.flush

View File

@ -166,67 +166,13 @@ module Shell
def update_prompt(prompt = '', new_prompt_char = nil)
new_prompt = self.init_prompt + ' ' + prompt + prompt_char + ' '
# Substitute colors
new_prompt.gsub!(/%u/, colorize('underline'))
new_prompt.gsub!(/%b/, colorize('bold'))
new_prompt.gsub!(/%cya/, colorize('cyan'))
new_prompt.gsub!(/%c/, colorize('clear'))
new_prompt.gsub!(/%red/, colorize('red'))
new_prompt.gsub!(/%grn/, colorize('green'))
new_prompt.gsub!(/%blu/, colorize('blue'))
new_prompt.gsub!(/%yel/, colorize('yellow'))
new_prompt.gsub!(/%whi/, colorize('white'))
new_prompt.gsub!(/%mag/, colorize('magenta'))
new_prompt.gsub!(/%blk/, colorize('black'))
new_prompt.gsub!(/%dred/, colorize('dark', 'red'))
new_prompt.gsub!(/%dgrn/, colorize('dark', 'green'))
new_prompt.gsub!(/%dblu/, colorize('dark', 'blue'))
new_prompt.gsub!(/%dyel/, colorize('dark', 'yellow'))
new_prompt.gsub!(/%dcya/, colorize('dark', 'cyan'))
new_prompt.gsub!(/%dwhi/, colorize('dark', 'white'))
new_prompt.gsub!(/%dmag/, colorize('dark', 'magenta'))
# This really should be handled when it's printed, not here
self.output.substitute_colors(new_prompt)
self.input.prompt = new_prompt if (self.input)
self.prompt_char = new_prompt_char if (new_prompt_char)
end
#
# Color checks
#
#
# Checks to see whether or not colors are supported on this shell
# console.
#
def supports_color?
# Color is disabled until we resolve some bugs
return false
term = Rex::Compat.getenv('TERM')
(term and term.match(/(?:vt10[03]|xterm(?:-color)?|linux|screen)/i) != nil)
end
#
# Resets coloring so that it's back to normal.
#
def reset_color
print(colorize('clear'))
end
#
# Returns colorized text if it's supported, otherwise an empty string.
#
def colorize(*color)
return do_colorize(*color)
end
#
# Colorize if this shell supports it
#
def do_colorize(*color)
supports_color?() ? Rex::Ui::Text::Color.ansi(*color) : ''
end
#
# Output shortcuts
#