Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/ui/text/output/stdio.rb
Views: 11704
# -*- coding: binary -*-12begin3require 'windows_console_color_support'4rescue ::LoadError5end67module Rex8module Ui9module Text1011###12#13# This class implements output against standard out.14#15###16class Output::Stdio < Rex::Ui::Text::Output17#18# Attributes19#2021# @!attribute io22# The raw `IO` backing this Text output. Defaults to `$stdout`23#24# @return [#flush, #puts, #write]25attr_writer :io2627#28# Constructor29#3031# @param options [Hash{Symbol => IO}]32# @option options [IO]33def initialize(options={})34options.assert_valid_keys(:io)3536super()3738self.io = options[:io]39end4041#42# Methods43#4445def flush46io.flush47end4849# IO to write to.50#51# @return [IO] Default to `$stdout`52def io53@io ||= $stdout54end5556# Use ANSI Control chars to reset prompt position for async output57# SEE https://github.com/rapid7/metasploit-framework/pull/757058def print_line(msg = '')59# TODO: there are unhandled quirks in async output buffering that60# we have not solved yet, for instance when loading meterpreter61# extensions, supporting Windows, printing output from commands, etc.62# Remove this guard when issues are resolved.63=begin64if (/mingw/ =~ RUBY_PLATFORM)65print(msg + "\n")66return67end68print("\033[s") # Save cursor position69print("\r\033[K" + msg + "\n")70if input and input.prompt71print("\r\033[K")72print(input.prompt.tr("\001\002", ''))73print(input.line_buffer.tr("\001\002", ''))74print("\033[u\033[B") # Restore cursor, move down one line75end76=end7778print(msg + "\n")79end8081#82# Prints the supplied message to standard output.83#84def print_raw(msg = '')85if (Rex::Compat.is_windows and supports_color?)86WindowsConsoleColorSupport.new(io).write(msg)87else88io.print(msg)89end9091io.flush9293msg94end95alias_method :write, :print_raw9697def supports_color?98case config[:color]99when true100return true101when false102return false103else # auto104if (Rex::Compat.is_windows)105return true106end107term = Rex::Compat.getenv('TERM')108return (term and term.match(/(?:vt10[03]|xterm(?:-color)?|linux|screen|rxvt)/i) != nil)109end110end111end112113end114end115end116117118119