sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit ba9ae2492b5553bd71f8a4e216d6f067eca6a0bd
parent 5fa434fe2d3b9b6c425fc602fb0d89ad8c85f20c
Author: Whyme Lyu <callme5long@gmail.com>
Date:   Sun, 30 Jun 2013 16:40:52 +0800

Make String#wrap display_length aware

Diffstat:
M lib/sup/util.rb | 8 ++++----
M test/unit/util/test_string.rb | 18 ++++++++++++++++++
2 files changed, 22 insertions(+), 4 deletions(-)
diff --git a/lib/sup/util.rb b/lib/sup/util.rb
@@ -349,14 +349,14 @@ class String
   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
diff --git a/test/unit/util/test_string.rb b/test/unit/util/test_string.rb
@@ -36,4 +36,22 @@ describe "Sup's String extension" do
       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