ruby-reference-manual:534
From: Kouhei Sutou <kou@c...>
Date: Mon, 29 Oct 2007 00:36:32 +0900 (JST)
Subject: [ruby-reference-manual:534] UTF-8な端末でも文字化けせずに出力する
須藤です。
端末のエンコーディングがEUC-JP以外の場合、refeの出力が文字化
けしてしまいます。
やっつけですが、出力される内容をLANGに指定されているエンコー
ディングに変換するパッチです。
Index: lib/bitclust/searcher.rb
===================================================================
--- lib/bitclust/searcher.rb (リビジョン 2217)
+++ lib/bitclust/searcher.rb (作業コピー)
@@ -14,6 +14,7 @@
require 'uri'
require 'rbconfig'
require 'optparse'
+require 'iconv'
module BitClust
@@ -140,7 +141,9 @@
end
def new_database
- Database.connect(@dblocation || dblocation())
+ db = Database.connect(@dblocation || dblocation())
+ @view.database = db if @view
+ db
end
def dblocation_name
@@ -284,10 +287,12 @@
class TerminalView
+ attr_accessor :database
def initialize(compiler, opts)
@compiler = compiler
@describe_all = opts[:describe_all]
@line = opts[:line]
+ @database = nil
end
def show_class(cs)
@@ -395,6 +400,30 @@
puts
end
+ def puts(*args)
+ super(*args.collect {|arg| convert(arg)})
+ end
+
+ def convert(string)
+ return string if @database.nil?
+ _output_encoding = output_encoding
+ return string if _output_encoding.nil?
+ begin
+ Iconv.iconv(_output_encoding, @database.encoding, string)
+ rescue Iconv::InvalidEncoding
+ string
+ end
+ end
+
+ def output_encoding
+ locale = ENV["LC_ALL"] || ENV["LC_MESSAGE"] || ENV["LANG"]
+ case locale
+ when /\.([a-z\d\-]+)\z/i
+ $1
+ else
+ nil
+ end
+ end
end
end
--
ML: ruby-reference-manual@m...
使い方: http://QuickML.com/
-> 534 2007-10-28 16:36 [kou@c... ] UTF-8な端末でも文字化けせずに出力する 547 2007-11-04 13:47 ┗[aamine@l... ] 549 2007-11-04 14:14 ┗[kou@c... ]