Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix Ruby 1.8 truncation bug with unicode chars

Closes #229.
  • Loading branch information...
commit debb93061944e5444e6da5be8630054229a930f4 1 parent 3120219
erikhuda authored
Showing with 27 additions and 4 deletions.
  1. +21 −4 lib/thor/shell/basic.rb
  2. +6 −0 spec/shell/basic_spec.rb
View
25 lib/thor/shell/basic.rb
@@ -326,10 +326,27 @@ def unix?
end
def truncate(string, width)
- if string.length <= width
- string
- else
- ( string[0, width-3] || "" ) + "..."
+ as_unicode do
+ chars = string.chars.to_a
+
+ if chars.length <= width
+ chars.join
+ else
+ ( chars[0, width-3].join ) + "..."
+ end
+ end
+ end
+
+ if "".respond_to?(:encode)
+ def as_unicode
+ yield
+ end
+ else
+ def as_unicode
+ old, $KCODE = $KCODE, "U"
+ yield
+ ensure
+ $KCODE = old
end
end
View
6 spec/shell/basic_spec.rb
@@ -1,3 +1,5 @@
+# encoding: UTF-8
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Thor::Shell::Basic do
@@ -145,12 +147,16 @@ def shell
end
it "uses maximum terminal width" do
+ @table << ["def", "#456", "Lançam foo bar"]
+ @table << ["ghi", "#789", "بالله عليكم"]
shell.should_receive(:terminal_width).and_return(20)
content = capture(:stdout){ shell.print_table(@table, :indent => 2, :truncate => true) }
content.should == <<-TABLE
abc #123 firs...
#0 empty
xyz #786 last...
+ def #456 Lanç...
+ ghi #789 بالل...
TABLE
end
Please sign in to comment.
Something went wrong with that request. Please try again.