Class: Win32::Console::ANSI::IO

Inherits:
IO
  • Object
show all
Includes:
Constants
Defined in:
lib/Win32/Console/ANSI.rb

Constant Summary collapse

VERSION =
'0.05'
DEBUG =
nil
FD_STD_MAP =
{
  :stdout => [1, STD_OUTPUT_HANDLE],
  :stderr => [2, STD_ERROR_HANDLE]
}
EncodeOk =

@todo: encode is another perl module

false
OEM =
Win32::Console::OutputCP()
@@cp =
cpANSI + cpOEM
@@color =

black foreground

{ 30 => 0,                                               # black foreground
      31 => FOREGROUND_RED,                                  # red foreground
      32 => FOREGROUND_GREEN,                                # green foreground
      33 => FOREGROUND_RED|FOREGROUND_GREEN,                 # yellow foreground
      34 => FOREGROUND_BLUE,                                 # blue foreground
      35 => FOREGROUND_BLUE|FOREGROUND_RED,                  # magenta foreground
      36 => FOREGROUND_BLUE|FOREGROUND_GREEN,                # cyan foreground
      37 => FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE, # white foreground
      40 => 0,                                               # black background
      41 => BACKGROUND_RED,                                  # red background
      42 => BACKGROUND_GREEN,                                # green background
      43 => BACKGROUND_RED|BACKGROUND_GREEN,                 # yellow background
      44 => BACKGROUND_BLUE,                                 # blue background
      45 => BACKGROUND_BLUE|BACKGROUND_RED,                  # magenta background
      46 => BACKGROUND_BLUE|BACKGROUND_GREEN,                # cyan background
      47 => BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE, # white background
}

Constants included from Constants

Constants::BACKGROUND_BLUE, Constants::BACKGROUND_GREEN, Constants::BACKGROUND_INTENSITY, Constants::BACKGROUND_RED, Constants::CAPSLOCK_ON, Constants::CONSOLE_TEXTMODE_BUFFER, Constants::CTRL_BREAK_EVENT, Constants::CTRL_CLOSE_EVENT, Constants::CTRL_C_EVENT, Constants::CTRL_LOGOFF_EVENT, Constants::CTRL_SHUTDOWN_EVENT, Constants::DOUBLE_CLICK, Constants::ENABLE_ECHO_INPUT, Constants::ENABLE_LINE_INPUT, Constants::ENABLE_MOUSE_INPUT, Constants::ENABLE_PROCESSED_INPUT, Constants::ENABLE_PROCESSED_OUTPUT, Constants::ENABLE_WINDOW_INPUT, Constants::ENABLE_WRAP_AT_EOL_OUTPUT, Constants::ENHANCED_KEY, Constants::FILE_SHARE_READ, Constants::FILE_SHARE_WRITE, Constants::FOCUS_EVENT, Constants::FOREGROUND_BLUE, Constants::FOREGROUND_GREEN, Constants::FOREGROUND_INTENSITY, Constants::FOREGROUND_RED, Constants::FROM_LEFT_1ST_BUTTON_PRESSED, Constants::FROM_LEFT_2ND_BUTTON_PRESSED, Constants::FROM_LEFT_3RD_BUTTON_PRESSED, Constants::FROM_LEFT_4TH_BUTTON_PRESSED, Constants::GENERIC_READ, Constants::GENERIC_WRITE, Constants::INVALID_HANDLE_VALUE, Constants::KEY_EVENT, Constants::LEFT_ALT_PRESSED, Constants::LEFT_CTRL_PRESSED, Constants::MENU_EVENT, Constants::MOUSE_EVENT, Constants::MOUSE_MOVED, Constants::MOUSE_WHEELED, Constants::NUMLOCK_ON, Constants::RIGHTMOST_BUTTON_PRESSED, Constants::RIGHT_ALT_PRESSED, Constants::RIGHT_CTRL_PRESSED, Constants::SCROLLLOCK_ON, Constants::SHIFT_PRESSED, Constants::STD_ERROR_HANDLE, Constants::STD_INPUT_HANDLE, Constants::STD_OUTPUT_HANDLE, Constants::WINDOW_BUFFER_SIZE_EVENT

Instance Method Summary collapse

Constructor Details

#initialize(fd_std = :stdout) ⇒ IO

Returns a new instance of IO.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/Win32/Console/ANSI.rb', line 85

def initialize(fd_std = :stdout)
  fd, handle = FD_STD_MAP[fd_std]
  super(fd, 'w')
  @Out = Win32::Console.new(handle)
  @x = @y = 0           # to save cursor position

  # None of these calls will work, when we are redirected
  unless redirected?
    @default_foreground = @Out.DefaultForeground
    @default_background = @Out.DefaultBackground
    @default_bold       = @Out.DefaultBold
    @default_underline  = @Out.DefaultUnderline
  end

  @foreground = @default_foreground
  @background = @default_background
  @bold       = @default_bold
  @underline  = @default_underline
  @revideo =
  @concealed = nil
  @conv = 1        # char conversion by default
  @buffer = []
  STDERR.puts "Console Mode=#{@Out.Mode}" if DEBUG
end

Instance Method Details

#putc(int) ⇒ Object

this redefined #putc buffers escape sequences but passes other values to #write as normal.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/Win32/Console/ANSI.rb', line 112

def putc(int)
  int = int.ord if RUBY_VERSION >= "1.9"
  if @buffer.empty?
    # match \e
    unless int == 27
      write(int.chr)
    else
      @buffer << int
    end
  else
    @buffer << int
    case int
    # match m, J, L, M, @, P, A, B, C, D, E, F, G, H, f, s, u, U, K, X
    when 109, 74, 76, 77, 64, 80, 65, 66, 67, 68, 
          69, 70, 71, 72, 102, 115, 117, 85, 75, 88
      write(@buffer.pack("c*"))
      @buffer.clear
    end
  end
end

#redirected?Boolean

Returns true if output is being redirected to something other then a terminal.

For now just checks the status of Win32::Console#redirected?

Returns:

  • (Boolean)


154
155
156
# File 'lib/Win32/Console/ANSI.rb', line 154

def redirected?
  @Out.redirected?
end

#write(*s) ⇒ Object

#write checks if $stdout is going to the console or if it’s being redirected. When to the console, it passes the string to _PrintString to be parsed for escape codes.

When redirected, it passes to WriteFile to allow escape codes and all to be output. The application that is handling the redirected IO should handle coloring. For Ruby applications, this means requiring Win32Conole again.



142
143
144
145
146
147
148
# File 'lib/Win32/Console/ANSI.rb', line 142

def write(*s)
  if redirected?
    s.each{ |x| @Out.WriteFile(x.dup.to_s) }
  else
    s.each{ |x| _PrintString(x) }
  end
end