Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Display Width with Unicode library #92

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions lib/sup/buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,11 @@ def write y, x, s, opts={}
@w.attrset Colormap.color_for(opts[:color] || :none, opts[:highlight])
s ||= ""
maxl = @width - x # maximum display width width
stringl = maxl # string "length"

# fill up the line with blanks to overwrite old screen contents
@w.mvaddstr y, x, " " * maxl unless opts[:no_fill]

## the next horribleness is thanks to ruby's lack of widechar support
stringl += 1 while stringl < s.length && s[0 ... stringl].display_length < maxl
@w.mvaddstr y, x, s[0 ... stringl]
@w.mvaddstr y, x, s.slice_by_display_length(maxl)
end

def clear
Expand Down
8 changes: 4 additions & 4 deletions lib/sup/modes/thread_index_mode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -856,14 +856,14 @@ def text_for_thread_at line

abbrev =
if cur_width + name.display_length > from_width
name[0 ... (from_width - cur_width - 1)] + "."
name.slice_by_display_length(from_width - cur_width - 1) + "."
elsif cur_width + name.display_length == from_width
name[0 ... (from_width - cur_width)]
name.slice_by_display_length(from_width - cur_width)
else
if last
name[0 ... (from_width - cur_width)]
name.slice_by_display_length(from_width - cur_width)
else
name[0 ... (from_width - cur_width - 1)] + ","
name.slice_by_display_length(from_width - cur_width - 1) + ","
end
end

Expand Down
23 changes: 12 additions & 11 deletions lib/sup/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'set'
require 'enumerator'
require 'benchmark'
require 'unicode'

## time for some monkeypatching!
class Symbol
Expand Down Expand Up @@ -254,14 +255,14 @@ def benchmark s, &b
end

class String
## nasty multibyte hack for ruby 1.8. if it's utf-8, split into chars using
## the utf8 regex and count those. otherwise, use the byte length.
def display_length
if RUBY_VERSION < '1.9.1' && ($encoding == "UTF-8" || $encoding == "utf8")
# scan hack is somewhat slow, worth trying to cache
@display_length ||= scan(/./u).size
else
size
@display_length ||= Unicode.width(self, false)
end

def slice_by_display_length len
each_char.each_with_object "" do |c, buffer|
len -= c.display_length
buffer << c if len >= 0
end
end

Expand Down Expand Up @@ -348,14 +349,14 @@ def split_on_commas_with_remainder
def wrap len
ret = []
s = self
while s.length > len
cut = s[0 ... len].rindex(/\s/)
while s.display_length > len
cut = s.slice_by_display_length(len).rindex(/\s/)
if cut
ret << s[0 ... cut]
s = s[(cut + 1) .. -1]
else
ret << s[0 ... len]
s = s[len .. -1]
ret << s.slice_by_display_length(len)
s = s[ret.last.length .. -1]
end
end
ret << s
Expand Down
1 change: 1 addition & 0 deletions sup.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ SUP: Please run `sup-psych-ify-config-files` to migrate from 0.13 to 0.14
s.add_runtime_dependency "mime-types", "~> 1.0"
s.add_runtime_dependency "locale", "~> 2.0"
s.add_runtime_dependency "chronic", "~> 0.9.1"
s.add_runtime_dependency "unicode", "~> 0.4.4"

s.add_development_dependency "bundler", "~> 1.3"
s.add_development_dependency "rake"
Expand Down
57 changes: 57 additions & 0 deletions test/unit/util/test_string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# encoding: utf-8

require "test_helper"

require "sup/util"

describe "Sup's String extension" do
describe "#display_length" do
let :data do
[
['some words', 10,],
['中文', 4,],
['ä', 1,],
]
end

it "calculates display length of a string" do
data.each do |(str, length)|
str.display_length.must_equal length
end
end
end

describe "#slice_by_display_length(len)" do
let :data do
[
['some words', 6, 'some w'],
['中文', 2, '中'],
['älpha', 3, 'älp'],
]
end

it "slices string by display length" do
data.each do |(str, length, sliced)|
str.slice_by_display_length(length).must_equal sliced
end
end
end

describe "#wrap" do
let :data do
[
['some words', 6, ['some', 'words']],
['some words', 80, ['some words']],
['中文', 2, ['中', '文']],
['中文', 5, ['中文']],
['älpha', 3, ['älp', 'ha']],
]
end

it "wraps string by display length" do
data.each do |(str, length, wrapped)|
str.wrap(length).must_equal wrapped
end
end
end
end
File renamed without changes.