Optimize left/right trimming
This commit is contained in:
parent
c3c94ea889
commit
7cecf648eb
36
fzf
36
fzf
@ -207,15 +207,35 @@ def ctrl char
|
|||||||
end
|
end
|
||||||
|
|
||||||
if RUBY_VERSION.split('.').map { |e| e.rjust(3, '0') }.join > '001009'
|
if RUBY_VERSION.split('.').map { |e| e.rjust(3, '0') }.join > '001009'
|
||||||
|
@wrx = Regexp.new '\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}'
|
||||||
def width str
|
def width str
|
||||||
@urx ||= Regexp.new '\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}'
|
str.gsub(@wrx, ' ').length
|
||||||
str.gsub(@urx, ' ').length
|
end
|
||||||
|
|
||||||
|
def trim str, len, left
|
||||||
|
width = width str
|
||||||
|
diff = 0
|
||||||
|
while width > len
|
||||||
|
width -= (left ? str[0, 1] : str[-1, 1]) =~ @wrx ? 2 : 1
|
||||||
|
str = left ? str[1..-1] : str[0...-1]
|
||||||
|
diff += 1
|
||||||
|
end
|
||||||
|
[str, diff]
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
def width str
|
def width str
|
||||||
str.length
|
str.length
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def trim str, len, left
|
||||||
|
diff = str.length - len
|
||||||
|
if diff > 0
|
||||||
|
[left ? str[diff..-1] : str[0...-diff], diff]
|
||||||
|
else
|
||||||
|
[str, 0]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class String
|
class String
|
||||||
def ord
|
def ord
|
||||||
self.unpack('c').first
|
self.unpack('c').first
|
||||||
@ -440,19 +460,15 @@ searcher = Thread.new {
|
|||||||
ewidth = width(line[0...e])
|
ewidth = width(line[0...e])
|
||||||
# Stri..
|
# Stri..
|
||||||
if ewidth <= maxc - 2
|
if ewidth <= maxc - 2
|
||||||
line = line[0...-1] while width(line) > maxc - 2
|
line, _ = trim line, maxc - 2, false
|
||||||
line << '..'
|
line << '..'
|
||||||
# ..ring
|
# ..ring
|
||||||
else
|
else
|
||||||
# ..ri..
|
# ..ri..
|
||||||
line = line[0...e] + '..' if ewidth < width(line) - 2
|
line = line[0...e] + '..' if ewidth < width(line) - 2
|
||||||
while width(line) > maxc - 2
|
line, diff = trim line, maxc - 2, true
|
||||||
b -= 1
|
b += 2 - diff
|
||||||
e -= 1
|
e += 2 - diff
|
||||||
line = line[1..-1]
|
|
||||||
end
|
|
||||||
b += 2
|
|
||||||
e += 2
|
|
||||||
b = [2, b].max
|
b = [2, b].max
|
||||||
line = '..' + line
|
line = '..' + line
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user