CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/ui/text/output/stdio.rb
Views: 11704
1
# -*- coding: binary -*-
2
3
begin
4
require 'windows_console_color_support'
5
rescue ::LoadError
6
end
7
8
module Rex
9
module Ui
10
module Text
11
12
###
13
#
14
# This class implements output against standard out.
15
#
16
###
17
class Output::Stdio < Rex::Ui::Text::Output
18
#
19
# Attributes
20
#
21
22
# @!attribute io
23
# The raw `IO` backing this Text output. Defaults to `$stdout`
24
#
25
# @return [#flush, #puts, #write]
26
attr_writer :io
27
28
#
29
# Constructor
30
#
31
32
# @param options [Hash{Symbol => IO}]
33
# @option options [IO]
34
def initialize(options={})
35
options.assert_valid_keys(:io)
36
37
super()
38
39
self.io = options[:io]
40
end
41
42
#
43
# Methods
44
#
45
46
def flush
47
io.flush
48
end
49
50
# IO to write to.
51
#
52
# @return [IO] Default to `$stdout`
53
def io
54
@io ||= $stdout
55
end
56
57
# Use ANSI Control chars to reset prompt position for async output
58
# SEE https://github.com/rapid7/metasploit-framework/pull/7570
59
def print_line(msg = '')
60
# TODO: there are unhandled quirks in async output buffering that
61
# we have not solved yet, for instance when loading meterpreter
62
# extensions, supporting Windows, printing output from commands, etc.
63
# Remove this guard when issues are resolved.
64
=begin
65
if (/mingw/ =~ RUBY_PLATFORM)
66
print(msg + "\n")
67
return
68
end
69
print("\033[s") # Save cursor position
70
print("\r\033[K" + msg + "\n")
71
if input and input.prompt
72
print("\r\033[K")
73
print(input.prompt.tr("\001\002", ''))
74
print(input.line_buffer.tr("\001\002", ''))
75
print("\033[u\033[B") # Restore cursor, move down one line
76
end
77
=end
78
79
print(msg + "\n")
80
end
81
82
#
83
# Prints the supplied message to standard output.
84
#
85
def print_raw(msg = '')
86
if (Rex::Compat.is_windows and supports_color?)
87
WindowsConsoleColorSupport.new(io).write(msg)
88
else
89
io.print(msg)
90
end
91
92
io.flush
93
94
msg
95
end
96
alias_method :write, :print_raw
97
98
def supports_color?
99
case config[:color]
100
when true
101
return true
102
when false
103
return false
104
else # auto
105
if (Rex::Compat.is_windows)
106
return true
107
end
108
term = Rex::Compat.getenv('TERM')
109
return (term and term.match(/(?:vt10[03]|xterm(?:-color)?|linux|screen|rxvt)/i) != nil)
110
end
111
end
112
end
113
114
end
115
end
116
end
117
118
119