Skip to content

Commit

Permalink
move windows part to lib/termtter.rb. and use iconv instead of kconv.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed Jan 14, 2009
1 parent cc75f10 commit 431baaa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 41 deletions.
39 changes: 0 additions & 39 deletions lib/plugin/stdout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,6 @@

$highline = HighLine.new

if win?
require 'kconv'
require 'Win32API'
STD_OUTPUT_HANDLE = 0xFFFFFFF5
$wSetConsoleTextAttribute = Win32API.new('kernel32','SetConsoleTextAttribute','II','I')
$wGetConsoleScreenBufferInfo = Win32API.new("kernel32", "GetConsoleScreenBufferInfo", ['l', 'p'], 'i')
$wGetStdHandle = Win32API.new('kernel32','GetStdHandle','I','I')

$hStdOut = $wGetStdHandle.call(STD_OUTPUT_HANDLE)
lpBuffer = ' ' * 22
$wGetConsoleScreenBufferInfo.call($hStdOut, lpBuffer)
$oldColor = lpBuffer.unpack('SSSSSssssSS')[4]

$colorMap = {
0 => 7, # black/white
37 => 8, # white/intensity
31 => 4 + 8, # red/red
32 => 2 + 8, # green/green
33 => 6 + 8, # yellow/yellow
34 => 1 + 8, # blue/blue
35 => 5 + 8, # magenta/purple
36 => 3 + 8, # cyan/aqua
90 => 7, # erase/white
}
def puts(str)
str = str.to_s.tosjis
tokens = str.split(/(\e\[\d+m)/)
tokens.each do |token|
if token =~ /\e\[(\d+)m/
$wSetConsoleTextAttribute.call $hStdOut, $colorMap[$1.to_i].to_i
else
STDOUT.print token
end
end
$wSetConsoleTextAttribute.call $hStdOut, $oldColor
STDOUT.puts
end
end

def color(str, value)
return str if value == :none
case value
Expand Down
51 changes: 49 additions & 2 deletions lib/termtter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,61 @@ def win?
end

if win?
require 'kconv'
require 'iconv'
require 'Win32API'
$wGetACP = Win32API.new('kernel32','GetACP','','I')

module Readline
$iconv_sj_to_u8 = Iconv.new('UTF-8', "CP#{$wGetACP.call()}")
alias :old_readline :readline
def readline(*a)
old_readline(*a).toutf8
begin
$iconv_sj_to_u8.iconv(old_readline(*a))
rescue
old_readline(*a)
end
end
module_function :old_readline, :readline
end

$wSetConsoleTextAttribute = Win32API.new('kernel32','SetConsoleTextAttribute','II','I')
$wGetConsoleScreenBufferInfo = Win32API.new("kernel32", "GetConsoleScreenBufferInfo", ['l', 'p'], 'i')
$wGetStdHandle = Win32API.new('kernel32','GetStdHandle','I','I')
$wGetACP = Win32API.new('kernel32','GetACP','','I')

$hStdOut = $wGetStdHandle.call(0xFFFFFFF5)
lpBuffer = ' ' * 22
$wGetConsoleScreenBufferInfo.call($hStdOut, lpBuffer)
$oldColor = lpBuffer.unpack('SSSSSssssSS')[4]

$colorMap = {
0 => 7, # black/white
37 => 8, # white/intensity
31 => 4 + 8, # red/red
32 => 2 + 8, # green/green
33 => 6 + 8, # yellow/yellow
34 => 1 + 8, # blue/blue
35 => 5 + 8, # magenta/purple
36 => 3 + 8, # cyan/aqua
90 => 7, # erase/white
}
$iconv_u8_to_sj = Iconv.new("CP#{$wGetACP.call()}", 'UTF-8')
def puts(str)
#str.to_s.tosjis.split(/(\e\[\d+m)/).each do |token|
str.to_s.split(/(\e\[\d+m)/).each do |token|
if token =~ /\e\[(\d+)m/
$wSetConsoleTextAttribute.call $hStdOut, $colorMap[$1.to_i].to_i
else
begin
STDOUT.print $iconv_u8_to_sj.iconv(token)
rescue
STDOUT.print token
end
end
end
$wSetConsoleTextAttribute.call $hStdOut, $oldColor
STDOUT.puts
end
end

def plugin(s)
Expand Down

0 comments on commit 431baaa

Please sign in to comment.