Skip to content

Commit

Permalink
remove unused method WrapAt. libyui richtext is better for long texts
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Feb 4, 2015
1 parent 0636454 commit 40e1563
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 124 deletions.
86 changes: 0 additions & 86 deletions library/types/src/modules/String.rb
Original file line number Diff line number Diff line change
Expand Up @@ -892,91 +892,6 @@ def Replace(s, source, target)
s
end

# Returns text wrapped at defined margin. Very useful for translated strings
# used for pop-up windows or dialogs where you can't know the width. It
# controls the maximum width of the string so the text should allways fit into
# the minimal ncurses window. If you expect some long words, such us URLs or
# words with a hyphen inside, you can also set the additional split-characters
# to "/-". Then the function can wrap the word also after these characters.
# This function description was wrapped using the function String::WrapAt().
#
# @example String::WrapAt("Some very long text",30,"/-");
#
# @param [String] text to be wrapped
# @param integer maximum width of the wrapped text
# @param string additional split-characters such as "-" or "/"
# @return [String] wrapped string
def WrapAt(text, width, split_string)
new_string = ""
avail = width # characters available in this line
lsep = "" # set to "\n" when at the beginning of a new line
wsep = "" # set to " " after words, unless at the beginning

Builtins.foreach(Builtins.splitstring(text, " \n")) do |word|
while Ops.greater_than(Builtins.size(word), 0)
# decide where to split the current word
split_at = 0
if Ops.less_or_equal(Builtins.size(word), width)
split_at = Builtins.size(word)
else
split_at = Builtins.findlastof(
Builtins.substring(
word,
0,
Ops.subtract(avail, Builtins.size(wsep))
),
Ops.add(" \n", split_string)
)
if !split_at.nil?
split_at = Ops.add(split_at, 1)
else
split_at = Builtins.findlastof(
Builtins.substring(word, 0, width),
Ops.add(" \n", split_string)
)
if !split_at.nil?
split_at = Ops.add(split_at, 1)
else
split_at = Ops.subtract(avail, Builtins.size(wsep))
end
end
end

# decide whether it fits into the same line or must go on
# a separate line
if Ops.greater_than(Ops.add(Builtins.size(wsep), split_at), avail)
if Ops.greater_than(Builtins.size(new_string), 0)
new_string = Ops.add(new_string, "\n")
end
avail = width
wsep = ""
lsep = ""
end

# add the next word or partial word
new_string = Ops.add(
Ops.add(Ops.add(new_string, lsep), wsep),
Builtins.substring(word, 0, split_at)
)
avail = Ops.subtract(
Ops.subtract(avail, Builtins.size(wsep)),
split_at
)
wsep = ""
lsep = ""
if avail == 0
avail = width
lsep = "\n"
elsif split_at == Builtins.size(word)
wsep = " "
end
word = Builtins.substring(word, split_at)
end
end

new_string
end

# Make a random base-36 number.
# srandom should be called beforehand.
# @param [Fixnum] len string length
Expand Down Expand Up @@ -1145,7 +1060,6 @@ def ReplaceWith(str, chars, glue)
publish function: :TextTable, type: "string (list <string>, list <list <string>>, map <string, any>)"
publish function: :UnderlinedHeader, type: "string (string, integer)"
publish function: :Replace, type: "string (string, string, string)"
publish function: :WrapAt, type: "string (string, integer, string)"
publish function: :Random, type: "string (integer)"
publish function: :FormatFilename, type: "string (string, integer)"
publish function: :RemoveShortcut, type: "string (string)"
Expand Down
38 changes: 0 additions & 38 deletions library/types/test/string_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -566,44 +566,6 @@
end
end

describe ".WrapAt" do
it "wraps text with to have maximum given length" do
long_text = "word " * 100

expected_result = (("word " * 16).strip + "\n") * 6 + ("word " * 4).strip

expect(subject.WrapAt(long_text, 80, "")).to eq expected_result
end

it "allows to specify splitter for long words" do
long_word = "abc-" * 100

expected_result = (("abc-" * 20 + "\n") * 5).chomp

expect(subject.WrapAt(long_word, 80, "-")).to eq expected_result
end

it "returns empty string if text is nil" do
expect(subject.WrapAt(nil, 80, "-")).to eq ""
end

it "returns nil string if length is nil" do
expect(subject.WrapAt("abc" * 500, nil, "-")).to eq nil
end

it "acts like empty split_string if it is nil" do
long_text = "word " * 100

expected_result = (("word " * 16).strip + "\n") * 6 + ("word " * 4).strip

expect(subject.WrapAt(long_text, 80, nil)).to eq expected_result
end

it "returns empty string if text and length are nil" do
expect(subject.WrapAt(nil, nil, nil)).to eq ""
end
end

describe ".Random" do
it "generates random 36-base number with given length" do
Yast::Builtins.srandom(50) # ensure we get same number
Expand Down

0 comments on commit 40e1563

Please sign in to comment.