color-console 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/README.md +12 -2
- data/lib/color-console.rb +1 -1
- data/lib/color_console.rb +1 -1
- data/lib/{console → color_console}/console.rb +0 -0
- data/lib/color_console/java_util_logger.rb +133 -0
- data/lib/color_console/log4r_logger.rb +114 -0
- data/lib/{console → color_console}/platform/ansi.rb +0 -0
- data/lib/{console → color_console}/platform/windows.rb +15 -70
- data/lib/color_console/platform/windows_ffi.rb +79 -0
- data/lib/color_console/platform/windows_fiddle.rb +82 -0
- data/lib/{console → color_console}/progress.rb +0 -0
- data/lib/{console → color_console}/table.rb +0 -0
- metadata +19 -14
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
ZDkzNGM2NWI3NmVmZjU4YTYxZjI4MTA3MGJlOGYxNGFjY2MyNjgwYQ==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: eac6c2a764d6f516366d6f5e8470d2ed3b63170d
|
4
|
+
data.tar.gz: aa2c094cf2ad8961fda8bdc3a74c484e0752bab5
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
Y2YxODcxYzNhZWZkMDI5NDczOWZlMGZiYmM2ZjFiNWQ0OTZiOTU4ZWUyNTVl
|
11
|
-
YTlkNGQ2YzMzZmZhNTgwODMxN2UxMmM3ZDMxYTQ0YzE1MWJmMDM=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
M2IxNTFlZDQ4YjRlNDUzZTA1NDU5NWJhODBjZjU4YTJjOWM3MjdiNWY4YTlh
|
14
|
-
ZmQ3NDQyZTdlZTU5NzVkYzJmMTljODFlYTViY2YyYjQzYjgwZTc1NTVjZDRj
|
15
|
-
Y2ZhODU3MGUwY2JmOGVjMzJkYjY5ZWNmYzA5NjVjMjRiMGNjOTg=
|
6
|
+
metadata.gz: 118c4ed399645b94a34d071314f3c0716048ca7044522421cb79b73c337992eab90f41e1326c4983c54d10f90c844fffef86982aa8508022b6ae77cc06b65b60
|
7
|
+
data.tar.gz: 3d60e42deccc08d8e9a9b318e8832b93413410dd9dc7390992fc92bc6ac9cc45a838d258acef608aed02af18fd37457bd7c8730b64cc0201f5f090de35dc3193
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# ColorConsole
|
2
2
|
|
3
|
-
ColorConsole is a small cross-platform library for outputting text to the console.
|
3
|
+
ColorConsole is a small cross-platform library for outputting color text to the console, as well as providing utilities for drawing progress bars and outputting tabular data.
|
4
4
|
|
5
|
-
On Windows, FFI is used to dynamically link to the Windows Console API functions, while on other platforms, ANSI escape sequences are used. As such, there are no dependencies and no libraries to install other than this gem.
|
5
|
+
On Windows, Fiddler (under MRI) or FFI (other engines) is used to dynamically link to the Windows Console API functions, while on other platforms, ANSI escape sequences are used. As such, there are no dependencies and no libraries to install other than this gem.
|
6
6
|
|
7
7
|
|
8
8
|
## Usage
|
@@ -75,4 +75,14 @@ In addition to `Console.puts` and `Console.write` for outputting text in color,
|
|
75
75
|
| 24 | Lorem ipsum | Some more text | Lorem ipsum dolor sit amet |
|
76
76
|
+----------+--------------------------+-----------------------------+-----------------------------+
|
77
77
|
```
|
78
|
+
* __Color Logging__: If you are using java.util.logging under JRuby, or the Ruby logging library Log4r, you can obtain colour log messages by replacing the console handler with one provided by this gem:
|
79
|
+
```ruby
|
80
|
+
# If using Log4r for logging...
|
81
|
+
require 'color_console/log4r_logger'
|
82
|
+
Console.replace_console_logger(level: :info)
|
83
|
+
|
84
|
+
# Or under JRuby with java.util.logging...
|
85
|
+
require 'color_console/java_util_logger'
|
86
|
+
Console.replace_console_logger(level: :fine, format: '%4$-6s %5$s%n')
|
87
|
+
```
|
78
88
|
|
data/lib/color-console.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
require '
|
1
|
+
require 'color_console/console'
|
2
2
|
|
data/lib/color_console.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
require '
|
1
|
+
require 'color_console/console'
|
2
2
|
|
File without changes
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'java'
|
2
|
+
require_relative '../color_console'
|
3
|
+
|
4
|
+
|
5
|
+
# A module for using our color console for log output with the java.util.logging
|
6
|
+
# log framework.
|
7
|
+
module Console
|
8
|
+
|
9
|
+
module JavaUtilLogger
|
10
|
+
|
11
|
+
include_package 'java.util.logging'
|
12
|
+
|
13
|
+
|
14
|
+
# Extends the java.util.ConsoleHandler, adding colorised logging output to
|
15
|
+
# the console.
|
16
|
+
class ColorConsoleHandler < ConsoleHandler
|
17
|
+
|
18
|
+
def initialize(format = RubyFormatter::DEFAULT_FORMAT)
|
19
|
+
super()
|
20
|
+
self.formatter = RubyFormatter.new(format)
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
# Publishes a log record by outputting it to the console, using an
|
25
|
+
# appropriate color for the severity of the log message.
|
26
|
+
def publish(log_record)
|
27
|
+
msg = formatter.format(log_record)
|
28
|
+
case log_record.level
|
29
|
+
when JavaUtilLogger::Level::INFO
|
30
|
+
Console.write msg, :white
|
31
|
+
when JavaUtilLogger::Level::CONFIG
|
32
|
+
Console.write msg, :cyan
|
33
|
+
when JavaUtilLogger::Level::FINE
|
34
|
+
Console.write msg, :light_gray
|
35
|
+
when JavaUtilLogger::Level::SEVERE
|
36
|
+
Console.write msg, :red
|
37
|
+
when JavaUtilLogger::Level::WARNING
|
38
|
+
Console.write msg, :yellow
|
39
|
+
else
|
40
|
+
Console.write msg, :dark_gray
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
# Extends java.util.logging.Formatter, adding the ability to customise the
|
48
|
+
# log format at runtime, and defaulting to a simpler single-line format more
|
49
|
+
# suitable for output to the console.
|
50
|
+
class RubyFormatter < Formatter
|
51
|
+
|
52
|
+
DEFAULT_FORMAT = '%4$-6s %5$s%n'
|
53
|
+
|
54
|
+
# A format string to use when formatting a log record.
|
55
|
+
# @see Java function String.format for the format string syntax. The
|
56
|
+
# values passed by this formatter to String.format are:
|
57
|
+
# - millis The time the log event occurred
|
58
|
+
# - source The name of the logger that logged the record
|
59
|
+
# - logger_name The name of the logger that logged the record
|
60
|
+
# - level The level of the message
|
61
|
+
# - message The log message
|
62
|
+
# - thrown Any exception that forms part of the log record.
|
63
|
+
attr_accessor :format_string
|
64
|
+
|
65
|
+
|
66
|
+
# Constructs a new formatter for formatting log records according to
|
67
|
+
# a format string.
|
68
|
+
#
|
69
|
+
# @param format The format string to use when building a String for
|
70
|
+
# logging.
|
71
|
+
def initialize(format = DEFAULT_FORMAT)
|
72
|
+
super()
|
73
|
+
@format_string = format
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
# Format a log record and return a string for publishing by a log handler.
|
78
|
+
def format(log_record)
|
79
|
+
lvl = case log_record.level
|
80
|
+
when JavaUtilLogger::Level::WARNING then 'WARN'
|
81
|
+
when JavaUtilLogger::Level::SEVERE then 'ERROR'
|
82
|
+
when JavaUtilLogger::Level::FINEST then 'DEBUG'
|
83
|
+
else log_record.level
|
84
|
+
end
|
85
|
+
java.lang.String.format(@format_string,
|
86
|
+
log_record.millis,
|
87
|
+
log_record.logger_name,
|
88
|
+
log_record.logger_name,
|
89
|
+
lvl,
|
90
|
+
log_record.message,
|
91
|
+
log_record.thrown)
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
# Removes any existing console handler, and adds a ColorConsoleHandler.
|
100
|
+
#
|
101
|
+
# @param options [Hash] An options hash.
|
102
|
+
# @option options [String] logger The name of the logger to replace the
|
103
|
+
# handler on. Defaults to an empty string, which returns the root logger.
|
104
|
+
# @option options [Level, Symbol] level The level to set the logger to.
|
105
|
+
# @option options [String] format A format string for the layout of the log
|
106
|
+
# record.
|
107
|
+
def replace_console_logger(options = {})
|
108
|
+
logger = options.fetch(:logger, '')
|
109
|
+
level = options[:level]
|
110
|
+
format = options.fetch(:format, JavaUtilLogger::RubyFormatter::DEFAULT_FORMAT)
|
111
|
+
|
112
|
+
# Remove any existing console handler
|
113
|
+
l = Java::JavaUtilLogging::Logger.getLogger(logger)
|
114
|
+
l.getHandlers.each do |h|
|
115
|
+
l.removeHandler(h) if h.is_a?(Java::JavaUtilLogging::ConsoleHandler)
|
116
|
+
end
|
117
|
+
|
118
|
+
# Add a ColorConsoleHandler
|
119
|
+
h = JavaUtilLogger::ColorConsoleHandler.new(format)
|
120
|
+
l.addHandler(h)
|
121
|
+
|
122
|
+
# Set the log level
|
123
|
+
case level
|
124
|
+
when Symbol, String
|
125
|
+
l.level = Java::JavaUtilLogging::Level.const_get(level.upcase.intern)
|
126
|
+
when Java::JavaUtilLogging::Level
|
127
|
+
l.level = level
|
128
|
+
end
|
129
|
+
end
|
130
|
+
module_function :replace_console_logger
|
131
|
+
|
132
|
+
end
|
133
|
+
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'log4r'
|
2
|
+
require_relative '../color_console'
|
3
|
+
|
4
|
+
|
5
|
+
# A module for using our color console for log output with the java.util.logging
|
6
|
+
# log framework.
|
7
|
+
module Console
|
8
|
+
|
9
|
+
module Log4rLogger
|
10
|
+
|
11
|
+
|
12
|
+
# Extends the Log4r StdoutOutputter, adding colorised logging output to
|
13
|
+
# the console.
|
14
|
+
class ColorConsoleOutputter < Log4r::StdoutOutputter
|
15
|
+
|
16
|
+
def initialize(name, options = {})
|
17
|
+
super(name, options)
|
18
|
+
@console_width = Console.width || -1
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
def format(event)
|
23
|
+
@fg = case event.level
|
24
|
+
when Log4r::INFO then :white
|
25
|
+
when Log4r::WARN then :yellow
|
26
|
+
when Log4r::ERROR then :red
|
27
|
+
when Log4r::DEBUG then :dark_gray
|
28
|
+
else :light_gray
|
29
|
+
end
|
30
|
+
|
31
|
+
if event.data
|
32
|
+
begin
|
33
|
+
thread = Log4r::NDC.peek.to_s.upcase[0, 2]
|
34
|
+
level = Log4r::LNAMES[event.level]
|
35
|
+
case
|
36
|
+
when event.data.is_a?(Exception) || event.data.java_kind_of?(java.lang.Throwable)
|
37
|
+
msg = Exception.format(event.data)
|
38
|
+
when event.data.is_a?(Array)
|
39
|
+
msg = event.data
|
40
|
+
case msg.first
|
41
|
+
when String then msg[0] = "%-8s %-2s %s" % [level, thread, msg[0]]
|
42
|
+
when Array then msg[0][0] = "%-8s %-2s %s" % [level, thread, msg[0][0]]
|
43
|
+
end
|
44
|
+
else
|
45
|
+
msg = Console.wrap_text(event.data, @console_width - 16)
|
46
|
+
msg = msg.each_with_index.map do |line, i|
|
47
|
+
"%-8s %-2s %s" % [[level][i], [thread][i], line]
|
48
|
+
end.join("\n")
|
49
|
+
end
|
50
|
+
msg
|
51
|
+
rescue Object => ex
|
52
|
+
"(Unable to format event.data)" # due to #{ex})\n"
|
53
|
+
end
|
54
|
+
else
|
55
|
+
""
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
def write(data)
|
61
|
+
if data.is_a?(Array)
|
62
|
+
if data[0].is_a?(String) && (data.length == 0 || data[1].is_a?(Symbol))
|
63
|
+
Console.puts *data
|
64
|
+
else
|
65
|
+
data.each do |chunk|
|
66
|
+
Console.write *chunk
|
67
|
+
end
|
68
|
+
Console.puts
|
69
|
+
end
|
70
|
+
else
|
71
|
+
Console.puts(data, @fg)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
# Removes any existing console handler, and adds a ColorConsoleHandler.
|
81
|
+
#
|
82
|
+
# @param options [Hash] An options hash.
|
83
|
+
# @option options [String] logger The name of the logger to replace the
|
84
|
+
# handler on. Defaults to nil, which returns the root logger.
|
85
|
+
# @option options [Level, Symbol] level The level to set the logger to.
|
86
|
+
# @option options [String] format A format string for the layout of the log
|
87
|
+
# record.
|
88
|
+
def replace_console_logger(options = {})
|
89
|
+
logger = options[:logger]
|
90
|
+
name = options.fetch(:outputter, 'color-console')
|
91
|
+
level = case options.delete(:level)
|
92
|
+
when String, Symbol then Log4r::LNAMES.index(options[:level].to_s.upcase)
|
93
|
+
end
|
94
|
+
STDOUT.puts level
|
95
|
+
|
96
|
+
# Remove any existing console handler
|
97
|
+
log = logger ? Log4r::Logger[logger] : Log4r::Logger.root
|
98
|
+
log = Log4r::Logger.new(logger) unless log
|
99
|
+
|
100
|
+
log.outputters.each do |o|
|
101
|
+
log.remove(o.name) if h.is_a?(Log4r::StdoutOutputter)
|
102
|
+
end
|
103
|
+
|
104
|
+
# Add a ColorConsoleHandler
|
105
|
+
out = Log4rLogger::ColorConsoleOutputter.new(name, options)
|
106
|
+
log.add out
|
107
|
+
|
108
|
+
# Set the log level
|
109
|
+
log.level = level if level
|
110
|
+
end
|
111
|
+
module_function :replace_console_logger
|
112
|
+
|
113
|
+
end
|
114
|
+
|
File without changes
|
@@ -1,59 +1,16 @@
|
|
1
|
-
|
1
|
+
if RUBY_ENGINE == 'ruby'
|
2
|
+
require_relative 'windows_fiddle'
|
3
|
+
else
|
4
|
+
require_relative 'windows_ffi'
|
5
|
+
end
|
6
|
+
|
2
7
|
|
3
8
|
|
4
9
|
module Console
|
5
10
|
|
6
|
-
# Implements
|
11
|
+
# Implements functions for interacting with the console under Windows.
|
7
12
|
module Windows
|
8
13
|
|
9
|
-
extend FFI::Library
|
10
|
-
|
11
|
-
ffi_lib 'kernel32.dll'
|
12
|
-
ffi_convention :stdcall
|
13
|
-
|
14
|
-
|
15
|
-
# FFI structure used to get/set information about the current console
|
16
|
-
# window buffer
|
17
|
-
class BufferInfo < FFI::Struct
|
18
|
-
layout :width, :short,
|
19
|
-
:height, :short,
|
20
|
-
:cursor_x, :short,
|
21
|
-
:cursor_y, :short,
|
22
|
-
:text_attributes, :ushort,
|
23
|
-
:window_left, :short,
|
24
|
-
:window_top, :short,
|
25
|
-
:window_right, :short,
|
26
|
-
:window_bottom, :short,
|
27
|
-
:max_width, :short,
|
28
|
-
:max_height, :short
|
29
|
-
end
|
30
|
-
|
31
|
-
|
32
|
-
# FFI structure used to get/set buffer co-ordinates
|
33
|
-
class Coord < FFI::Struct
|
34
|
-
layout :x, :short,
|
35
|
-
:y, :short
|
36
|
-
|
37
|
-
def initialize(x, y)
|
38
|
-
self[:x] = x
|
39
|
-
self[:y] = y
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
# Define Windows console functions we need
|
44
|
-
attach_function :get_std_handle, :GetStdHandle, [:uint], :pointer
|
45
|
-
attach_function :get_console_screen_buffer_info, :GetConsoleScreenBufferInfo, [:pointer, :pointer], :bool
|
46
|
-
attach_function :set_console_cursor_position, :SetConsoleCursorPosition, [:pointer, Coord.by_value], :bool
|
47
|
-
attach_function :set_console_text_attribute, :SetConsoleTextAttribute, [:pointer, :ushort], :bool
|
48
|
-
attach_function :set_console_title, :SetConsoleTitleA, [:pointer], :bool
|
49
|
-
|
50
|
-
|
51
|
-
# Constants representing STDIN, STDOUT, and STDERR
|
52
|
-
STD_OUTPUT_HANDLE = 0xFFFFFFF5
|
53
|
-
STD_INPUT_HANDLE = 0xFFFFFFF6
|
54
|
-
STD_ERROR_HANDLE = 0xFFFFFFF7
|
55
|
-
|
56
|
-
|
57
14
|
# Retrieve a handle to STDOUT
|
58
15
|
def stdout
|
59
16
|
@stdout ||= self.get_std_handle(STD_OUTPUT_HANDLE)
|
@@ -62,14 +19,6 @@ module Console
|
|
62
19
|
private :stdout
|
63
20
|
|
64
21
|
|
65
|
-
# Retrieve a BufferInfo object
|
66
|
-
def buffer_info
|
67
|
-
@buffer_info ||= BufferInfo.new
|
68
|
-
end
|
69
|
-
module_function :buffer_info
|
70
|
-
private :buffer_info
|
71
|
-
|
72
|
-
|
73
22
|
# Populate a BufferInfo structure with details about the current
|
74
23
|
# buffer state.
|
75
24
|
#
|
@@ -93,17 +42,6 @@ module Console
|
|
93
42
|
module_function :set_color
|
94
43
|
|
95
44
|
|
96
|
-
# Sets the cursor position to the specified +x+ and +y+ locations in the
|
97
|
-
# console output buffer. If +y+ is nil, the cursor is positioned at +x+ on
|
98
|
-
# the current line.
|
99
|
-
def set_cursor_position(x, y)
|
100
|
-
if stdout && x && y
|
101
|
-
coord = Coord.new(x, y)
|
102
|
-
self.set_console_cursor_position(stdout, coord)
|
103
|
-
end
|
104
|
-
end
|
105
|
-
module_function :set_cursor_position
|
106
|
-
|
107
45
|
end
|
108
46
|
|
109
47
|
|
@@ -229,7 +167,14 @@ module Console
|
|
229
167
|
_clear_line (@status.length / self.width) + 1
|
230
168
|
@status_displayed = false
|
231
169
|
end
|
232
|
-
|
170
|
+
buffer = Windows.get_buffer_info
|
171
|
+
if buffer && text && text.length > 0 &&
|
172
|
+
text.length == (buffer[:window_right] + 1 - buffer[:cursor_x])
|
173
|
+
# Text length is same as width of window
|
174
|
+
_write("#{text}", fg, bg)
|
175
|
+
else
|
176
|
+
_write("#{text}\r\n", fg, bg)
|
177
|
+
end
|
233
178
|
if @status
|
234
179
|
_write(@status, @status_fg, @status_bg)
|
235
180
|
@status_displayed = true
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'ffi'
|
2
|
+
|
3
|
+
|
4
|
+
module Console
|
5
|
+
|
6
|
+
# Implements Windows-specific platform functionality
|
7
|
+
module Windows
|
8
|
+
|
9
|
+
extend FFI::Library
|
10
|
+
|
11
|
+
# Constants representing STDIN, STDOUT, and STDERR
|
12
|
+
STD_OUTPUT_HANDLE = 0xFFFFFFF5
|
13
|
+
STD_INPUT_HANDLE = 0xFFFFFFF6
|
14
|
+
STD_ERROR_HANDLE = 0xFFFFFFF7
|
15
|
+
|
16
|
+
|
17
|
+
ffi_lib 'kernel32.dll'
|
18
|
+
ffi_convention :stdcall
|
19
|
+
|
20
|
+
|
21
|
+
# FFI structure used to get/set information about the current console
|
22
|
+
# window buffer
|
23
|
+
class BufferInfo < FFI::Struct
|
24
|
+
layout :width, :short,
|
25
|
+
:height, :short,
|
26
|
+
:cursor_x, :short,
|
27
|
+
:cursor_y, :short,
|
28
|
+
:text_attributes, :ushort,
|
29
|
+
:window_left, :short,
|
30
|
+
:window_top, :short,
|
31
|
+
:window_right, :short,
|
32
|
+
:window_bottom, :short,
|
33
|
+
:max_width, :short,
|
34
|
+
:max_height, :short
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
# FFI structure used to get/set buffer co-ordinates
|
39
|
+
class Coord < FFI::Struct
|
40
|
+
layout :x, :short,
|
41
|
+
:y, :short
|
42
|
+
|
43
|
+
def initialize(x, y)
|
44
|
+
self[:x] = x
|
45
|
+
self[:y] = y
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Define Windows console functions we need
|
50
|
+
attach_function :get_std_handle, :GetStdHandle, [:uint], :pointer
|
51
|
+
attach_function :get_console_screen_buffer_info, :GetConsoleScreenBufferInfo, [:pointer, :pointer], :bool
|
52
|
+
attach_function :set_console_cursor_position, :SetConsoleCursorPosition, [:pointer, Coord.by_value], :bool
|
53
|
+
attach_function :set_console_text_attribute, :SetConsoleTextAttribute, [:pointer, :ushort], :bool
|
54
|
+
attach_function :set_console_title, :SetConsoleTitleA, [:pointer], :bool
|
55
|
+
|
56
|
+
|
57
|
+
# Retrieve a BufferInfo object
|
58
|
+
def buffer_info
|
59
|
+
@buffer_info ||= BufferInfo.new
|
60
|
+
end
|
61
|
+
module_function :buffer_info
|
62
|
+
private :buffer_info
|
63
|
+
|
64
|
+
|
65
|
+
# Sets the cursor position to the specified +x+ and +y+ locations in the
|
66
|
+
# console output buffer. If +y+ is nil, the cursor is positioned at +x+ on
|
67
|
+
# the current line.
|
68
|
+
def set_cursor_position(x, y)
|
69
|
+
if stdout && x && y
|
70
|
+
coord = Coord.new(x, y)
|
71
|
+
self.set_console_cursor_position(stdout, coord)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
module_function :set_cursor_position
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'fiddle'
|
2
|
+
require 'fiddle/import'
|
3
|
+
|
4
|
+
|
5
|
+
module Console
|
6
|
+
|
7
|
+
# Implements Windows-specific platform functionality when running
|
8
|
+
# under MRI. Uses Fiddle, since MRI does not provide FFI functionality
|
9
|
+
# as standard, and furthermore requires a Ruby DevKit installation to
|
10
|
+
# install the ffi gem. This is a royal pain, so we instead use Fiddle
|
11
|
+
# to invoke the Windows console API.
|
12
|
+
module Windows
|
13
|
+
|
14
|
+
extend Fiddle::Importer
|
15
|
+
|
16
|
+
dlload 'kernel32.dll'
|
17
|
+
|
18
|
+
|
19
|
+
# Constant representing STDOUT (0xFFFFFFF5)
|
20
|
+
# We can't use this however, as it overflows to a Bignum!
|
21
|
+
STD_OUTPUT_HANDLE = -11
|
22
|
+
|
23
|
+
|
24
|
+
# Wrap the need to call #call on a Fiddle::Function
|
25
|
+
def self.attach_function(name, sig)
|
26
|
+
func = extern(sig, :stdcall)
|
27
|
+
define_singleton_method(name){ |*args| func.call(*args) }
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
# FFI structure used to get/set information about the current console
|
32
|
+
# window buffer
|
33
|
+
BufferInfo = struct([
|
34
|
+
'short width',
|
35
|
+
'short height',
|
36
|
+
'short cursor_x',
|
37
|
+
'short cursor_y',
|
38
|
+
'short text_attributes',
|
39
|
+
'short window_left',
|
40
|
+
'short window_top',
|
41
|
+
'short window_right',
|
42
|
+
'short window_bottom',
|
43
|
+
'short max_width',
|
44
|
+
'short max_height'
|
45
|
+
])
|
46
|
+
|
47
|
+
class BufferInfo
|
48
|
+
def [](name)
|
49
|
+
self.send(name)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
attach_function :get_std_handle, 'void* GetStdHandle(int)'
|
54
|
+
attach_function :get_console_screen_buffer_info, 'int GetConsoleScreenBufferInfo(*void, *void)'
|
55
|
+
attach_function :set_console_cursor_position, 'int SetConsoleCursorPosition(*void, int)'
|
56
|
+
attach_function :set_console_text_attribute, 'int SetConsoleTextAttribute(*void, short)'
|
57
|
+
attach_function :set_console_title, 'int SetConsoleTitleA(*void)'
|
58
|
+
|
59
|
+
|
60
|
+
# Retrieve a BufferInfo object
|
61
|
+
def buffer_info
|
62
|
+
@buffer_info ||= BufferInfo.malloc
|
63
|
+
end
|
64
|
+
module_function :buffer_info
|
65
|
+
private :buffer_info
|
66
|
+
|
67
|
+
|
68
|
+
# Sets the cursor position to the specified +x+ and +y+ locations in the
|
69
|
+
# console output buffer. If +y+ is nil, the cursor is positioned at +x+ on
|
70
|
+
# the current line.
|
71
|
+
def set_cursor_position(x, y)
|
72
|
+
if stdout && x && y
|
73
|
+
coord = y << 16 | x
|
74
|
+
self.set_console_cursor_position(stdout, coord) == 0
|
75
|
+
end
|
76
|
+
end
|
77
|
+
module_function :set_cursor_position
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
File without changes
|
File without changes
|
metadata
CHANGED
@@ -1,32 +1,37 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: color-console
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Gardiner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
14
|
-
|
15
|
-
|
13
|
+
description: |2
|
14
|
+
ColorConsole supports cross-platform (ANSI and Windows) colored text output to the console.
|
15
|
+
It also provides useful methods for building command-line interfaces that provide status
|
16
|
+
messages and progress bars.
|
16
17
|
email: adam.b.gardiner@gmail.com
|
17
18
|
executables: []
|
18
19
|
extensions: []
|
19
20
|
extra_rdoc_files: []
|
20
21
|
files:
|
21
|
-
- LICENSE
|
22
22
|
- README.md
|
23
|
+
- LICENSE
|
23
24
|
- lib/color-console.rb
|
25
|
+
- lib/color_console/console.rb
|
26
|
+
- lib/color_console/java_util_logger.rb
|
27
|
+
- lib/color_console/log4r_logger.rb
|
28
|
+
- lib/color_console/platform/ansi.rb
|
29
|
+
- lib/color_console/platform/windows.rb
|
30
|
+
- lib/color_console/platform/windows_ffi.rb
|
31
|
+
- lib/color_console/platform/windows_fiddle.rb
|
32
|
+
- lib/color_console/progress.rb
|
33
|
+
- lib/color_console/table.rb
|
24
34
|
- lib/color_console.rb
|
25
|
-
- lib/console/console.rb
|
26
|
-
- lib/console/platform/ansi.rb
|
27
|
-
- lib/console/platform/windows.rb
|
28
|
-
- lib/console/progress.rb
|
29
|
-
- lib/console/table.rb
|
30
35
|
homepage: https://github.com/agardiner/color-console
|
31
36
|
licenses: []
|
32
37
|
metadata: {}
|
@@ -36,17 +41,17 @@ require_paths:
|
|
36
41
|
- lib
|
37
42
|
required_ruby_version: !ruby/object:Gem::Requirement
|
38
43
|
requirements:
|
39
|
-
- -
|
44
|
+
- - '>='
|
40
45
|
- !ruby/object:Gem::Version
|
41
46
|
version: '0'
|
42
47
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
48
|
requirements:
|
44
|
-
- -
|
49
|
+
- - '>='
|
45
50
|
- !ruby/object:Gem::Version
|
46
51
|
version: '0'
|
47
52
|
requirements: []
|
48
53
|
rubyforge_project:
|
49
|
-
rubygems_version: 2.
|
54
|
+
rubygems_version: 2.0.14
|
50
55
|
signing_key:
|
51
56
|
specification_version: 4
|
52
57
|
summary: ColorConsole is a cross-platform library for outputting colored text to the
|